github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/cache/random_test.go (about) 1 package cache_test 2 3 import ( 4 "encoding/binary" 5 "testing" 6 7 "github.com/egonelbre/exp/cache" 8 "github.com/loov/hrtime" 9 ) 10 11 func BenchmarkRandom_Growing(b *testing.B) { 12 b.ResetTimer() 13 start := hrtime.Now() 14 for k := 0; k < b.N; k++ { 15 c := cache.NewRandom(1e9, nil) 16 for i := 0; i < ItemsToAdd; i++ { 17 var key [8]byte 18 binary.LittleEndian.PutUint64(key[:], uint64(i)) 19 c.Add(cache.Hash(key[:]), 1) 20 } 21 } 22 finish := hrtime.Now() 23 b.StopTimer() 24 b.ReportMetric(float64((finish-start).Nanoseconds())/float64(b.N*ItemsToAdd), "ns/item") 25 } 26 27 func BenchmarkRandom_Prealloc(b *testing.B) { 28 b.ResetTimer() 29 start := hrtime.Now() 30 for k := 0; k < b.N; k++ { 31 c := cache.NewRandomPrealloc(ItemsToAdd, 1e9, nil) 32 for i := 0; i < ItemsToAdd; i++ { 33 var key [8]byte 34 binary.LittleEndian.PutUint64(key[:], uint64(i)) 35 c.Add(cache.Hash(key[:]), 1) 36 } 37 } 38 finish := hrtime.Now() 39 b.StopTimer() 40 b.ReportMetric(float64((finish-start).Nanoseconds())/float64(b.N*ItemsToAdd), "ns/item") 41 }