Skip to content

Commit ae80db1

Browse files
committed
imp(examples): update multi-threading examples
1 parent 1fa9ebb commit ae80db1

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

examples/multi-thread.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate pyroscope;
22

3-
use pyroscope::{PyroscopeAgent, Result};
3+
use pyroscope::{backend::Tag, PyroscopeAgent, Result};
44
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
55
use std::{
66
collections::hash_map::DefaultHasher,
@@ -36,6 +36,34 @@ fn hash_rounds2(n: u64) -> u64 {
3636
n
3737
}
3838

39+
fn extra_rounds1(n: u64) -> u64 {
40+
let hash_str = "Some string to hash";
41+
let mut default_hasher = DefaultHasher::new();
42+
43+
for _ in 0..n {
44+
for _ in 0..1000 {
45+
default_hasher.write(hash_str.as_bytes());
46+
}
47+
hash_str.hash(&mut default_hasher);
48+
}
49+
50+
n
51+
}
52+
53+
fn extra_rounds2(n: u64) -> u64 {
54+
let hash_str = "Some string to hash";
55+
let mut default_hasher = DefaultHasher::new();
56+
57+
for _ in 0..n {
58+
for _ in 0..1000 {
59+
default_hasher.write(hash_str.as_bytes());
60+
}
61+
hash_str.hash(&mut default_hasher);
62+
}
63+
64+
n
65+
}
66+
3967
fn main() -> Result<()> {
4068
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.multithread")
4169
.tags([("Host", "Rust")].to_vec())
@@ -52,15 +80,25 @@ fn main() -> Result<()> {
5280
// Start Agent
5381
agent.start()?;
5482

83+
let (add_tag, remove_tag) = agent.tag_wrapper();
84+
5585
let handle_1 = thread::Builder::new()
5686
.name("thread-1".to_string())
57-
.spawn(|| {
87+
.spawn(move || {
5888
hash_rounds1(300_000);
89+
add_tag("extra".to_string(), "round-1".to_string()).unwrap();
90+
extra_rounds1(200_000);
91+
remove_tag("extra".to_string(), "round-1".to_string()).unwrap();
5992
})?;
6093

94+
let (add_tag, remove_tag) = agent.tag_wrapper();
95+
6196
let handle_2 = thread::Builder::new()
6297
.name("thread-2".to_string())
63-
.spawn(|| {
98+
.spawn(move || {
99+
add_tag("extra".to_string(), "round-2".to_string()).unwrap();
100+
extra_rounds2(100_000);
101+
remove_tag("extra".to_string(), "round-2".to_string()).unwrap();
64102
hash_rounds2(500_000);
65103
})?;
66104

0 commit comments

Comments
 (0)