forked from deib-polimi/renoir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_top_words_e2e.rs
158 lines (144 loc) · 3.59 KB
/
rolling_top_words_e2e.rs
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
154
155
156
157
158
use std::time::Instant;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use nanorand::Rng;
use renoir::prelude::*;
const TOPICS: [&str; 50] = [
"#love",
"#instagood",
"#fashion",
"#photooftheday",
"#beautiful",
"#art",
"#photography",
"#happy",
"#picoftheday",
"#cute",
"#follow",
"#tbt",
"#followme",
"#nature",
"#like",
"#travel",
"#instagram",
"#style",
"#repost",
"#summer",
"#instadaily",
"#selfie",
"#me",
"#friends",
"#fitness",
"#girl",
"#food",
"#fun",
"#beauty",
"#instalike",
"#smile",
"#family",
"#photo",
"#life",
"#likeforlike",
"#music",
"#ootd",
"#follow",
"#makeup",
"#amazing",
"#igers",
"#nofilter",
"#dog",
"#model",
"#sunset",
"#beach",
"#instamood",
"#foodporn",
"#motivation",
"#followforfollow",
];
const PROB: f64 = 0.1;
fn random_topic() -> String {
let mut rng = nanorand::tls_rng();
for topic in TOPICS {
if rng.generate::<f64>() < PROB {
return topic.to_string();
}
}
TOPICS[0].to_string()
}
struct TopicSource {
id: u64,
instances: u64,
num_gen: u64,
limit: u64,
}
impl TopicSource {
fn new(id: u64, instances: u64, limit: u64) -> Self {
Self {
id,
instances,
num_gen: 0,
limit,
}
}
}
impl Iterator for TopicSource {
type Item = (i64, String);
fn next(&mut self) -> Option<Self::Item> {
let nth = self.num_gen * self.instances + self.id;
if nth > self.limit {
return None;
}
let topic = random_topic();
let ts_millis = nth as i64;
self.num_gen += 1;
Some((ts_millis, topic))
}
}
fn main() {
let win_size_millis = 1000;
let win_step_millis = 500;
let k = 4;
let (config, args) = RuntimeConfig::from_args();
let limit: u64 = args[1].parse().expect("Invalid number of events");
config.spawn_remote_workers();
let env = StreamContext::new(config);
let source =
ParallelIteratorSource::new(move |id, instances| TopicSource::new(id, instances, limit));
env.stream(source)
// add a timestamp for each item (using the one generated by the source) and add a watermark
// every 10 items
.add_timestamps(|(ts, _)| *ts, {
let mut count = 0;
move |_, &ts| {
count += 1;
if count % 10 == 0 {
Some(ts)
} else {
None
}
}
})
// forget about the timestamp, it's already attached to the messages
.map(|(_ts, w)| w)
// count each word separately
.group_by(|w| w.clone())
.window(EventTimeWindow::sliding(win_size_millis, win_step_millis))
// count how many times each word appears in the window
.map(|w| w.len())
.unkey()
// this window has the same alignment of the previous one, so it will contain the same items
.window_all(EventTimeWindow::tumbling(win_step_millis))
.map(move |mut words| {
// find the k most frequent words for each window
words.sort_unstable_by_key(|(_w, c)| -(*c as i64));
words.truncate(k);
words
})
.drop_key()
.flatten()
.fold(0, |acc, _| *acc += 1)
.for_each(|c| println!("{c}"));
let start = Instant::now();
env.execute_blocking();
println!("Elapsed: {:?}", start.elapsed());
}