-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimistic_lock_test.go
93 lines (89 loc) · 1.89 KB
/
optimistic_lock_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
package glock
import (
"fmt"
"log"
"sync"
"testing"
"time"
)
/*
Run test: go test -run TestOptimisticRun
go test -bench BenchmarkOptimistic100t -benchmem
go test -bench BenchmarkOptimistic10000t -benchmem
?*/
func TestOptimisticRun(t *testing.T) {
ol, err := CreateOptimisticLock("localhost:6379", "", "test_", time.Second)
if err != nil {
panic(err)
}
if err := ol.Lock("key1"); err != nil {
log.Print(err)
}
if err := ol.Lock("key1"); err != nil {
log.Print(err)
}
if err := ol.Lock("key1"); err != nil {
log.Print(err)
}
if err := ol.Unlock("key1"); err != nil {
log.Print(err)
}
if err := ol.Lock("key1"); err != nil {
log.Print(err)
}
}
func BenchmarkOptimistic100t(t *testing.B) {
ol, err := CreateOptimisticLock("localhost:6379", "", "test_", time.Second)
if err != nil {
panic(err)
}
for i := 0; i < 100; i++ {
if err := ol.Lock(fmt.Sprintf("key_%v", i)); err != nil {
log.Print(err)
}
if err := ol.Unlock(fmt.Sprintf("key_%v", i)); err != nil {
log.Print(err)
}
}
}
func BenchmarkOptimistic10000Miltiples(t *testing.B) {
ol, err := CreateOptimisticLock("localhost:6379", "", "test_", time.Second)
if err != nil {
panic(err)
}
c := make(chan string, 1000)
wg := &sync.WaitGroup{}
for i := 0; i < 50; i++ {
go func() {
for {
item := <-c
if err := ol.Lock(item); err != nil {
log.Print(err)
}
if err := ol.Unlock(item); err != nil {
log.Print(err)
}
wg.Done()
}
}()
}
for i := 0; i < 10000; i++ {
c <- fmt.Sprintf("key_%v", i)
wg.Add(1)
}
wg.Wait()
}
func BenchmarkOptimistic10000t(t *testing.B) {
ol, err := CreateOptimisticLock("localhost:6379", "", "test_", time.Second)
if err != nil {
panic(err)
}
for i := 0; i < 10000; i++ {
if err := ol.Lock(fmt.Sprintf("key_%v", i)); err != nil {
log.Print(err)
}
if err := ol.Unlock(fmt.Sprintf("key_%v", i)); err != nil {
log.Print(err)
}
}
}