From d29792696af366fa07875ef716a841eec2b42bde Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 5 Mar 2021 08:55:49 -0600 Subject: [PATCH] stdaddr: Add benchmarks. This adds benchmarks for decoding and generating the payment script for each supported version 0 address type. This is part of a series of commits to fully implement the stdaddr package. --- internal/staging/stdaddr/bench_test.go | 118 +++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 internal/staging/stdaddr/bench_test.go diff --git a/internal/staging/stdaddr/bench_test.go b/internal/staging/stdaddr/bench_test.go new file mode 100644 index 00000000..24da07d4 --- /dev/null +++ b/internal/staging/stdaddr/bench_test.go @@ -0,0 +1,118 @@ +// Copyright (c) 2021 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package stdaddr + +import ( + "testing" + + "github.com/decred/dcrd/chaincfg/v3" +) + +// BenchmarkDecode benchmarks the performance of decoding various types of +// addresses. +func BenchmarkDecode(b *testing.B) { + mainNetParams := chaincfg.MainNetParams() + + benches := []struct { + name string // benchmark name + addr string // address to decode + params AddressParams + }{{ + name: "v0 p2sh", + addr: "DcuQKx8BES9wU7C6Q5VmLBjw436r27hayjS", + params: mainNetParams, + }, { + name: "v0 p2pkh-ecdsa-secp256k1", + addr: "DsUZxxoHJSty8DCfwfartwTYbuhmVct7tJu", + params: mainNetParams, + }, { + name: "v0 p2pkh-ed25519", + addr: "DeeUhrRoTp4DftsqddVW96yMGMW4sgQFYUE", + params: mainNetParams, + }, { + name: "v0 p2pkh-schnorr-secp256k1", + addr: "DSXcZv4oSRiEoWL2a9aD8sgfptRo1YEXNKj", + params: mainNetParams, + }, { + name: "v0 p2pk-ecdsa-secp256k1", + addr: "DkM3ZigNyiwHrsXRjkDQ8t8tW6uKGW9g61qEkG3bMqQPQWYEf5X3J", + params: mainNetParams, + }, { + name: "v0 p2pk-ed25519", + addr: "DkM5zR8tqWNAHngZQDTyAeqzabZxMKrkSbCFULDhmvySn3uHmm221", + params: mainNetParams, + }, { + name: "v0 p2pk-schnorr-secp256k1", + addr: "DkM7TD2qsne9DKo4uA2ZNt3XhejYVwT5mmQWtUXtjdPhRHXTSKxN4", + params: mainNetParams, + }} + + for _, bench := range benches { + b.Run(bench.name, func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := DecodeAddress(bench.addr, bench.params) + if err != nil { + b.Fatalf("%q: unexpected error: %v", bench.name, err) + } + } + }) + } +} + +// BenchmarkPaymentScript benchmarks the performance of generating payment +// scripts for various types of addresses. +func BenchmarkPaymentScript(b *testing.B) { + mainNetParams := chaincfg.MainNetParams() + + benches := []struct { + name string // benchmark name + addr string // address to decode + params AddressParams + }{{ + name: "v0 p2sh", + addr: "DcuQKx8BES9wU7C6Q5VmLBjw436r27hayjS", + params: mainNetParams, + }, { + name: "v0 p2pkh-ecdsa-secp256k1", + addr: "DsUZxxoHJSty8DCfwfartwTYbuhmVct7tJu", + params: mainNetParams, + }, { + name: "v0 p2pkh-ed25519", + addr: "DeeUhrRoTp4DftsqddVW96yMGMW4sgQFYUE", + params: mainNetParams, + }, { + name: "v0 p2pkh-schnorr-secp256k1", + addr: "DSXcZv4oSRiEoWL2a9aD8sgfptRo1YEXNKj", + params: mainNetParams, + }, { + name: "v0 p2pk-ecdsa-secp256k1", + addr: "DkM3ZigNyiwHrsXRjkDQ8t8tW6uKGW9g61qEkG3bMqQPQWYEf5X3J", + params: mainNetParams, + }, { + name: "v0 p2pk-ed25519", + addr: "DkM5zR8tqWNAHngZQDTyAeqzabZxMKrkSbCFULDhmvySn3uHmm221", + params: mainNetParams, + }, { + name: "v0 p2pk-schnorr-secp256k1", + addr: "DkM7TD2qsne9DKo4uA2ZNt3XhejYVwT5mmQWtUXtjdPhRHXTSKxN4", + params: mainNetParams, + }} + + for _, bench := range benches { + addr, err := DecodeAddress(bench.addr, bench.params) + if err != nil { + b.Fatalf("%q: unexpected error: %v", bench.name, err) + } + b.Run(bench.name, func(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = addr.PaymentScript() + } + }) + } +}