bench_test.go raw

   1  package hdkeychain_test
   2  
   3  import (
   4  	"testing"
   5  	
   6  	"github.com/p9c/p9/pkg/util/hdkeychain"
   7  )
   8  
   9  // bip0032MasterPriv1 is the master private extended key from the first set of test vectors in BIP0032.
  10  const bip0032MasterPriv1 = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbP" +
  11  	"y6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
  12  
  13  // BenchmarkDeriveHardened benchmarks how long it takes to derive a hardened child from a master private extended key.
  14  func BenchmarkDeriveHardened(b *testing.B) {
  15  	b.StopTimer()
  16  	masterKey, e := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
  17  	if e != nil {
  18  		b.Errorf("Failed to decode master seed: %v", e)
  19  	}
  20  	b.StartTimer()
  21  	for i := 0; i < b.N; i++ {
  22  		_, _ = masterKey.Child(hdkeychain.HardenedKeyStart)
  23  	}
  24  }
  25  
  26  // BenchmarkDeriveNormal benchmarks how long it takes to derive a normal (non-hardened) child from a master private
  27  // extended key.
  28  func BenchmarkDeriveNormal(b *testing.B) {
  29  	b.StopTimer()
  30  	masterKey, e := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
  31  	if e != nil {
  32  		b.Errorf("Failed to decode master seed: %v", e)
  33  	}
  34  	b.StartTimer()
  35  	for i := 0; i < b.N; i++ {
  36  		_, _ = masterKey.Child(0)
  37  	}
  38  }
  39  
  40  // BenchmarkPrivToPub benchmarks how long it takes to convert a private extended key to a public extended key.
  41  func BenchmarkPrivToPub(b *testing.B) {
  42  	b.StopTimer()
  43  	masterKey, e := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
  44  	if e != nil {
  45  		b.Errorf("Failed to decode master seed: %v", e)
  46  	}
  47  	b.StartTimer()
  48  	for i := 0; i < b.N; i++ {
  49  		_, _ = masterKey.Neuter()
  50  	}
  51  }
  52  
  53  // BenchmarkDeserialize benchmarks how long it takes to deserialize a private extended key.
  54  func BenchmarkDeserialize(b *testing.B) {
  55  	for i := 0; i < b.N; i++ {
  56  		_, _ = hdkeychain.NewKeyFromString(bip0032MasterPriv1)
  57  		
  58  	}
  59  }
  60  
  61  // BenchmarkSerialize benchmarks how long it takes to serialize a private extended key.
  62  func BenchmarkSerialize(b *testing.B) {
  63  	b.StopTimer()
  64  	masterKey, e := hdkeychain.NewKeyFromString(bip0032MasterPriv1)
  65  	if e != nil {
  66  		b.Errorf("Failed to decode master seed: %v", e)
  67  	}
  68  	b.StartTimer()
  69  	for i := 0; i < b.N; i++ {
  70  		_ = masterKey.String()
  71  	}
  72  }
  73