Created
March 24, 2016 13:40
-
-
Save janisz/53e487224ed9bad0d06a to your computer and use it in GitHub Desktop.
Go Free cache vs BigCache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bigcache | |
import ( | |
"encoding/binary" | |
"testing" | |
"time" | |
"github.com/allegro/bigcache" | |
"github.com/coocood/freecache" | |
) | |
var BigCacheConfig = bigcache.Config{ | |
Shards: 256, // number of shards | |
LifeWindow: 10 * time.Minute, // time after which entry can be evicted | |
MaxEntriesInWindow: 1000 * 10 * 60, // rps * lifeWindow | |
MaxEntrySize: 500, // max entry size in bytes | |
} | |
func BenchmarkFreeCacheSet(b *testing.B) { | |
cache := freecache.NewCache(256 * 1024 * 1024) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Set(key[:], make([]byte, 8), 0) | |
} | |
} | |
func BenchmarkBigCacheSet(b *testing.B) { | |
cache := bigcache.NewBigCache(BigCacheConfig) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Set(string(key[:]), make([]byte, 8)) | |
} | |
} | |
func BenchmarkMapSet(b *testing.B) { | |
m := make(map[string][]byte) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
m[string(key[:])] = make([]byte, 8) | |
} | |
} | |
func BenchmarkFreeCacheGet(b *testing.B) { | |
b.StopTimer() | |
cache := freecache.NewCache(256 * 1024 * 1024) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Set(key[:], make([]byte, 8), 0) | |
} | |
b.StartTimer() | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Get(key[:]) | |
} | |
} | |
func BenchmarkBigCacheGet(b *testing.B) { | |
b.StopTimer() | |
cache := bigcache.NewBigCache(BigCacheConfig) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Set(string(key[:]), make([]byte, 8)) | |
} | |
b.StartTimer() | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
cache.Get(string(key[:])) | |
} | |
} | |
func BenchmarkMapGet(b *testing.B) { | |
b.StopTimer() | |
m := make(map[string][]byte) | |
var key [8]byte | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
m[string(key[:])] = make([]byte, 8) | |
} | |
b.StartTimer() | |
var hitCount int64 | |
for i := 0; i < b.N; i++ { | |
binary.LittleEndian.PutUint64(key[:], uint64(i)) | |
if m[string(key[:])] != nil { | |
hitCount++ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ go version | |
go version go1.6rc2 linux/amd64 | |
~ go test -bench=. -benchmem bench_test.go | |
testing: warning: no tests to run | |
PASS | |
BenchmarkFreeCacheSet-4 3000000 502 ns/op 164 B/op 0 allocs/op | |
BenchmarkBigCacheSet-4 2000000 937 ns/op 280 B/op 4 allocs/op | |
BenchmarkMapSet-4 1000000 1024 ns/op 212 B/op 2 allocs/op | |
BenchmarkFreeCacheGet-4 3000000 560 ns/op 8 B/op 1 allocs/op | |
BenchmarkBigCacheGet-4 3000000 565 ns/op 32 B/op 4 allocs/op | |
BenchmarkMapGet-4 10000000 225 ns/op 0 B/op 0 allocs/op | |
ok command-line-arguments 35.786s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment