package main import ( "fmt" "math/rand" "sync" "time" "git.crumpington.com/public/toolbox/kvmemcache" ) const ( mean = 1000000 stdDev = 100 computeTime = 2 * time.Millisecond cacheSize = 800 cacheTTL = time.Second numThreads = 8 numCalls = 1024 * 1024 ) func randKey() string { fSample := rand.NormFloat64()*stdDev + mean sample := int64(fSample) return fmt.Sprintf("%d", sample) } func run(c *kvmemcache.Cache, wg *sync.WaitGroup) { defer wg.Done() for i := 0; i < numCalls; i++ { key := randKey() _, _ = c.Get(key) } } func main() { c := kvmemcache.New(kvmemcache.Config{ MaxSize: cacheSize, TTL: cacheTTL, Src: func(key string) (interface{}, error) { time.Sleep(computeTime) return key, nil }, }) wg := &sync.WaitGroup{} wg.Add(numThreads) t0 := time.Now() for i := 0; i < numThreads; i++ { go run(c, wg) } wg.Wait() dt := time.Since(t0) dtSec := float64(dt) / float64(time.Second) stats := c.Stats() fmt.Printf(` Time taken: %12.3f sec Hits: %12d Misses: %12d Calls/sec: %12.3f Speedup: %12.3f `, dtSec, stats.Hits, stats.Misses, float64(stats.Hits+stats.Misses)/dtSec, float64(numThreads*numCalls*computeTime)/float64(dt), ) }