Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missleading benchmark results #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions caches_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const maxEntryCount = 10000

type myStruct struct {
Id int `json:"id"`

Check failure on line 20 in caches_bench_test.go

View workflow job for this annotation

GitHub Actions / Build (stable)

struct field Id should be ID

Check failure on line 20 in caches_bench_test.go

View workflow job for this annotation

GitHub Actions / Build (oldstable)

struct field Id should be ID
}

type constructor[T any] interface {
Expand Down Expand Up @@ -154,14 +154,22 @@

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 @@

hitCount := 0
for i := 0; i < b.N; i++ {
id := rand.Intn(maxEntryCount)
data, _ := cache.Get(key(id))
//id := rand.Intn(maxEntryCount)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about generating ids upfront ant then just reading them as ids[i]? That should be much faster

data, _ := cache.Get("a")
v, _ := cs.Parse(data)
_ = (T)(v)
hitCount++
Expand Down Expand Up @@ -262,6 +270,10 @@
MapGet[[]byte](byteConstructor{}, b)
}

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

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