Skip to content

Commit

Permalink
some sample code to show why current benchmark test results are misle…
Browse files Browse the repository at this point in the history
…ading

the rand.Intn use more time than the actual code we want to measure
  • Loading branch information
KingOfBaboon committed Sep 18, 2023
1 parent 08dc779 commit fc24505
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions caches_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,22 @@ func MapGet[T any](cs constructor[T], b *testing.B) {

hitCount := 0
for i := 0; i < b.N; i++ {
id := rand.Intn(maxEntryCount)
if e, ok := m[key(id)]; ok {
if e, ok := m["a"]; ok {
_ = (T)(e)
hitCount++
}
}
}

func MapGetBaseline[T any](b *testing.B) {
hitCount := 0
for i := 0; i < b.N; i++ {
id := rand.Intn(maxEntryCount)
key(id)
hitCount++
}
}

func SyncMapGet[T any](cs constructor[T], b *testing.B) {
b.StopTimer()
var m sync.Map
Expand Down Expand Up @@ -230,8 +238,8 @@ func BigCacheGet[T any](cs constructor[T], b *testing.B) {

hitCount := 0
for i := 0; i < b.N; i++ {
id := rand.Intn(maxEntryCount)
data, _ := cache.Get(key(id))
//id := rand.Intn(maxEntryCount)
data, _ := cache.Get("a")
v, _ := cs.Parse(data)
_ = (T)(v)
hitCount++
Expand Down Expand Up @@ -262,6 +270,10 @@ func BenchmarkMapGetForBytes(b *testing.B) {
MapGet[[]byte](byteConstructor{}, b)
}

func BenchmarkMapGetBaseline(b *testing.B) {
MapGetBaseline[[]byte](b)
}

func BenchmarkSyncMapGetForBytes(b *testing.B) {
SyncMapGet[[]byte](byteConstructor{}, b)
}
Expand Down

0 comments on commit fc24505

Please sign in to comment.