Skip to content

Commit dd00869

Browse files
committed
refactor(example): change examples stress function
1 parent be49593 commit dd00869

File tree

6 files changed

+108
-44
lines changed

6 files changed

+108
-44
lines changed

examples/async.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
extern crate pyroscope;
22

33
use pyroscope::{PyroscopeAgent, Result};
4+
use std::hash::{Hash, Hasher};
45

5-
fn fibonacci1(n: u64) -> u64 {
6-
match n {
7-
0 | 1 => 1,
8-
n => fibonacci1(n - 1) + fibonacci1(n - 2),
6+
fn hash_rounds1(n: u64) -> u64 {
7+
let hash_str = "Some string to hash";
8+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
9+
10+
for _ in 0..n {
11+
for _ in 0..1000 {
12+
default_hasher.write(hash_str.as_bytes());
13+
}
14+
hash_str.hash(&mut default_hasher);
915
}
16+
17+
n
1018
}
1119

12-
fn fibonacci2(n: u64) -> u64 {
13-
match n {
14-
0 | 1 => 1,
15-
n => fibonacci2(n - 1) + fibonacci2(n - 2),
20+
fn hash_rounds2(n: u64) -> u64 {
21+
let hash_str = "Some string to hash";
22+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
23+
24+
for _ in 0..n {
25+
for _ in 0..1000 {
26+
default_hasher.write(hash_str.as_bytes());
27+
}
28+
hash_str.hash(&mut default_hasher);
1629
}
30+
31+
n
1732
}
1833

1934
#[tokio::main]
@@ -26,14 +41,14 @@ async fn main() -> Result<()> {
2641
agent.start()?;
2742

2843
tokio::task::spawn(async {
29-
let n = fibonacci1(45);
44+
let n = hash_rounds1(300_000);
3045
println!("Thread 1: {}", n);
3146
})
3247
.await
3348
.unwrap();
3449

3550
tokio::task::spawn(async {
36-
let n = fibonacci2(45);
51+
let n = hash_rounds2(300_000);
3752
println!("Thread 2: {}", n);
3853
})
3954
.await

examples/backend.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ extern crate pyroscope;
22

33
use pyroscope::{PyroscopeAgent, Result};
44
use pyroscope_backends::pprof::{Pprof, PprofConfig};
5+
use std::hash::{Hash, Hasher};
56

6-
fn fibonacci(n: u64) -> u64 {
7-
match n {
8-
0 | 1 => 1,
9-
n => fibonacci(n - 1) + fibonacci(n - 2),
7+
fn hash_rounds(n: u64) -> u64 {
8+
let hash_str = "Some string to hash";
9+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
10+
11+
for _ in 0..n {
12+
for _ in 0..1000 {
13+
default_hasher.write(hash_str.as_bytes());
14+
}
15+
hash_str.hash(&mut default_hasher);
1016
}
17+
18+
n
1119
}
1220

1321
fn main() -> Result<()> {
@@ -17,7 +25,7 @@ fn main() -> Result<()> {
1725
.build()?;
1826

1927
agent.start()?;
20-
let _result = fibonacci(45);
28+
let _result = hash_rounds(300_000);
2129
agent.stop()?;
2230

2331
Ok(())

examples/basic.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
extern crate pyroscope;
22

33
use pyroscope::{PyroscopeAgent, Result};
4+
use std::hash::{Hash, Hasher};
45

5-
fn fibonacci(n: u64) -> u64 {
6-
match n {
7-
0 | 1 => 1,
8-
n => fibonacci(n - 1) + fibonacci(n - 2),
6+
fn hash_rounds(n: u64) -> u64 {
7+
let hash_str = "Some string to hash";
8+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
9+
10+
for _ in 0..n {
11+
for _ in 0..1000 {
12+
default_hasher.write(hash_str.as_bytes());
13+
}
14+
hash_str.hash(&mut default_hasher);
915
}
16+
17+
n
1018
}
1119

1220
fn main() -> Result<()> {
@@ -24,7 +32,7 @@ fn main() -> Result<()> {
2432
// Start Agent
2533
agent.start()?;
2634

27-
let _result = fibonacci(47);
35+
let _result = hash_rounds(300_000);
2836

2937
// Show stop time
3038
let stop = std::time::SystemTime::now()

examples/multi-thread.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
extern crate pyroscope;
22

33
use pyroscope::{PyroscopeAgent, Result};
4-
5-
use std::thread;
6-
7-
fn fibonacci1(n: u64) -> u64 {
8-
match n {
9-
0 | 1 => 1,
10-
n => fibonacci1(n - 1) + fibonacci1(n - 2),
4+
use std::{
5+
collections::hash_map::DefaultHasher,
6+
hash::{Hash, Hasher},
7+
thread,
8+
};
9+
10+
fn hash_rounds1(n: u64) -> u64 {
11+
let hash_str = "Some string to hash";
12+
let mut default_hasher = DefaultHasher::new();
13+
14+
for _ in 0..n {
15+
for _ in 0..1000 {
16+
default_hasher.write(hash_str.as_bytes());
17+
}
18+
hash_str.hash(&mut default_hasher);
1119
}
20+
21+
n
1222
}
1323

14-
fn fibonacci2(n: u64) -> u64 {
15-
match n {
16-
0 | 1 => 1,
17-
n => fibonacci2(n - 1) + fibonacci2(n - 2),
24+
fn hash_rounds2(n: u64) -> u64 {
25+
let hash_str = "Some string to hash";
26+
let mut default_hasher = DefaultHasher::new();
27+
28+
for _ in 0..n {
29+
for _ in 0..1000 {
30+
default_hasher.write(hash_str.as_bytes());
31+
}
32+
hash_str.hash(&mut default_hasher);
1833
}
34+
35+
n
1936
}
2037

2138
fn main() -> Result<()> {
@@ -26,11 +43,11 @@ fn main() -> Result<()> {
2643
agent.start()?;
2744

2845
let handle_1 = thread::spawn(|| {
29-
fibonacci1(45);
46+
hash_rounds1(300_000);
3047
});
3148

3249
let handle_2 = thread::spawn(|| {
33-
fibonacci2(45);
50+
hash_rounds2(500_000);
3451
});
3552

3653
// Wait for the threads to complete

examples/tags.rs

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

33
use pyroscope::{PyroscopeAgent, Result};
4+
use std::hash::{Hash, Hasher};
45

5-
fn fibonacci(n: u64) -> u64 {
6-
match n {
7-
0 | 1 => 1,
8-
n => fibonacci(n - 1) + fibonacci(n - 2),
6+
fn hash_rounds(n: u64) -> u64 {
7+
let hash_str = "Some string to hash";
8+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
9+
10+
for _ in 0..n {
11+
for _ in 0..1000 {
12+
default_hasher.write(hash_str.as_bytes());
13+
}
14+
hash_str.hash(&mut default_hasher);
915
}
16+
17+
n
1018
}
1119

1220
fn main() -> Result<()> {
@@ -18,13 +26,13 @@ fn main() -> Result<()> {
1826
agent.start()?;
1927

2028
// Make some calculation
21-
let _result = fibonacci(47);
29+
let _result = hash_rounds(300_000);
2230

2331
// Add Tags
2432
agent.add_tags(&[("series", "Number 2")])?;
2533

2634
// Do more calculation
27-
let _result = fibonacci(47);
35+
let _result = hash_rounds(500_000);
2836

2937
// Stop Agent
3038
agent.stop()?;

examples/with-logger.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ extern crate pyroscope;
33
use log::info;
44

55
use pyroscope::{PyroscopeAgent, Result};
6+
use std::hash::{Hash, Hasher};
67

7-
fn fibonacci(n: u64) -> u64 {
8-
match n {
9-
0 | 1 => 1,
10-
n => fibonacci(n - 1) + fibonacci(n - 2),
8+
fn hash_rounds(n: u64) -> u64 {
9+
let hash_str = "Some string to hash";
10+
let mut default_hasher = std::collections::hash_map::DefaultHasher::new();
11+
12+
for _ in 0..n {
13+
for _ in 0..1000 {
14+
default_hasher.write(hash_str.as_bytes());
15+
}
16+
hash_str.hash(&mut default_hasher);
1117
}
18+
19+
n
1220
}
1321

1422
fn main() -> Result<()> {
@@ -26,7 +34,7 @@ fn main() -> Result<()> {
2634
// Start Agent
2735
agent.start()?;
2836

29-
let _result = fibonacci(47);
37+
let _result = hash_rounds(300_000);
3038

3139
// Stop Agent
3240
agent.stop()?;

0 commit comments

Comments
 (0)