forked from fleek-network/latency-measure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
329 lines (276 loc) · 10.1 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
mod collect;
mod jobs;
use std::{collections::HashMap, error::Error, fmt::Write};
use clap::Parser;
use indicatif::{ProgressState, ProgressStyle};
use jobs::Jobs;
use measure::{MeasureDurationRequest, MeasureRequest, MeasureResponse};
use reqwest::{ClientBuilder, RequestBuilder};
use serde::{Deserialize, Serialize};
use tabled::builder::Builder;
#[derive(Parser)]
pub struct CliArgs {
/// The url the measure service will be making the http request to
target_request_url: Option<String>,
/// The HTTP method for the http request the measure service will be making to the target url
#[clap(long)]
target_request_method: Option<String>,
/// The HTTP body for the http request the measure service will be making to the target url
#[clap(long)]
target_request_body: Option<String>,
/// The HTTP headers for the http request the measure service will be making to the target url
#[arg(value_parser = parse_key_val::<String, String>)]
#[clap(long)]
target_request_headers: Option<Vec<(String, String)>>,
/// The comparison url the measure service will be calling the http `get` method` on
#[clap(long = "comp")]
comparison_url: Option<String>,
/// The ip address of the measure services
#[clap(long)]
services: Option<Vec<String>>,
/// Compute and print the average of the results
#[clap(short, long)]
average: bool,
/// The number of times to get a latencty measurement from service
#[clap(short, long, default_value_t = 10)]
times: usize,
/// The delay in milliseconds between each measurement
#[clap(short, long, default_value_t = 500)]
delay: usize,
/// The output file to write the json results to
#[clap(short, long)]
output_dir: Option<String>,
/// Creates requests concurrently rather than sequentially
/// and ignores the delay param
#[clap(long)]
flood: bool,
}
/// Parse a single key-value pair
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
T: std::str::FromStr,
T::Err: Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: Error + Send + Sync + 'static,
{
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = CliArgs::parse();
let _ = Runtime::new(args)?.start().await?;
Ok(())
}
#[derive(Debug)]
struct Runtime {
jobs: Jobs,
results: HashMap<String, Vec<MeasureResponse>>,
comparison_results: Option<HashMap<String, Vec<MeasureResponse>>>,
output_dir: Option<String>,
average: bool,
times: usize,
delay: usize,
}
#[derive(Debug, Serialize, Deserialize)]
struct Output {
/// mapping from service ip to the results of the target url
target_results: HashMap<String, Vec<MeasureResponse>>,
/// mapping from service ip to the results of the comparison url
comparison_results: Option<HashMap<String, Vec<MeasureResponse>>>,
}
impl Runtime {
fn new(args: CliArgs) -> anyhow::Result<Self> {
Ok(Runtime {
jobs: args.jobs()?,
results: HashMap::new(),
comparison_results: args.comparison_url.map(|_| HashMap::new()),
average: args.average,
times: args.times,
delay: args.delay,
output_dir: args.output_dir,
})
}
async fn start(mut self) -> anyhow::Result<()> {
let Jobs {
services,
target_url,
target_method,
target_body,
target_headers,
comparison_url,
} = self.jobs.clone();
for service_ip in services {
println!("running for: {}", service_ip);
self.run(
service_ip,
target_url.clone(),
target_method.clone(),
target_body.clone(),
target_headers.clone(),
comparison_url.clone(),
)
.await?;
}
let output = self.output();
for (ip, results) in output.target_results.iter() {
let mut builder = Builder::default();
// Push the header row (0..self.times)
builder.push_record(
std::iter::once(String::from(""))
.chain((0..self.times).map(|i| (i + 1).to_string())),
);
// Push the target url and the results
builder.push_record(
std::iter::once(target_url.clone()).chain(
results
.iter()
.map(|res| format!("{}ms", res.overall_duration.unwrap().as_millis())),
),
);
// Push the comparison url and the results if applicable
if let Some(ref comp) = output.comparison_results {
let comp = comp.get(ip).expect("comparison results for this ip");
builder.push_record(
std::iter::once(comparison_url.as_ref().expect("comparison url").clone())
.chain(
comp.iter()
.map(|res| format!("{}ms", res.overall_duration.unwrap().as_millis())),
),
);
}
println!("Results for service ip: {}", ip);
println!("{}", builder.build());
}
if let Some(ref dir) = self.output_dir {
// theres no other tasks running so blocking is acceptable
std::fs::create_dir_all(dir)?;
let timestamp = chrono::Utc::now().to_rfc3339();
let mut file = std::fs::File::create(format!("{}/{}.json", dir, timestamp))?;
serde_json::to_writer(&mut file, &output)?;
}
Ok(())
}
async fn run(
&mut self,
service_ip: String,
target_url: String,
target_method: String,
target_body: Option<String>,
target_headers: Option<HashMap<String, String>>,
maybe_comp: Option<String>,
) -> anyhow::Result<()> {
if target_body.is_some() && target_method != "POST" {
return Err(anyhow::anyhow!("body is only supported for POST requests"));
}
let req = make_request(
&service_ip,
&target_url,
&target_method,
&target_headers,
&target_body,
)?;
println!("measuring target ttfb");
self.results.insert(
service_ip.clone(),
Self::measure(req, self.times, self.delay).await?,
);
if let Some(ref url) = maybe_comp {
let comparison_req = make_request(
&service_ip,
&url,
&target_method,
&target_headers,
&target_body,
)?;
println!("measuring comparison ttfb");
self.comparison_results
.as_mut()
.expect("comparison results")
.insert(
service_ip.clone(),
Self::measure(comparison_req, self.times, self.delay).await?,
);
}
if self.average {
let target = collect::average(
self.results
.get(&service_ip)
.expect("results for this ip")
.iter(),
self.times,
);
print_average(target_url, target);
match self.comparison_results {
Some(ref comp) => {
let comp = collect::average(
comp.get(&service_ip).expect("results for this ip").iter(),
self.times,
);
print_average(maybe_comp.expect("comparison url"), comp);
}
None => (),
};
}
Ok(())
}
async fn measure(
req: reqwest::RequestBuilder,
times: usize,
delay: usize,
) -> anyhow::Result<Vec<MeasureResponse>> {
let mut buf = Vec::with_capacity(times);
let pb = indicatif::ProgressBar::new(times as u64);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
for i in 0..times {
let cloned = req
.try_clone()
.ok_or(anyhow::anyhow!("failed to clone request"))?;
let res = cloned.send().await?.json::<MeasureResponse>().await?;
buf.push(res);
pb.set_position(i as u64);
tokio::time::sleep(tokio::time::Duration::from_millis(delay as u64)).await;
}
Ok(buf)
}
fn output(&self) -> Output {
Output {
target_results: self.results.clone(),
comparison_results: self.comparison_results.clone(),
}
}
}
fn make_request(
service_ip: &String,
target_url: &String,
target_method: &String,
target_headers: &Option<HashMap<String, String>>,
target_body: &Option<String>,
) -> Result<RequestBuilder, reqwest::Error> {
let req = ClientBuilder::new().build()?;
let req = if target_method != "GET" {
req.post(format!("{0}/duration", &service_ip))
.json(&MeasureDurationRequest {
target: target_url.clone(),
method: target_method.clone(),
headers: target_headers.clone(),
body: target_body.clone(),
})
} else {
req.post(format!("{0}/ttfb", &service_ip))
.json(&MeasureRequest {
target: target_url.clone(),
})
};
Ok(req)
}
fn print_average(label: String, measure: MeasureResponse) {
println!("URL: {:#?}", label);
println!("Average: {}ms", measure.ttfb_duration.as_millis());
println!("Overall: {}ms", measure.overall_duration.unwrap().as_millis());
}