From b46a319870d3302230a5e388fc2dfd632f856761 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Fri, 26 Sep 2025 18:18:44 +0200 Subject: [PATCH] accounts/keystore: introduce accountCache.add and accountCache.find benchmarks For #16874 --- accounts/keystore/account_cache_test.go | 91 +++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index c9a8cdfcef..5ba7211d18 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -17,6 +17,7 @@ package keystore import ( + "encoding/binary" "errors" "fmt" "math/rand" @@ -405,3 +406,93 @@ func forceCopyFile(dst, src string) error { } return os.WriteFile(dst, data, 0644) } + +func BenchmarkAdd(b *testing.B) { + for _, preload := range []int{10, 100, 1000, 1_000_000} { + b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) { + benchmarkAdd(b, preload) + }) + } +} + +func benchmarkAdd(b *testing.B, preload int) { + dir := filepath.Join("testdata", "dir") + cache, _ := newAccountCache(dir) + cache.watcher.running = true // prevent unexpected reloads + + for i := range preload { + acc := accounts.Account{ + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/preload%08x", i)}, + } + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i)) + + cache.add(acc) + } + + b.ResetTimer() + b.ReportAllocs() + for i := range b.N { + acc := accounts.Account{ + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/bench%08x", i)}, + } + binary.NativeEndian.PutUint64(acc.Address[12:], uint64(i)) + + cache.add(acc) + } +} + +func BenchmarkFind(b *testing.B) { + for _, preload := range []int{10, 100, 1000, 1_000_000} { + b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) { + benchmarkFind(b, preload) + }) + } +} + +func benchmarkFind(b *testing.B, preload int) { + dir := filepath.Join("testdata", "dir") + cache, _ := newAccountCache(dir) + cache.watcher.running = true // prevent unexpected reloads + + for i := range preload { + acc := accounts.Account{ + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", i)}, + } + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i)) + + cache.add(acc) + } + + b.Run("by address", func(b *testing.B) { + acc := accounts.Account{} + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(preload/2)) + + b.ResetTimer() + b.ReportAllocs() + for range b.N { + cache.find(acc) + } + }) + + b.Run("by path", func(b *testing.B) { + acc := accounts.Account{ + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", preload/2)}, + } + + b.ResetTimer() + b.ReportAllocs() + for range b.N { + cache.find(acc) + } + }) + + b.Run("ambiguous", func(b *testing.B) { + acc := accounts.Account{} + + b.ResetTimer() + b.ReportAllocs() + for range b.N { + cache.find(acc) + } + }) +}