2020-03-14 12:11:46 +00:00
|
|
|
package keylock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkMutex_Baseline(b *testing.B) {
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
mu.Lock()
|
2020-03-14 12:14:35 +00:00
|
|
|
_ = 0
|
2020-03-14 12:11:46 +00:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkKeyLock_SingleKey(b *testing.B) {
|
|
|
|
l := New()
|
|
|
|
|
|
|
|
keys := []string{"a"}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
_, unlock := l.LockKeys(keys, nil)
|
|
|
|
unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkKeyLock_MultipleKeys(b *testing.B) {
|
|
|
|
l := New()
|
|
|
|
|
|
|
|
keys := []string{"a", "b", "c", "d"}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
_, unlock := l.LockKeys(keys, nil)
|
|
|
|
unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkKeyLock_DifferentKeys(b *testing.B) {
|
|
|
|
l := New()
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
keys := []string{strconv.Itoa(rand.Int())}
|
|
|
|
|
|
|
|
for pb.Next() {
|
|
|
|
_, unlock := l.LockKeys(keys, nil)
|
|
|
|
unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-16 13:26:26 +00:00
|
|
|
|
|
|
|
func BenchmarkKeyLock_NoBusyWait(b *testing.B) {
|
|
|
|
l := New()
|
|
|
|
|
|
|
|
lockedKey := []string{"locked"}
|
|
|
|
l.LockKeys(lockedKey, nil)
|
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
defer close(cancel)
|
2022-03-18 20:25:42 +00:00
|
|
|
for i := 0; i < 1000; i++ {
|
2020-03-16 13:26:26 +00:00
|
|
|
go func() {
|
|
|
|
l.LockKeys(lockedKey, cancel)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
openKey := []string{"a"}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
canceled, unlock := l.LockKeys(openKey, nil)
|
|
|
|
if canceled {
|
|
|
|
b.Fatal("spurious lock fail")
|
|
|
|
}
|
|
|
|
unlock()
|
|
|
|
}
|
|
|
|
}
|