github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/accounts/benchmark_cachedb_test.go (about)

     1  package accounts
     2  
     3  import (
     4  	"path/filepath"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  // These are commented because they take a pretty long time and I'm not that patient.
    12  //
    13  //func benchmarkCacheDBAccounts(n int, b *testing.B) {
    14  //	// 20000 -> 20k
    15  //	staticKeyFilesResourcePath := strconv.Itoa(n)
    16  //	if strings.HasSuffix(staticKeyFilesResourcePath, "000") {
    17  //		staticKeyFilesResourcePath = strings.TrimSuffix(staticKeyFilesResourcePath, "000")
    18  //		staticKeyFilesResourcePath += "k"
    19  //	}
    20  //
    21  //	staticKeyFilesResourcePath = filepath.Join("testdata", "benchmark_keystore"+staticKeyFilesResourcePath)
    22  //
    23  //	start := time.Now()
    24  //	cache := newCacheDB(staticKeyFilesResourcePath)
    25  //	elapsed := time.Since(start)
    26  //
    27  //	b.Logf("establishing cache for %v accs: %v", n, elapsed)
    28  //
    29  //	b.ResetTimer() // _benchmark_ timer, not setup timer.
    30  //
    31  //	for i := 0; i < b.N; i++ {
    32  //		cache.accounts()
    33  //	}
    34  //	cache.close()
    35  //}
    36  //func BenchmarkCacheDBAccounts100(b *testing.B)    { benchmarkCacheDBAccounts(100, b) }
    37  //func BenchmarkCacheDBAccounts500(b *testing.B)    { benchmarkCacheDBAccounts(500, b) }
    38  //func BenchmarkCacheDBAccounts1000(b *testing.B)   { benchmarkCacheDBAccounts(1000, b) }
    39  //func BenchmarkCacheDBAccounts5000(b *testing.B)   { benchmarkCacheDBAccounts(5000, b) }
    40  //func BenchmarkCacheDBAccounts10000(b *testing.B)  { benchmarkCacheDBAccounts(10000, b) }
    41  //func BenchmarkCacheDBAccounts20000(b *testing.B)  { benchmarkCacheDBAccounts(20000, b) }
    42  //func BenchmarkCacheDBAccounts100000(b *testing.B) { benchmarkCacheDBAccounts(100000, b) }
    43  //func BenchmarkCacheDBAccounts200000(b *testing.B) { benchmarkCacheDBAccounts(200000, b) }
    44  //func BenchmarkCacheDBAccounts500000(b *testing.B) { benchmarkCacheDBAccounts(500000, b) }
    45  
    46  // ac.add checks ac.all to see if given account already exists in cache,
    47  // iff it doesn't, it adds account to byAddr map.
    48  //
    49  // No accounts added here are existing in cache, so *sort.Search* will iterate through all
    50  // cached accounts _up to_ relevant alphabetizing. This is somewhat redundant to test cache.accounts(),
    51  // except will test sort.Search instead of sort.Sort.
    52  //
    53  // Note that this _does not_ include ac.newCacheDB.
    54  func benchmarkCacheDBAdd(n int, b *testing.B) {
    55  
    56  	// 20000 -> 20k
    57  	staticKeyFilesResourcePath := strconv.Itoa(n)
    58  	if strings.HasSuffix(staticKeyFilesResourcePath, "000") {
    59  		staticKeyFilesResourcePath = strings.TrimSuffix(staticKeyFilesResourcePath, "000")
    60  		staticKeyFilesResourcePath += "k"
    61  	}
    62  
    63  	staticKeyFilesResourcePath = filepath.Join("testdata", "benchmark_keystore"+staticKeyFilesResourcePath)
    64  
    65  	start := time.Now()
    66  	cache := newCacheDB(staticKeyFilesResourcePath)
    67  	elapsed := time.Since(start)
    68  
    69  	b.Logf("establishing cache for %v accs: %v", n, elapsed)
    70  
    71  	b.ResetTimer() // _benchmark_ timer, not setup timer.
    72  
    73  	for i := 0; i < b.N; i++ {
    74  		// cacheTestAccounts are constant established in cache_test.go
    75  		for _, a := range cachetestAccounts {
    76  			cache.add(a)
    77  		}
    78  	}
    79  	cache.close()
    80  }
    81  
    82  func BenchmarkCacheDBAdd100(b *testing.B)    { benchmarkCacheDBAdd(100, b) }
    83  func BenchmarkCacheDBAdd500(b *testing.B)    { benchmarkCacheDBAdd(500, b) }
    84  func BenchmarkCacheDBAdd1000(b *testing.B)   { benchmarkCacheDBAdd(1000, b) }
    85  func BenchmarkCacheDBAdd5000(b *testing.B)   { benchmarkCacheDBAdd(5000, b) }
    86  func BenchmarkCacheDBAdd10000(b *testing.B)  { benchmarkCacheDBAdd(10000, b) }
    87  func BenchmarkCacheDBAdd20000(b *testing.B)  { benchmarkCacheDBAdd(20000, b) }
    88  func BenchmarkCacheDBAdd100000(b *testing.B) { benchmarkCacheDBAdd(100000, b) }
    89  func BenchmarkCacheDBAdd200000(b *testing.B) { benchmarkCacheDBAdd(200000, b) }
    90  func BenchmarkCacheDBAdd500000(b *testing.B) { benchmarkCacheDBAdd(500000, b) }
    91  
    92  // ac.find checks ac.all to see if given account already exists in cache,
    93  // iff it doesn't, it adds account to byAddr map.
    94  //
    95  // 3/4 added here are existing in cache; .find will iterate through ac.all
    96  // cached, breaking only upon a find. There is no sort. method here.
    97  //
    98  // Note that this _does not_ include ac.newCacheDB.
    99  func benchmarkCacheDBFind(n int, onlyExisting bool, b *testing.B) {
   100  
   101  	// 20000 -> 20k
   102  	staticKeyFilesResourcePath := strconv.Itoa(n)
   103  	if strings.HasSuffix(staticKeyFilesResourcePath, "000") {
   104  		staticKeyFilesResourcePath = strings.TrimSuffix(staticKeyFilesResourcePath, "000")
   105  		staticKeyFilesResourcePath += "k"
   106  	}
   107  
   108  	staticKeyFilesResourcePath = filepath.Join("testdata", "benchmark_keystore"+staticKeyFilesResourcePath)
   109  
   110  	start := time.Now()
   111  	cache := newCacheDB(staticKeyFilesResourcePath)
   112  	elapsed := time.Since(start)
   113  	b.Logf("establishing cache for %v accs: %v", n, elapsed)
   114  
   115  	accs := cache.accounts()
   116  
   117  	// Set up 1 DNE and 3 existing accs.
   118  	// Using the last accs because they should take the longest to iterate to.
   119  	var findAccounts []Account
   120  
   121  	if !onlyExisting {
   122  		findAccounts = append(cachetestAccounts[(len(cachetestAccounts)-1):], accs[(len(accs)-3):]...)
   123  		if len(findAccounts) != 4 {
   124  			b.Fatalf("wrong number find accs: got: %v, want: 4", len(findAccounts))
   125  		}
   126  	} else {
   127  		findAccounts = accs[(len(accs) - 4):]
   128  	}
   129  	accs = nil // clear mem?
   130  
   131  	b.ResetTimer() // _benchmark_ timer, not setup timer.
   132  
   133  	for i := 0; i < b.N; i++ {
   134  		for _, a := range findAccounts {
   135  			cache.find(a)
   136  		}
   137  	}
   138  	cache.close()
   139  }
   140  
   141  func BenchmarkCacheDBFind100(b *testing.B)                { benchmarkCacheDBFind(100, false, b) }
   142  func BenchmarkCacheDBFind500(b *testing.B)                { benchmarkCacheDBFind(500, false, b) }
   143  func BenchmarkCacheDBFind1000(b *testing.B)               { benchmarkCacheDBFind(1000, false, b) }
   144  func BenchmarkCacheDBFind5000(b *testing.B)               { benchmarkCacheDBFind(5000, false, b) }
   145  func BenchmarkCacheDBFind10000(b *testing.B)              { benchmarkCacheDBFind(10000, false, b) }
   146  func BenchmarkCacheDBFind20000(b *testing.B)              { benchmarkCacheDBFind(20000, false, b) }
   147  func BenchmarkCacheDBFind100000(b *testing.B)             { benchmarkCacheDBFind(100000, false, b) }
   148  func BenchmarkCacheDBFind200000(b *testing.B)             { benchmarkCacheDBFind(200000, false, b) }
   149  func BenchmarkCacheDBFind500000(b *testing.B)             { benchmarkCacheDBFind(500000, false, b) }
   150  func BenchmarkCacheDBFind100OnlyExisting(b *testing.B)    { benchmarkCacheDBFind(100, true, b) }
   151  func BenchmarkCacheDBFind500OnlyExisting(b *testing.B)    { benchmarkCacheDBFind(500, true, b) }
   152  func BenchmarkCacheDBFind1000OnlyExisting(b *testing.B)   { benchmarkCacheDBFind(1000, true, b) }
   153  func BenchmarkCacheDBFind5000OnlyExisting(b *testing.B)   { benchmarkCacheDBFind(5000, true, b) }
   154  func BenchmarkCacheDBFind10000OnlyExisting(b *testing.B)  { benchmarkCacheDBFind(10000, true, b) }
   155  func BenchmarkCacheDBFind20000OnlyExisting(b *testing.B)  { benchmarkCacheDBFind(20000, true, b) }
   156  func BenchmarkCacheDBFind100000OnlyExisting(b *testing.B) { benchmarkCacheDBFind(100000, true, b) }
   157  func BenchmarkCacheDBFind200000OnlyExisting(b *testing.B) { benchmarkCacheDBFind(200000, true, b) }
   158  func BenchmarkCacheDBFind500000OnlyExisting(b *testing.B) { benchmarkCacheDBFind(500000, true, b) }