forked from grafana/pyroscope-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-thread.rs
123 lines (98 loc) · 3.07 KB
/
multi-thread.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
extern crate pyroscope;
use pyroscope::{PyroscopeAgent, Result};
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
thread,
};
fn hash_rounds1(n: u64) -> u64 {
let hash_str = "Some string to hash";
let mut default_hasher = DefaultHasher::new();
for _ in 0..n {
for _ in 0..1000 {
default_hasher.write(hash_str.as_bytes());
}
hash_str.hash(&mut default_hasher);
}
n
}
fn hash_rounds2(n: u64) -> u64 {
let hash_str = "Some string to hash";
let mut default_hasher = DefaultHasher::new();
for _ in 0..n {
for _ in 0..1000 {
default_hasher.write(hash_str.as_bytes());
}
hash_str.hash(&mut default_hasher);
}
n
}
fn extra_rounds1(n: u64) -> u64 {
let hash_str = "Some string to hash";
let mut default_hasher = DefaultHasher::new();
for _ in 0..n {
for _ in 0..1000 {
default_hasher.write(hash_str.as_bytes());
}
hash_str.hash(&mut default_hasher);
}
n
}
fn extra_rounds2(n: u64) -> u64 {
let hash_str = "Some string to hash";
let mut default_hasher = DefaultHasher::new();
for _ in 0..n {
for _ in 0..1000 {
default_hasher.write(hash_str.as_bytes());
}
hash_str.hash(&mut default_hasher);
}
n
}
fn main() -> Result<()> {
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.multithread")
.tags([("Host", "Rust")].to_vec())
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
.build()?;
// Show start time
let start = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
println!("Start Time: {}", start);
// Start Agent
let agent_running = agent.start()?;
let (add_tag, remove_tag) = agent_running.tag_wrapper();
let handle_1 = thread::Builder::new()
.name("thread-1".to_string())
.spawn(move || {
hash_rounds1(300_000);
add_tag("extra".to_string(), "round-1".to_string()).unwrap();
extra_rounds1(200_000);
remove_tag("extra".to_string(), "round-1".to_string()).unwrap();
})?;
let (add_tag, remove_tag) = agent_running.tag_wrapper();
let handle_2 = thread::Builder::new()
.name("thread-2".to_string())
.spawn(move || {
add_tag("extra".to_string(), "round-2".to_string()).unwrap();
extra_rounds2(100_000);
remove_tag("extra".to_string(), "round-2".to_string()).unwrap();
hash_rounds2(500_000);
})?;
// Wait for the threads to complete
handle_1.join().unwrap();
handle_2.join().unwrap();
// Stop Agent
let agent_ready = agent_running.stop()?;
// Shutdown the Agent
agent_ready.shutdown();
// Show program exit time
let exit = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
println!("Exit Time: {}", exit);
Ok(())
}