improve benchmark validity by not measuring startup costs

Signed-off-by: glightfoot <glightfoot@rsglab.com>
This commit is contained in:
glightfoot 2020-07-02 15:25:25 -04:00
parent b162bd047a
commit 8c97daddee
2 changed files with 24 additions and 0 deletions

View file

@ -46,7 +46,14 @@ func benchmarkUDPListener(times int, b *testing.B) {
bytesInput[run*len(input)+i] = fmt.Sprintf("run%d%s", run, input[i]) bytesInput[run*len(input)+i] = fmt.Sprintf("run%d%s", run, input[i])
} }
} }
// reset benchmark timer to not measure startup costs
b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
// pause benchmark timer for creating the chan and listener
b.StopTimer()
// there are more events than input lines, need bigger buffer // there are more events than input lines, need bigger buffer
events := make(chan event.Events, len(bytesInput)*times*2) events := make(chan event.Events, len(bytesInput)*times*2)
@ -59,6 +66,9 @@ func benchmarkUDPListener(times int, b *testing.B) {
TagsReceived: tagsReceived, TagsReceived: tagsReceived,
} }
// resume benchmark timer
b.StartTimer()
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
for _, line := range bytesInput { for _, line := range bytesInput {
l.HandlePacket([]byte(line)) l.HandlePacket([]byte(line))
@ -154,8 +164,19 @@ mappings:
} }
ex := exporter.NewExporter(testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount) ex := exporter.NewExporter(testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount)
// reset benchmark timer to not measure startup costs
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
// pause benchmark timer for creating the chan
b.StopTimer()
ec := make(chan event.Events, 1000) ec := make(chan event.Events, 1000)
// resume benchmark timer
b.StartTimer()
go func() { go func() {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
ec <- events ec <- events

View file

@ -37,6 +37,9 @@ func benchmarkLineToEvents(times int, b *testing.B) {
} }
logger := log.NewNopLogger() logger := log.NewNopLogger()
// reset benchmark timer to not measure startup costs
b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
for _, l := range input { for _, l := range input {