11//! Benchmark to measure global mutex contention in UUID generation
2- //!
2+ //!
33//! This benchmark demonstrates the performance impact of the global mutex
44//! in uuid.zig by comparing single-threaded vs multi-threaded generation.
55
@@ -11,17 +11,17 @@ const num_threads = 8;
1111
1212fn singleThreadedBenchmark () ! void {
1313 var gen = uuid .initSecure ();
14-
14+
1515 const start = std .time .nanoTimestamp ();
16-
16+
1717 for (0.. num_iterations ) | _ | {
1818 _ = try gen .next ();
1919 }
20-
20+
2121 const end = std .time .nanoTimestamp ();
2222 const elapsed_ms = @divFloor (end - start , std .time .ns_per_ms );
2323 const ops_per_sec = (num_iterations * std .time .ns_per_s ) / @as (u64 , @intCast (end - start ));
24-
24+
2525 std .debug .print ("Single-threaded: {} UUIDs in {}ms ({} ops/sec)\n " , .{
2626 num_iterations ,
2727 elapsed_ms ,
@@ -37,27 +37,27 @@ const ThreadContext = struct {
3737
3838fn workerThread (ctx : * ThreadContext ) ! void {
3939 var gen = uuid .initSecure ();
40-
40+
4141 const start = std .time .nanoTimestamp ();
42-
42+
4343 for (0.. ctx .count ) | _ | {
4444 _ = try gen .next ();
4545 }
46-
46+
4747 const end = std .time .nanoTimestamp ();
4848 ctx .duration_ns = end - start ;
4949}
5050
5151fn multiThreadedBenchmark () ! void {
5252 const allocator = std .heap .page_allocator ;
53-
53+
5454 var threads : [num_threads ]std.Thread = undefined ;
5555 var contexts : [num_threads ]ThreadContext = undefined ;
56-
56+
5757 const per_thread = num_iterations / num_threads ;
58-
58+
5959 const total_start = std .time .nanoTimestamp ();
60-
60+
6161 // Spawn threads
6262 for (0.. num_threads ) | i | {
6363 contexts [i ] = .{
@@ -67,24 +67,24 @@ fn multiThreadedBenchmark() !void {
6767 };
6868 threads [i ] = try std .Thread .spawn (.{}, workerThread , .{& contexts [i ]});
6969 }
70-
70+
7171 // Wait for all threads
7272 for (0.. num_threads ) | i | {
7373 threads [i ].join ();
7474 }
75-
75+
7676 const total_end = std .time .nanoTimestamp ();
7777 const total_elapsed = total_end - total_start ;
7878 const total_elapsed_ms = @divFloor (total_elapsed , std .time .ns_per_ms );
7979 const total_ops_per_sec = (num_iterations * std .time .ns_per_s ) / @as (u64 , @intCast (total_elapsed ));
80-
80+
8181 std .debug .print ("\n Multi-threaded ({} threads): {} UUIDs in {}ms ({} ops/sec)\n " , .{
8282 num_threads ,
8383 num_iterations ,
8484 total_elapsed_ms ,
8585 total_ops_per_sec ,
8686 });
87-
87+
8888 // Show per-thread stats
8989 std .debug .print ("Per-thread breakdown:\n " , .{});
9090 for (contexts , 0.. ) | ctx , i | {
@@ -96,17 +96,17 @@ fn multiThreadedBenchmark() !void {
9696 thread_ops_per_sec ,
9797 });
9898 }
99-
99+
100100 _ = allocator ;
101101}
102102
103103pub fn main () ! void {
104104 std .debug .print ("=== UUID Generation Mutex Contention Benchmark ===\n\n " , .{});
105105 std .debug .print ("Generating {} UUIDs...\n\n " , .{num_iterations });
106-
106+
107107 try singleThreadedBenchmark ();
108108 try multiThreadedBenchmark ();
109-
109+
110110 std .debug .print ("\n === Analysis ===\n " , .{});
111111 std .debug .print ("If multi-threaded performance is significantly worse than\n " , .{});
112112 std .debug .print ("single-threaded * num_threads, the global mutex is causing contention.\n " , .{});
0 commit comments