Skip to content

Commit

Permalink
refine bench tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Dec 30, 2023
1 parent 485aa83 commit 3a375f4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 98 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

* Simple
- No Dependency
- Less than 1000 Sloc
- Less than 1000 sloc
* Fast
- Faster than all well-known LRU caches
- Zero memory allocs
* GC friendly
- Pointerless data structs
- Continuous memory layout
* Memory efficient
- Save at least 30% memory than other LRU caches
- Uses 24 extra bytes per cache object

### Getting Started

Expand Down Expand Up @@ -44,23 +44,23 @@ func main() {

### Benchmarks

A Performance result as below
A Performance result on keysize=16, cachesize=1000000, parallelism=32
```
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) Silver 4216 CPU @ 2.10GHz
goarch: arm64
pkg: bench
BenchmarkCloudflareGet
BenchmarkCloudflareGet-8 100000000 59.11 ns/op 16 B/op 1 allocs/op
BenchmarkCloudflareGet-4 32207244 191.4 ns/op 16 B/op 1 allocs/op
BenchmarkCcacheGet
BenchmarkCcacheGet-8 105142296 56.85 ns/op 20 B/op 2 allocs/op
BenchmarkCcacheGet-4 32112368 183.4 ns/op 20 B/op 2 allocs/op
BenchmarkRistrettoGet
BenchmarkRistrettoGet-8 131303994 45.99 ns/op 16 B/op 1 allocs/op
BenchmarkGoburrowGet
BenchmarkGoburrowGet-8 100000000 62.94 ns/op 16 B/op 1 allocs/op
BenchmarkRistrettoGet-4 39304964 156.9 ns/op 16 B/op 1 allocs/op
BenchmarkEcacheGet
BenchmarkEcacheGet-4 35331213 166.2 ns/op 0 B/op 0 allocs/op
BenchmarkPhusluGet
BenchmarkPhusluGet-8 176671556 34.11 ns/op 0 B/op 0 allocs/op
BenchmarkPhusluGet-4 45333045 132.9 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 43.895s
ok bench 42.942s
```

[godoc-img]: http://img.shields.io/badge/godoc-reference-blue.svg
Expand Down
41 changes: 36 additions & 5 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package bench

import (
"fmt"
"testing"
"time"
_ "unsafe"
Expand All @@ -14,9 +15,19 @@ import (
)

const (
keysize = 16
cachesize = 1000000
parallelism = 32
)

var keymap = func() (x []string) {
x = make([]string, cachesize)
for i := 0; i < cachesize; i++ {
x[i] = fmt.Sprintf(fmt.Sprintf("%%0%dd", keysize), i)
}
return
}()

//go:noescape
//go:linkname fastrandn runtime.fastrandn
func fastrandn(x uint32) uint32
Expand All @@ -32,7 +43,11 @@ func BenchmarkCloudflareGet(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = cache.Get(keymap[fastrandn(cachesize)])
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)
}
}
})
}
Expand All @@ -48,7 +63,11 @@ func BenchmarkCcacheGet(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = cache.Get(keymap[fastrandn(cachesize)])
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)
}
}
})
}
Expand All @@ -68,7 +87,11 @@ func BenchmarkRistrettoGet(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = cache.Get(keymap[fastrandn(cachesize)])
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)
}
}
})
}
Expand All @@ -84,7 +107,11 @@ func BenchmarkEcacheGet(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = cache.Get(keymap[fastrandn(cachesize)])
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)
}
}
})
}
Expand All @@ -100,7 +127,11 @@ func BenchmarkPhusluGet(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = cache.Get(keymap[fastrandn(cachesize)])
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)
}
}
})
}
81 changes: 0 additions & 81 deletions bench/memory_test.go

This file was deleted.

0 comments on commit 3a375f4

Please sign in to comment.