Skip to content

Commit

Permalink
run bench test with 75% reads 25% writes
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jan 2, 2024
1 parent fefd6a9 commit 84cec43
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,52 @@ func BenchmarkCloudflareGet(b *testing.B) {
for i := 0; i < cachesize/2; i++ {
cache.Set(keymap[i], i, time.Now().Add(time.Hour))
}

b.SetParallelism(parallelism)
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
expires := time.Now().Add(time.Hour)
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v.(int) != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
cache.Get(keymap[i])
} else {
cache.Set(keymap[i], i, expires)
}
}
})
}

func BenchmarkCcacheGet(b *testing.B) {
cache := ccache.New(ccache.Configure[int]().MaxSize(cachesize))
for i := 0; i < cachesize/2; i++ {
cache.Set(keymap[i], i, time.Hour)
}

b.SetParallelism(parallelism)
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v := cache.Get(keymap[i])
if v != nil && v.Value() != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
cache.Get(keymap[i])
} else {
cache.Set(keymap[i], i, time.Hour)
}
}
})
}

func BenchmarkEcacheGet(b *testing.B) {
cache := ecache.NewLRUCache(1024, cachesize/1024, time.Hour)
for i := 0; i < cachesize/2; i++ {
cache.Put(keymap[i], i)
}

b.SetParallelism(parallelism)
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
cache.Get(keymap[i])
} else {
cache.Put(keymap[i], i)
}
}
})
Expand All @@ -97,7 +93,7 @@ func BenchmarkEcacheGet(b *testing.B) {
func BenchmarkRistrettoGet(b *testing.B) {
cache, _ := ristretto.NewCache(&ristretto.Config{
NumCounters: cachesize, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
MaxCost: 2 << 30, // maximum cost of cache (2GB).
BufferItems: 64, // number of keys per Get buffer.
})
for i := 0; i < cachesize/2; i++ {
Expand All @@ -110,9 +106,10 @@ func BenchmarkRistrettoGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
cache.Get(keymap[i])
} else {
cache.SetWithTTL(keymap[i], i, 1, time.Hour)
}
}
})
Expand All @@ -130,9 +127,10 @@ func BenchmarkTheineGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
_, _ = cache.Get(keymap[i])
} else {
_ = cache.SetWithTTL(keymap[i], i, 1, time.Hour)
}
}
})
Expand All @@ -150,9 +148,10 @@ func BenchmarkOtterGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
_, _ = cache.Get(keymap[i])
} else {
cache.SetWithTTL(keymap[i], i, time.Hour)
}
}
})
Expand All @@ -170,9 +169,10 @@ func BenchmarkPhusluGet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := int(fastrandn(cachesize))
v, ok := cache.Get(keymap[i])
if ok && v != i {
b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v)
if i >= cachesize/4 {
_, _ = cache.Get(keymap[i])
} else {
cache.SetWithTTL(keymap[i], i, time.Hour)
}
}
})
Expand Down

0 comments on commit 84cec43

Please sign in to comment.