2014-12-22 07:37:59 -08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2015-11-30 13:04:49 -08:00
|
|
|
"time"
|
2014-12-22 07:37:59 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// func TestDjb33(t *testing.T) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
var shardedKeys = []string{
|
|
|
|
"f",
|
|
|
|
"fo",
|
|
|
|
"foo",
|
|
|
|
"barf",
|
|
|
|
"barfo",
|
|
|
|
"foobar",
|
|
|
|
"bazbarf",
|
|
|
|
"bazbarfo",
|
|
|
|
"bazbarfoo",
|
|
|
|
"foobarbazq",
|
|
|
|
"foobarbazqu",
|
|
|
|
"foobarbazquu",
|
|
|
|
"foobarbazquux",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShardedCache(t *testing.T) {
|
|
|
|
tc := unexportedNewSharded(DefaultExpiration, 0, 13)
|
|
|
|
for _, v := range shardedKeys {
|
|
|
|
tc.Set(v, "value", DefaultExpiration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:04:49 -08:00
|
|
|
func BenchmarkShardedCacheGetExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkShardedCacheGet(b, 5*time.Minute)
|
2015-11-30 13:04:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkShardedCacheGetNotExpiring(b *testing.B) {
|
|
|
|
benchmarkShardedCacheGet(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) {
|
2014-12-22 07:37:59 -08:00
|
|
|
b.StopTimer()
|
2015-11-30 13:04:49 -08:00
|
|
|
tc := unexportedNewSharded(exp, 0, 10)
|
2014-12-22 07:37:59 -08:00
|
|
|
tc.Set("foobarba", "zquux", DefaultExpiration)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
tc.Get("foobarba")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:04:49 -08:00
|
|
|
func BenchmarkShardedCacheGetManyConcurrentExpiring(b *testing.B) {
|
2015-11-30 13:04:57 -08:00
|
|
|
benchmarkShardedCacheGetManyConcurrent(b, 5*time.Minute)
|
2015-11-30 13:04:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) {
|
|
|
|
benchmarkShardedCacheGetManyConcurrent(b, NoExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
|
2014-12-22 07:37:59 -08:00
|
|
|
b.StopTimer()
|
|
|
|
n := 10000
|
2015-11-30 13:04:49 -08:00
|
|
|
tsc := unexportedNewSharded(exp, 0, 20)
|
2014-12-22 07:37:59 -08:00
|
|
|
keys := make([]string, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
k := "foo" + strconv.Itoa(n)
|
|
|
|
keys[i] = k
|
|
|
|
tsc.Set(k, "bar", DefaultExpiration)
|
|
|
|
}
|
|
|
|
each := b.N / n
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
wg.Add(n)
|
|
|
|
for _, v := range keys {
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < each; j++ {
|
|
|
|
tsc.Get(v)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
wg.Wait()
|
|
|
|
}
|