Skip to content

Commit

Permalink
add concurrent benchmark for recording the status code
Browse files Browse the repository at this point in the history
  • Loading branch information
sbunce committed Nov 9, 2020
1 parent a71cfa1 commit b494329
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions memmetrics/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memmetrics

import (
"runtime"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -30,6 +31,42 @@ func BenchmarkRecord(b *testing.B) {
}
}

func BenchmarkRecordConcurrently(b *testing.B) {
b.ReportAllocs()

rr, err := NewRTMetrics(RTClock(testutils.GetClock()))
require.NoError(b, err)

// warm up metrics. Adding a new code can do allocations, but in the steady
// state recording a code is cheap. We want to measure the steady state.
const codes = 100
for code := 0; code < codes; code++ {
rr.Record(code, time.Second)
}

concurrency := runtime.NumCPU()
b.Logf("NumCPU: %d, Concurrency: %d, GOMAXPROCS: %d",
runtime.NumCPU(), concurrency, runtime.GOMAXPROCS(0))
wg := sync.WaitGroup{}
wg.Add(concurrency)
perG := b.N/concurrency
if perG == 0 {
perG = 1
}

b.ResetTimer()
for i := 0; i < concurrency; i++ {
go func() {
for j := 0; j < perG; j++ {
rr.Record(j%codes, time.Second)
}
wg.Done()
}()
}

wg.Wait()
}

func TestDefaults(t *testing.T) {
rr, err := NewRTMetrics(RTClock(testutils.GetClock()))
require.NoError(t, err)
Expand Down

0 comments on commit b494329

Please sign in to comment.