Skip to content

Commit 223d012

Browse files
authored
Merge pull request #3 from bsv-blockchain/feat/add-missing-benchmarks-to-readme
[Test] Add benchmarks for queue functions
2 parents acadc21 + f06411d commit 223d012

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ make bench
306306

307307
| Benchmark | Iterations | ns/op | B/op | allocs/op |
308308
|--------------------------------------------------|------------|------:|-----:|----------:|
309-
| [LockFreeQ](lock_free_queue_benchmark_test.go) | 15076060 | 73.92 | 16 | 1 |
309+
| [LockFreeQ](lock_free_queue_benchmark_test.go) | 24752019 | 79.73 | 16 | 1 |
310+
| [NewLockFreeQ](lock_free_queue_benchmark_test.go) | 1000000000 | 0.29 | 0 | 0 |
311+
| [LockFreeQ_Enqueue](lock_free_queue_benchmark_test.go) | 25041442 | 49.18 | 16 | 1 |
312+
| [LockFreeQ_Dequeue](lock_free_queue_benchmark_test.go) | 338000521 | 11.14 | 0 | 0 |
313+
| [LockFreeQ_IsEmpty](lock_free_queue_benchmark_test.go) | 1000000000 | 0.60 | 0 | 0 |
310314

311315
> These benchmarks reflect fast, allocation-free lookups for most retrieval functions, ensuring optimal performance in production environments.
312316
> Performance benchmarks for the core functions in this library, executed on a 13th Gen Intel i7-1360P (AMD64).

lock_free_queue_benchmark_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
88
func 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

Comments
 (0)