Replace sharded hash function/misc mods
parent
8a2f4f19ef
commit
03284ca422
@ -0,0 +1,68 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGet(b *testing.B) {
|
||||
b.StopTimer()
|
||||
tc := unexportedNewSharded(DefaultExpiration, 0, 10)
|
||||
tc.Set("foobarba", "zquux", DefaultExpiration)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tc.Get("foobarba")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkShardedCacheGetManyConcurrent(b *testing.B) {
|
||||
b.StopTimer()
|
||||
n := 10000
|
||||
tsc := unexportedNewSharded(DefaultExpiration, 0, 20)
|
||||
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()
|
||||
}
|
Loading…
Reference in New Issue