github.com/decred/dcrlnd@v0.7.6/internal/zero/benchmark_test.go (about)

     1  // Copyright (c) 2015 The btcsuite developers
     2  // Copyright (c) 2015-2018 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package zero
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  var (
    13  	bytes32 = make([]byte, 32) // typical key size
    14  	bytes64 = make([]byte, 64) // passphrase hash size
    15  	bytea32 = new([32]byte)
    16  	bytea64 = new([64]byte)
    17  )
    18  
    19  // xor is the "slow" byte zeroing implementation which this package
    20  // originally replaced.  If this function benchmarks faster than the
    21  // functions exported by this package in a future Go version (perhaps
    22  // by calling runtime.memclr), replace the "optimized" versions with
    23  // this.
    24  func xor(b []byte) {
    25  	for i := range b {
    26  		b[i] ^= b[i]
    27  	}
    28  }
    29  
    30  // zrange is an alternative zero implementation that, while currently
    31  // slower than the functions provided by this package, may be faster
    32  // in a future Go release.  Switch to this or the xor implementation
    33  // if they ever become faster.
    34  func zrange(b []byte) {
    35  	for i := range b {
    36  		b[i] = 0
    37  	}
    38  }
    39  
    40  func BenchmarkXor32(b *testing.B) {
    41  	for i := 0; i < b.N; i++ {
    42  		xor(bytes32)
    43  	}
    44  }
    45  
    46  func BenchmarkXor64(b *testing.B) {
    47  	for i := 0; i < b.N; i++ {
    48  		xor(bytes64)
    49  	}
    50  }
    51  
    52  func BenchmarkRange32(b *testing.B) {
    53  	for i := 0; i < b.N; i++ {
    54  		zrange(bytes32)
    55  	}
    56  }
    57  
    58  func BenchmarkRange64(b *testing.B) {
    59  	for i := 0; i < b.N; i++ {
    60  		zrange(bytes64)
    61  	}
    62  }
    63  
    64  func BenchmarkBytes32(b *testing.B) {
    65  	for i := 0; i < b.N; i++ {
    66  		Bytes(bytes32)
    67  	}
    68  }
    69  
    70  func BenchmarkBytes64(b *testing.B) {
    71  	for i := 0; i < b.N; i++ {
    72  		Bytes(bytes64)
    73  	}
    74  }
    75  
    76  func BenchmarkBytea32(b *testing.B) {
    77  	for i := 0; i < b.N; i++ {
    78  		Bytea32(bytea32)
    79  	}
    80  }
    81  
    82  func BenchmarkBytea64(b *testing.B) {
    83  	for i := 0; i < b.N; i++ {
    84  		Bytea64(bytea64)
    85  	}
    86  }