-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskiplist_bench_test.go
82 lines (73 loc) · 1.8 KB
/
skiplist_bench_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package tomtp
import (
"fmt"
"math/rand"
"testing"
"time"
)
// BenchmarkSortedHashMap runs benchmarks for basic operations
func BenchmarkSortedHashMap(b *testing.B) {
b.Run("put", func(b *testing.B) {
shm := newSortedHashMap[int, int](func(a, b int) bool { return a < b })
b.ResetTimer()
for i := 0; i < b.N; i++ {
shm.Put(i, i)
}
})
b.Run("get", func(b *testing.B) {
shm := newSortedHashMap[int, int](func(a, b int) bool { return a < b })
for i := 0; i < 1000; i++ {
shm.Put(i, i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
shm.Get(i % 1000)
}
})
b.Run("remove", func(b *testing.B) {
shm := newSortedHashMap[int, int](func(a, b int) bool { return a < b })
for i := 0; i < 1000; i++ {
shm.Put(i, i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
shm.Remove(i % 1000)
}
})
}
// BenchmarkFuzzSortedHashMap runs benchmarks with random operations
func BenchmarkFuzzSortedHashMap(b *testing.B) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < b.N; i++ {
seed := rng.Int63()
// Create a new RNG for each iteration to ensure deterministic behavior
iterRng := rand.New(rand.NewSource(seed))
numOps := uint(100)
shm := newSortedHashMap[int, string](func(a, b int) bool { return a < b })
expected := make(map[int]string)
// Perform random operations
for j := uint(0); j < numOps; j++ {
op := operationShm(iterRng.Intn(6))
key := iterRng.Intn(1000)
switch op {
case opShmPut:
value := fmt.Sprintf("value-%d", key)
shm.Put(key, value)
expected[key] = value
case opShmGet:
shm.Get(key)
case opShmRemove:
shm.Remove(key)
delete(expected, key)
case opShmMin:
shm.Min()
case opShmMax:
shm.Max()
case opShmNext:
if pair := shm.Get(key); pair != nil {
pair.Next()
}
}
}
}
}