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

test(tm2/pkg/cmap): add benchmarks to show true impact of contention #3540

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
57 changes: 57 additions & 0 deletions tm2/pkg/cmap/cmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import (
"fmt"
"runtime"
"strings"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,6 +58,61 @@
assert.Nil(t, cmap.Get("key2"))
}

var sink any = nil

func BenchmarkCMapConcurrentInsertsDeletesHas(b *testing.B) {
cm := NewCMap()
keys := make([]string, 100000)
for i := range keys {
keys[i] = fmt.Sprintf("key%d", i)
}
b.ResetTimer()

for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
semaCh := make(chan bool)
nCPU := runtime.NumCPU()
for j := 0; j < nCPU; j++ {
wg.Add(1)
go func() {
defer wg.Done()

// Make sure that all the goroutines run at the
// exact same time for true concurrent tests.
<-semaCh

for i, key := range keys {
if (j+i)%2 == 0 {
cm.Has(key)
} else {
cm.Set(key, j)
}
_ = cm.Size()
if (i+1)%3 == 0 {
cm.Delete(key)
}

if (i+1)%327 == 0 {
cm.Clear()
}
_ = cm.Size()
_ = cm.Keys()
}
_ = cm.Values()
}()
}
close(semaCh)
wg.Wait()

sink = wg

Check failure on line 107 in tm2/pkg/cmap/cmap_test.go

View workflow job for this annotation

GitHub Actions / Run TM2 suite / Go Lint / lint

copylocks: assignment copies lock value to sink: sync.WaitGroup contains sync.noCopy (govet)
}

if sink == nil {
b.Fatal("Benchmark did not run!")
}
sink = nil
}

func BenchmarkCMapHas(b *testing.B) {
m := NewCMap()
for i := 0; i < 1000; i++ {
Expand Down
Loading