-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsortedmap_bench_test.go
100 lines (86 loc) · 1.8 KB
/
sortedmap_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2018-2019 Burak Sezer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sorted
import (
"fmt"
"math/rand"
"testing"
"time"
)
func BenchmarkSortedMapSet(b *testing.B) {
m := NewSortedMap(0)
defer m.Close()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := m.Set(bkey(n), bval(n))
if err != nil {
b.Fatalf("Expected nil. Got %v", err)
}
}
}
func BenchmarkSortedMapGet(b *testing.B) {
m := NewSortedMap(0)
defer m.Close()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := m.Set(bkey(n), bval(n))
if err != nil {
b.Fatalf("Expected nil. Got %v", err)
}
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := m.Get(bkey(n))
if err != nil {
b.Fatalf("Expected nil. Got %v", err)
}
}
}
func parallelKey(threadID int, counter int) string {
return fmt.Sprintf("%04d-%09d", threadID, counter)
}
func BenchmarkSortedMapSetParallel(b *testing.B) {
m := NewSortedMap(0)
defer m.Close()
rand.Seed(time.Now().Unix())
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
id := rand.Intn(1000000)
counter := 0
for pb.Next() {
key := []byte(parallelKey(id, counter))
err := m.Set(key, key)
if err != nil {
b.Errorf("Expected nil. Got %s", err)
}
counter = counter + 1
}
})
}
func BenchmarkSortedMapGetParallel(b *testing.B) {
m := NewSortedMap(0)
defer m.Close()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := m.Set(bkey(n), bval(n))
if err != nil {
b.Fatalf("Expected nil. Got %v", err)
}
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
n := 0
for pb.Next() {
_, err := m.Get(bkey(n))
if err != nil {
b.Errorf("Expected nil. Got %s", err)
}
n++
}
})
}