forked from synesissoftware/p99.Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_histogram.rs
More file actions
78 lines (60 loc) · 2.14 KB
/
Copy pathbuild_histogram.rs
File metadata and controls
78 lines (60 loc) · 2.14 KB
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
// examples/build_histogram.rs : example program showing `Histogram`
use p99::Histogram;
#[rustfmt::skip]
use std::{
env as std_env,
thread as std_thread,
time as std_time,
};
struct SimpleRng {
state: u64,
}
// API functions
impl SimpleRng {
fn new(seed: u64) -> Self {
Self { state: seed }
}
}
// Mutating methods
impl SimpleRng {
fn next(&mut self) -> u64 {
self.state = self.state.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
self.state
}
}
// Helper functions
fn main() {
let tries = match std_env::var("P99_TRIES") {
Ok(val) => match val.parse::<usize>() {
Ok(num) => num,
Err(_) => {
eprintln!("Warning: failed to parse P99_TRIES value '{}', defaulting to 100", val);
100
},
},
Err(_) => 100,
};
println!("Running Histogram example with {} tries...", tries);
let mut histogram = Histogram::default();
let mut rng = SimpleRng::new(12_345);
for _ in 0..tries {
// Generate a random delay from 1 to 500 microseconds.
let delay_us = (rng.next() % 1_000) + 1;
let start = std_time::Instant::now();
std_thread::sleep(std_time::Duration::from_micros(delay_us));
let elapsed = start.elapsed();
histogram.push_event_duration(elapsed);
}
println!("\nHistogram printed via `{{:#?}}` format:\n");
println!("{:#?}", histogram);
println!("\nPercentiles (approximated):");
println!(" p50 (f64): {:?} ns", histogram.value_at_percentile(50.0));
println!(" p50 (integer): {:?} ns", histogram.value_at_p50());
println!(" p75 (integer): {:?} ns", histogram.value_at_p75());
println!(" p90 (integer): {:?} ns", histogram.value_at_p90());
println!(" p95 (integer): {:?} ns", histogram.value_at_p95());
println!(" p99 (integer): {:?} ns", histogram.value_at_p99());
println!(" p99.5 (integer): {:?} ns", histogram.value_at_p99_5());
println!(" p99.9 (integer): {:?} ns", histogram.value_at_p99_9());
println!(" p99.99 (integer): {:?} ns", histogram.value_at_p99_99());
}