-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_free_queue_benchmark_test.go
More file actions
58 lines (49 loc) · 1.21 KB
/
lock_free_queue_benchmark_test.go
File metadata and controls
58 lines (49 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package lockfreequeue
import (
"testing"
)
// BenchmarkLockFreeQueue benchmarks enqueueing and dequeuing values.
func BenchmarkLockFreeQueue(b *testing.B) {
q := NewLockFreeQ[int]()
for i := 0; i < b.N; i++ {
q.Enqueue(i)
}
for i := 0; i < b.N; i++ {
if val := q.Dequeue(); val == nil {
b.Errorf("Expected a value, got nil at iteration %d", i)
}
}
}
// BenchmarkNewLockFreeQ measures the cost of creating new queue instances.
func BenchmarkNewLockFreeQ(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLockFreeQ[int]()
}
}
// BenchmarkLockFreeQEnqueue benchmarks adding items to the queue.
func BenchmarkLockFreeQEnqueue(b *testing.B) {
q := NewLockFreeQ[int]()
for i := 0; i < b.N; i++ {
q.Enqueue(i)
}
}
// BenchmarkLockFreeQDequeue benchmarks removing items from the queue.
func BenchmarkLockFreeQDequeue(b *testing.B) {
q := NewLockFreeQ[int]()
for i := 0; i < b.N; i++ {
q.Enqueue(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if q.Dequeue() == nil {
b.Errorf("expected a value at iteration %d", i)
}
}
}
// BenchmarkLockFreeQIsEmpty benchmarks the IsEmpty check.
func BenchmarkLockFreeQIsEmpty(b *testing.B) {
q := NewLockFreeQ[int]()
for i := 0; i < b.N; i++ {
_ = q.IsEmpty()
}
}