1use snowflake_gen::{BitLayout, init, next_id};
2use std::sync::Arc;
3use std::sync::atomic::{AtomicU64, Ordering};
4use std::thread;
5use std::time::Instant;
6
7fn main() {
8 let layout = BitLayout::new(41, 5, 5, 12).unwrap();
10 init(1, layout).expect("could not initialize global snowflake");
11
12 println!("Starting benchmark with 32 threads...");
13 let thread_count = 32;
14 let iterations_per_thread = 1_000_000;
15
16 let counter = Arc::new(AtomicU64::new(0));
17 let start = Instant::now();
18
19 let handles: Vec<_> = (0..thread_count)
20 .map(|_| {
21 let cnt = Arc::clone(&counter);
22 thread::spawn(move || {
23 let mut local_count = 0;
24 for _ in 0..iterations_per_thread {
25 if let Ok(_) = next_id() {
26 local_count += 1;
27 }
28 }
29 cnt.fetch_add(local_count, Ordering::Relaxed);
30 })
31 })
32 .collect();
33
34 for h in handles {
35 h.join().unwrap();
36 }
37
38 let duration = start.elapsed();
39 let total_ids = counter.load(Ordering::Relaxed);
40 let ids_per_sec = total_ids as f64 / duration.as_secs_f64();
41
42 println!("Generated {} IDs in {:?}", total_ids, duration);
43 println!(
44 "Throughput: {:.2} million IDs / sec",
45 ids_per_sec / 1_000_000.0
46 );
47}