Skip to content

Commit

Permalink
txscript: Add benchmark for script parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent d683492 commit a93ee39
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions txscript/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,43 @@ func BenchmarkCalcWitnessSigHash(b *testing.B) {
}
}
}

// genComplexScript returns a script comprised of half as many opcodes as the
// maximum allowed followed by as many max size data pushes fit without
// exceeding the max allowed script size.
func genComplexScript() ([]byte, error) {
var scriptLen int
builder := NewScriptBuilder()
for i := 0; i < MaxOpsPerScript/2; i++ {
builder.AddOp(OP_TRUE)
scriptLen++
}
maxData := bytes.Repeat([]byte{0x02}, MaxScriptElementSize)
for i := 0; i < (MaxScriptSize-scriptLen)/(MaxScriptElementSize+3); i++ {
builder.AddData(maxData)
}
return builder.Script()
}

// BenchmarkScriptParsing benchmarks how long it takes to parse a very large
// script.
func BenchmarkScriptParsing(b *testing.B) {
script, err := genComplexScript()
if err != nil {
b.Fatalf("failed to create benchmark script: %v", err)
}

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
pops, err := parseScript(script)
if err != nil {
b.Fatalf("failed to parse script: %v", err)
}

for _, pop := range pops {
_ = pop.opcode
_ = pop.data
}
}
}

0 comments on commit a93ee39

Please sign in to comment.