-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
153 lines (148 loc) · 5.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2022 The FeatHub Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import sys
import time
from datetime import timedelta
from feathub.common import types
from feathub.feathub_client import FeathubClient
from feathub.feature_tables.sinks.black_hole_sink import BlackHoleSink
from feathub.feature_tables.sources.datagen_source import DataGenSource, RandomField
from feathub.feature_views.feature import Feature
from feathub.feature_views.sliding_feature_view import SlidingFeatureView
from feathub.feature_views.transforms.sliding_window_transform import (
SlidingWindowTransform,
)
from feathub.table.schema import Schema
def run(records_num: int):
client = FeathubClient(
props={
"processor": {
"type": "flink",
"flink": {
"master": "localhost:8081",
},
},
"online_store": {
"types": ["memory"],
"memory": {},
},
"registry": {
"type": "local",
"local": {
"namespace": "default",
},
},
"feature_service": {
"type": "local",
"local": {},
},
}
)
purchase_events_schema = (
Schema.new_builder()
.column("user_id", types.Int32)
.column("item_id", types.Int32)
.column("item_count", types.Int32)
.column("timestamp", types.Timestamp)
.build()
)
purchase_events_source = DataGenSource(
name="purchase_events",
schema=purchase_events_schema,
number_of_rows=records_num,
rows_per_second=sys.maxsize,
field_configs={
"user_id": RandomField(minimum=0, maximum=9),
"item_id": RandomField(minimum=0, maximum=9),
"item_count": RandomField(minimum=1, maximum=4),
},
keys=["user_id"],
timestamp_field="timestamp",
)
step_size = timedelta(milliseconds=2)
user_purchase_cnt_features = SlidingFeatureView(
name="user_purchase_cnt_features",
source=purchase_events_source,
features=[
Feature(
name="total_purchase_2_ms",
transform=SlidingWindowTransform(
expr="item_count",
agg_func="SUM",
window_size=timedelta(milliseconds=2),
step_size=step_size,
group_by_keys=["user_id"],
),
),
Feature(
name="total_purchase_5_ms",
transform=SlidingWindowTransform(
expr="item_count",
agg_func="SUM",
window_size=timedelta(milliseconds=5),
step_size=step_size,
group_by_keys=["user_id"],
),
),
Feature(
name="total_purchase_10_ms",
transform=SlidingWindowTransform(
expr="item_count",
agg_func="SUM",
window_size=timedelta(milliseconds=10),
step_size=step_size,
group_by_keys=["user_id"],
),
),
Feature(
name="total_purchase_100_ms",
transform=SlidingWindowTransform(
expr="item_count",
agg_func="SUM",
window_size=timedelta(milliseconds=100),
step_size=step_size,
group_by_keys=["user_id"],
),
),
Feature(
name="total_purchase_10_s",
transform=SlidingWindowTransform(
expr="item_count",
agg_func="SUM",
window_size=timedelta(seconds=10),
step_size=step_size,
group_by_keys=["user_id"],
),
),
],
)
job = client.materialize_features(
features=user_purchase_cnt_features, sink=BlackHoleSink(), allow_overwrite=True
)
try:
start_time = time.time()
job.wait()
run_time = time.time() - start_time
print(
f"Num records {records_num}, run time: {run_time:.2f} seconds, "
f"throughput: {int(records_num / run_time)} rps"
)
finally:
job.cancel()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--records-num", type=int, default=10_000_000)
args = parser.parse_args()
run(args.records_num)