|
17 | 17 | package keystore
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "encoding/binary" |
20 | 21 | "errors"
|
21 | 22 | "fmt"
|
22 | 23 | "math/rand"
|
@@ -405,3 +406,93 @@ func forceCopyFile(dst, src string) error {
|
405 | 406 | }
|
406 | 407 | return os.WriteFile(dst, data, 0644)
|
407 | 408 | }
|
| 409 | + |
| 410 | +func BenchmarkAdd(b *testing.B) { |
| 411 | + for _, preload := range []int{10, 100, 1000, 1_000_000} { |
| 412 | + b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) { |
| 413 | + benchmarkAdd(b, preload) |
| 414 | + }) |
| 415 | + } |
| 416 | +} |
| 417 | + |
| 418 | +func benchmarkAdd(b *testing.B, preload int) { |
| 419 | + dir := filepath.Join("testdata", "dir") |
| 420 | + cache, _ := newAccountCache(dir) |
| 421 | + cache.watcher.running = true // prevent unexpected reloads |
| 422 | + |
| 423 | + for i := range preload { |
| 424 | + acc := accounts.Account{ |
| 425 | + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/preload%08x", i)}, |
| 426 | + } |
| 427 | + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i)) |
| 428 | + |
| 429 | + cache.add(acc) |
| 430 | + } |
| 431 | + |
| 432 | + b.ResetTimer() |
| 433 | + b.ReportAllocs() |
| 434 | + for i := range b.N { |
| 435 | + acc := accounts.Account{ |
| 436 | + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/bench%08x", i)}, |
| 437 | + } |
| 438 | + binary.NativeEndian.PutUint64(acc.Address[12:], uint64(i)) |
| 439 | + |
| 440 | + cache.add(acc) |
| 441 | + } |
| 442 | +} |
| 443 | + |
| 444 | +func BenchmarkFind(b *testing.B) { |
| 445 | + for _, preload := range []int{10, 100, 1000, 1_000_000} { |
| 446 | + b.Run(fmt.Sprintf("preload=%d", preload), func(b *testing.B) { |
| 447 | + benchmarkFind(b, preload) |
| 448 | + }) |
| 449 | + } |
| 450 | +} |
| 451 | + |
| 452 | +func benchmarkFind(b *testing.B, preload int) { |
| 453 | + dir := filepath.Join("testdata", "dir") |
| 454 | + cache, _ := newAccountCache(dir) |
| 455 | + cache.watcher.running = true // prevent unexpected reloads |
| 456 | + |
| 457 | + for i := range preload { |
| 458 | + acc := accounts.Account{ |
| 459 | + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", i)}, |
| 460 | + } |
| 461 | + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(i)) |
| 462 | + |
| 463 | + cache.add(acc) |
| 464 | + } |
| 465 | + |
| 466 | + b.Run("by address", func(b *testing.B) { |
| 467 | + acc := accounts.Account{} |
| 468 | + binary.NativeEndian.PutUint64(acc.Address[0:], uint64(preload/2)) |
| 469 | + |
| 470 | + b.ResetTimer() |
| 471 | + b.ReportAllocs() |
| 472 | + for range b.N { |
| 473 | + cache.find(acc) |
| 474 | + } |
| 475 | + }) |
| 476 | + |
| 477 | + b.Run("by path", func(b *testing.B) { |
| 478 | + acc := accounts.Account{ |
| 479 | + URL: accounts.URL{Scheme: KeyStoreScheme, Path: fmt.Sprintf("dir/account%08x", preload/2)}, |
| 480 | + } |
| 481 | + |
| 482 | + b.ResetTimer() |
| 483 | + b.ReportAllocs() |
| 484 | + for range b.N { |
| 485 | + cache.find(acc) |
| 486 | + } |
| 487 | + }) |
| 488 | + |
| 489 | + b.Run("ambiguous", func(b *testing.B) { |
| 490 | + acc := accounts.Account{} |
| 491 | + |
| 492 | + b.ResetTimer() |
| 493 | + b.ReportAllocs() |
| 494 | + for range b.N { |
| 495 | + cache.find(acc) |
| 496 | + } |
| 497 | + }) |
| 498 | +} |
0 commit comments