-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.rs
197 lines (182 loc) · 6.24 KB
/
generator.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::event::Event;
use crate::Record;
use tokio::task::JoinHandle;
use tracing::info;
use std::sync::Arc;
pub struct Generator {
events: Vec<Event>,
joins: Vec<JoinHandle<()>>,
}
impl Generator {
pub fn from_records(records: Vec<Record>, step: usize, scale: u32) -> Generator {
let mut events = Vec::new();
for mut record in records.into_iter() {
record.start /= scale;
record.end /= scale;
let event = Event::new(record, step / scale as usize);
events.push(event);
}
events.sort();
Generator {
events,
joins: Vec::new(),
}
}
pub async fn start(&mut self) -> Result<(), crate::Error> {
info!("Get ready to face your phobia :))");
let mut current: u32 = 0;
for mut event in self.events.clone().into_iter() {
if current < event.record.start {
let delay = (event.record.start - current) as u64;
let duration = tokio::time::Duration::from_secs(delay);
tokio::time::sleep(duration).await;
current = event.record.start;
}
let join = tokio::spawn(async move {
info!("Generator starting event: {:?}", event);
let _ = event.run().await;
});
self.joins.push(join);
}
Ok(())
}
pub async fn start_unsafe(&'static mut self) -> Result<(), crate::Error> {
info!("Get ready to face your phobia :))");
let mut current: u32 = 0;
for event in self.events.iter() {
if current < event.record.start {
let delay = (event.record.start - current) as u64;
let duration = tokio::time::Duration::from_secs(delay);
tokio::time::sleep(duration).await;
current = event.record.start;
}
let event = Arc::new(event);
let join = tokio::spawn(async move {
info!("Generator starting event: {:?}", event);
match event.clone().run_unsafe().await {
Ok(_) => (),
Err(err) => info!("{:?} failed because {err}", event),
}
});
self.joins.push(join);
}
Ok(())
}
pub async fn start_leak(&mut self) -> Result<(), crate::Error> {
info!("Get ready to face your phobia :))");
let mut current: u32 = 0;
for mut event in self.events.clone().into_iter() {
if current < event.record.start {
let delay = (event.record.start - current) as u64;
let duration = tokio::time::Duration::from_secs(delay);
tokio::time::sleep(duration).await;
current = event.record.start;
}
let join = tokio::spawn(async move {
info!("Generator starting event: {:?}", event);
let _ = event.run_leak().await;
});
self.joins.push(join);
}
Ok(())
}
pub async fn wait(self) -> Result<(), crate::Error> {
for join in self.joins.into_iter() {
tokio::join!(join).0?;
}
Ok(())
}
}
#[cfg(test)]
mod generator_tests {
use crate::generator::Generator;
use httpmock::prelude::*;
#[test]
fn test_from_records() {
let record1 = crate::Record {
method: "POST".into(),
host: "http://localhost".into(),
start: 0,
end: 2,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let record2 = crate::Record {
method: "POST".into(),
host: "http://localhost".into(),
start: 1,
end: 2,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let record3 = crate::Record {
method: "POST".into(),
host: "http://localhost".into(),
start: 0,
end: 1,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let records = vec![record2, record3.clone(), record1];
let generator = Generator::from_records(records, 1, 1);
assert!(generator.events.get(0).unwrap().record.end == record3.end);
}
#[tokio::test]
async fn test_generator() -> Result<(), crate::Error> {
let server = MockServer::start();
let mock = server.mock(|when, then| {
when.method(POST)
.path("/yolo/v2/predict")
.body_contains("file");
then.status(200);
});
let record1 = crate::Record {
method: "POST".into(),
host: server.base_url(),
start: 0,
end: 8,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let record2 = crate::Record {
method: "POST".into(),
host: server.base_url(),
start: 2,
end: 4,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let record3 = crate::Record {
method: "POST".into(),
host: server.base_url(),
start: 0,
end: 8,
path: "/yolo/v2/predict".into(),
body: crate::Body::MULTIPART {
path: "./tests/data/test_data.yaml".into(),
name: "file".into(),
},
};
let records = vec![record2, record3, record1];
let mut generator = Generator::from_records(records, 2, 2);
generator.start().await?;
generator.wait().await?;
mock.assert_hits(9);
Ok(())
}
}