@@ -4,17 +4,52 @@ import (
44 "testing"
55)
66
7- // BenchmarkLockFreeQueue benchmarks the lock-free queue's enqueue and dequeue operations
7+ // BenchmarkLockFreeQueue benchmarks enqueueing and dequeuing values.
88func BenchmarkLockFreeQueue (b * testing.B ) {
99 q := NewLockFreeQ [int ]()
10-
1110 for i := 0 ; i < b .N ; i ++ {
1211 q .Enqueue (i )
1312 }
14-
1513 for i := 0 ; i < b .N ; i ++ {
1614 if val := q .Dequeue (); val == nil {
1715 b .Errorf ("Expected a value, got nil at iteration %d" , i )
1816 }
1917 }
2018}
19+
20+ // BenchmarkNewLockFreeQ measures the cost of creating new queue instances.
21+ func BenchmarkNewLockFreeQ (b * testing.B ) {
22+ for i := 0 ; i < b .N ; i ++ {
23+ _ = NewLockFreeQ [int ]()
24+ }
25+ }
26+
27+ // BenchmarkLockFreeQ_Enqueue benchmarks adding items to the queue.
28+ func BenchmarkLockFreeQ_Enqueue (b * testing.B ) {
29+ q := NewLockFreeQ [int ]()
30+ for i := 0 ; i < b .N ; i ++ {
31+ q .Enqueue (i )
32+ }
33+ }
34+
35+ // BenchmarkLockFreeQ_Dequeue benchmarks removing items from the queue.
36+ func BenchmarkLockFreeQ_Dequeue (b * testing.B ) {
37+ q := NewLockFreeQ [int ]()
38+ for i := 0 ; i < b .N ; i ++ {
39+ q .Enqueue (i )
40+ }
41+ b .ResetTimer ()
42+ for i := 0 ; i < b .N ; i ++ {
43+ if q .Dequeue () == nil {
44+ b .Errorf ("expected a value at iteration %d" , i )
45+ }
46+ }
47+ }
48+
49+ // BenchmarkLockFreeQ_IsEmpty benchmarks the IsEmpty check.
50+ func BenchmarkLockFreeQ_IsEmpty (b * testing.B ) {
51+ q := NewLockFreeQ [int ]()
52+ for i := 0 ; i < b .N ; i ++ {
53+ _ = q .IsEmpty ()
54+ }
55+ }
0 commit comments