-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
75 lines (65 loc) · 1.47 KB
/
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
package mcache
import (
"fmt"
"testing"
"time"
)
// BenchmarkWrite
func BenchmarkWrite(b *testing.B) {
mcache := NewCache[int]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mcache.Set(fmt.Sprintf("%d", i), i, time.Second)
}
b.StopTimer()
mcache.Cleanup()
}
// BenchmarkRead
func BenchmarkRead(b *testing.B) {
mcache := NewCache[int]()
for i := 0; i < b.N; i++ {
mcache.Set(fmt.Sprintf("%d", i), i, time.Minute)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mcache.Get(fmt.Sprintf("%d", i))
}
b.StopTimer()
mcache.Clear()
}
// BenchmarkRW
func BenchmarkRWD(b *testing.B) {
mcache := NewCache[int]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mcache.Set(fmt.Sprintf("%d", i), i, time.Hour)
mcache.Get(fmt.Sprintf("%d", i))
mcache.Del(fmt.Sprintf("%d", i))
}
b.StopTimer()
mcache.Clear()
}
// global var mutex:
// BenchmarkConcurrentRWD-4 293641 5057 ns/op 437 B/op 13 allocs/op
// struct field mutex:
// BenchmarkConcurrentRWD-4 368404 2837 ns/op 207 B/op 16 allocs/op
func BenchmarkConcurrentRWD(b *testing.B) {
c1 := NewCache[int]()
c2 := NewCache[int]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func(i int) {
c1.Set(fmt.Sprintf("%d", i), i, time.Hour)
c1.Get(fmt.Sprintf("%d", i))
c1.Del(fmt.Sprintf("%d", i))
}(i)
go func(i int) {
c2.Set(fmt.Sprintf("%d", i), i, time.Hour)
c2.Get(fmt.Sprintf("%d", i))
c2.Del(fmt.Sprintf("%d", i))
}(i)
}
b.StopTimer()
c1.Clear()
c2.Clear()
}