From 8de1bc3ecc4ff6f56179fa068a0e40ee46a1c9d7 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:30 -0500 Subject: [PATCH] txscript: Add benchmark for GetPreciseSigOpCount. --- txscript/bench_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/txscript/bench_test.go b/txscript/bench_test.go index 7d0b90c4..817eb98f 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -356,3 +356,29 @@ func BenchmarkGetSigOpCount(b *testing.B) { _ = GetSigOpCount(script) } } + +// BenchmarkGetPreciseSigOpCount benchmarks how long it takes to count the +// signature operations of a very large script using the more precise counting +// method. +func BenchmarkGetPreciseSigOpCount(b *testing.B) { + redeemScript, err := genComplexScript() + if err != nil { + b.Fatalf("failed to create benchmark script: %v", err) + } + + // Create a fake pay-to-script-hash to pass the necessary checks and create + // the signature script accordingly by pushing the generated "redeem" script + // as the final data push so the benchmark will cover the p2sh path. + scriptHash := "0x0000000000000000000000000000000000000001" + pkScript := mustParseShortForm("HASH160 DATA_20 " + scriptHash + " EQUAL") + sigScript, err := NewScriptBuilder().AddFullData(redeemScript).Script() + if err != nil { + b.Fatalf("failed to create signature script: %v", err) + } + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _ = GetPreciseSigOpCount(sigScript, pkScript, true) + } +}