Merge pull request #35063 from LK4D4/locker_benchmarks

pkg/locker: add benchmarks
This commit is contained in:
Vincent Demeester 2017-10-03 09:45:56 +02:00 committed by GitHub
commit a8e7aca3fd
1 changed files with 37 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package locker
import (
"math/rand"
"strconv"
"sync"
"testing"
"time"
@ -122,3 +124,38 @@ func TestLockerConcurrency(t *testing.T) {
t.Fatalf("lock should not exist: %v", ctr)
}
}
func BenchmarkLocker(b *testing.B) {
l := New()
for i := 0; i < b.N; i++ {
l.Lock("test")
l.Unlock("test")
}
}
func BenchmarkLockerParallel(b *testing.B) {
l := New()
b.SetParallelism(128)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l.Lock("test")
l.Unlock("test")
}
})
}
func BenchmarkLockerMoreKeys(b *testing.B) {
l := New()
var keys []string
for i := 0; i < 64; i++ {
keys = append(keys, strconv.Itoa(i))
}
b.SetParallelism(128)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := keys[rand.Intn(len(keys))]
l.Lock(k)
l.Unlock(k)
}
})
}