Skip to content

Commit

Permalink
Benchmark comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Mar 14, 2023
1 parent af5a39a commit d81ad67
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions metrics/engine/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"testing"

"github.com/clarkduvall/hyperloglog"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -221,3 +222,43 @@ func newTestPreInitState(tb testing.TB) *lib.TestPreInitState {
BuiltinMetrics: metrics.RegisterBuiltinMetrics(reg),
}
}

func cardinalityMap() {
m := make(map[uint64]struct{})
for i := 1; i <= 100000; i++ {
m[uint64(i)] = struct{}{}
}
}

func cardinalityHLL() {
hll, _ := hyperloglog.NewPlus(10)
for i := 1; i <= 100000; i++ {
hll.Add(hyperWrap(i))
}
}

type hyperWrap int

func (hw hyperWrap) Sum64() uint64 {
return uint64(hw)
}

// goos: linux
// goarch: amd64
// pkg: go.k6.io/k6/metrics/engine
// cpu: AMD Ryzen 7 4800H with Radeon Graphics
// BenchmarkDiffMap-16 72 14654822 ns/op 3274790 B/op 3928 allocs/op
// BenchmarkDiffHLL-16 93 11855822 ns/op 800565 B/op 99758 allocs/op
// PASS
// ok go.k6.io/k6/metrics/engine 2.204s
func BenchmarkDiffMap(b *testing.B) {
for i := 0; i < b.N; i++ {
cardinalityMap()
}
}

func BenchmarkDiffHLL(b *testing.B) {
for i := 0; i < b.N; i++ {
cardinalityHLL()
}
}

0 comments on commit d81ad67

Please sign in to comment.