mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-13 11:36:37 +00:00
accounts/keystore: introduce accountCache.add and accountCache.find benchmarks
For #16874
This commit is contained in:
parent
c984d9086e
commit
b46a319870
1 changed files with 91 additions and 0 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
package keystore
|
package keystore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
@ -405,3 +406,93 @@ func forceCopyFile(dst, src string) error {
|
||||||
}
|
}
|
||||||
return os.WriteFile(dst, data, 0644)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue