Skip to content

Commit

Permalink
primitives: Add merkle root benchmarks.
Browse files Browse the repository at this point in the history
BenchmarkCalcMerkleRootInPlace
------------------------------
20_leaves      95551      12436 ns/op   0 B/op   0 allocs/op
1000_leaves     2047     591660 ns/op   0 B/op   0 allocs/op
2000_leaves     1018    1179846 ns/op   0 B/op   0 allocs/op
4000_leaves      507    2357246 ns/op   0 B/op   0 allocs/op
8000_leaves      254    4710341 ns/op   0 B/op   0 allocs/op
16000_leaves     127    9499098 ns/op   0 B/op   0 allocs/op
32000_leaves      62   18876558 ns/op   0 B/op   0 allocs/op

BenchmarkCalcMerkleRoot
-----------------------
20_leaves      92455      12840 ns/op       640 B/op   1 allocs/op
1000_leaves     1965     608127 ns/op     32769 B/op   1 allocs/op
2000_leaves      969    1261865 ns/op     65536 B/op   1 allocs/op
4000_leaves      493    2439323 ns/op    131073 B/op   1 allocs/op
8000_leaves      246    4850361 ns/op    262144 B/op   1 allocs/op
16000_leaves     122    9826700 ns/op    516101 B/op   1 allocs/op
32000_leaves      63   19420881 ns/op   1024008 B/op   1 allocs/op
  • Loading branch information
davecgh committed Dec 4, 2021
1 parent db8c99f commit a4526b4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions internal/staging/primitives/merkle_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2019-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package primitives

import (
"fmt"
"testing"

"github.com/decred/dcrd/chaincfg/chainhash"
)

// BenchmarkCalcMerkleRootInPlace benchmarks merkle root calculation for various
// numbers of leaves using the mutable in-place algorithm.
func BenchmarkCalcMerkleRootInPlace(b *testing.B) {
// Create several slices of leaves of various sizes to benchmark.
numLeavesToBench := []int{20, 1000, 2000, 4000, 8000, 16000, 32000}
origLeaves := make([][]chainhash.Hash, len(numLeavesToBench))
for i, numLeaves := range numLeavesToBench {
origLeaves[i] = make([]chainhash.Hash, numLeaves)
}

for benchIdx := range origLeaves {
testLeaves := origLeaves[benchIdx]
benchName := fmt.Sprintf("%d leaves", len(testLeaves))
b.Run(benchName, func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = CalcMerkleRootInPlace(testLeaves)
}
})
}
}

// BenchmarkCalcMerkleRoot benchmarks merkle root calculation for various
// numbers of leaves using the non-mutable version.
func BenchmarkCalcMerkleRoot(b *testing.B) {
// Create several slices of leaves of various sizes to benchmark.
numLeavesToBench := []int{20, 1000, 2000, 4000, 8000, 16000, 32000}
origLeaves := make([][]chainhash.Hash, len(numLeavesToBench))
for i, numLeaves := range numLeavesToBench {
origLeaves[i] = make([]chainhash.Hash, numLeaves)
}

for benchIdx := range origLeaves {
testLeaves := origLeaves[benchIdx]
benchName := fmt.Sprintf("%d leaves", len(testLeaves))
b.Run(benchName, func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = CalcMerkleRoot(testLeaves)
}
})
}
}

0 comments on commit a4526b4

Please sign in to comment.