|
| 1 | +extern crate pyroscope; |
| 2 | + |
| 3 | +use pyroscope::{PyroscopeAgent, Result}; |
| 4 | +use pyroscope_pprofrs::{pprof_backend, PprofConfig}; |
| 5 | +use std::hash::{Hash, Hasher}; |
| 6 | + |
| 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); |
| 16 | + } |
| 17 | + |
| 18 | + n |
| 19 | +} |
| 20 | + |
| 21 | +fn main() -> Result<()> { |
| 22 | + // TODO: Change this token to your own. |
| 23 | + let token = String::from("<your-token>"); |
| 24 | + |
| 25 | + let agent = PyroscopeAgent::builder("http://localhost:4040", "example.basic") |
| 26 | + .backend(pprof_backend(PprofConfig::new().sample_rate(100))) |
| 27 | + .auth_token(token) |
| 28 | + .tags([("TagA", "ValueA"), ("TagB", "ValueB")].to_vec()) |
| 29 | + .build()?; |
| 30 | + |
| 31 | + // Show start time |
| 32 | + let start = std::time::SystemTime::now() |
| 33 | + .duration_since(std::time::UNIX_EPOCH) |
| 34 | + .unwrap() |
| 35 | + .as_secs(); |
| 36 | + println!("Start Time: {}", start); |
| 37 | + |
| 38 | + // Start Agent |
| 39 | + let agent_running = agent.start()?; |
| 40 | + |
| 41 | + let _result = hash_rounds(300_000); |
| 42 | + |
| 43 | + // Show stop time |
| 44 | + let stop = std::time::SystemTime::now() |
| 45 | + .duration_since(std::time::UNIX_EPOCH) |
| 46 | + .unwrap() |
| 47 | + .as_secs(); |
| 48 | + println!("Stop Time: {}", stop); |
| 49 | + |
| 50 | + // Stop Agent |
| 51 | + let agent_ready = agent_running.stop()?; |
| 52 | + |
| 53 | + // Shutdown the Agent |
| 54 | + agent_ready.shutdown(); |
| 55 | + |
| 56 | + // Show program exit time |
| 57 | + let exit = std::time::SystemTime::now() |
| 58 | + .duration_since(std::time::UNIX_EPOCH) |
| 59 | + .unwrap() |
| 60 | + .as_secs(); |
| 61 | + println!("Exit Time: {}", exit); |
| 62 | + |
| 63 | + Ok(()) |
| 64 | +} |
0 commit comments