From 87f6767ea04e5d74787b9c6ef348040cb4efb441 Mon Sep 17 00:00:00 2001 From: Gabor Boros Date: Thu, 7 Oct 2021 09:44:45 +0200 Subject: [PATCH] test(benchmark): add benchmarks for NewWorklog --- internal/pkg/worklog/worklog_test.go | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/internal/pkg/worklog/worklog_test.go b/internal/pkg/worklog/worklog_test.go index 82cb6c2..94138ce 100644 --- a/internal/pkg/worklog/worklog_test.go +++ b/internal/pkg/worklog/worklog_test.go @@ -2,11 +2,53 @@ package worklog_test import ( "testing" + "time" "github.com/gabor-boros/minutes/internal/pkg/worklog" "github.com/stretchr/testify/assert" ) +var worklogBenchResult worklog.Worklog + +func benchmarkNewWorklog(b *testing.B, entryCount int) { + b.StopTimer() + + var entries []worklog.Entry + + for i := 0; i != entryCount; i++ { + entry := getTestEntry() + entry.Start.Add(time.Hour * time.Duration(i)) + entries = append(entries, entry) + } + + b.StartTimer() + + var result worklog.Worklog + for n := 0; n != b.N; n++ { + result = worklog.NewWorklog(entries) + } + + // always store the result to a package level variable + // so the compiler cannot eliminate the Benchmark itself. + worklogBenchResult = result +} + +func BenchmarkNewWorklog_5(b *testing.B) { + benchmarkNewWorklog(b, 5) +} + +func BenchmarkNewWorklog_10(b *testing.B) { + benchmarkNewWorklog(b, 10) +} + +func BenchmarkNewWorklog_50(b *testing.B) { + benchmarkNewWorklog(b, 50) +} + +func BenchmarkNewWorklog_100(b *testing.B) { + benchmarkNewWorklog(b, 100) +} + func TestWorklogCompleteEntries(t *testing.T) { completeEntry := getTestEntry()