From 3140e0b994e4108220afedc3de8eaf42ff216904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 30 Jul 2024 10:06:48 +0200 Subject: [PATCH] identity: Add BenchmarkHashString --- identity/identityhash_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/identity/identityhash_test.go b/identity/identityhash_test.go index 1ecaf761245..52debe293f2 100644 --- a/identity/identityhash_test.go +++ b/identity/identityhash_test.go @@ -14,6 +14,9 @@ package identity import ( + "fmt" + "math" + "strings" "testing" qt "github.com/frankban/quicktest" @@ -42,3 +45,24 @@ func (t tstKeyer) Key() string { func (t tstKeyer) String() string { return "key: " + t.key } + +func BenchmarkHashString(b *testing.B) { + word := " hello " + + var tests []string + + for i := 1; i <= 5; i++ { + sentence := strings.Repeat(word, int(math.Pow(4, float64(i)))) + tests = append(tests, sentence) + } + + b.ResetTimer() + + for _, test := range tests { + b.Run(fmt.Sprintf("n%d", len(test)), func(b *testing.B) { + for i := 0; i < b.N; i++ { + HashString(test) + } + }) + } +}