From 71243992b48da4c82389949239603fb5e33147ac Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 9 Apr 2022 15:49:13 -0500 Subject: [PATCH] primitives: Add subsidy calc benchmarks. CalcSubsidyCacheSparse 26023311 45.99 ns/op 0 B/op 0 allocs/op CalcWorkSubsidy 21051818 52.12 ns/op 0 B/op 0 allocs/op CalcStakeVoteSubsidy 22216708 53.44 ns/op 0 B/op 0 allocs/op CalcTreasurySubsidy 22610749 49.78 ns/op 0 B/op 0 allocs/op --- .../staging/primitives/subsidy_bench_test.go | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 internal/staging/primitives/subsidy_bench_test.go diff --git a/internal/staging/primitives/subsidy_bench_test.go b/internal/staging/primitives/subsidy_bench_test.go new file mode 100644 index 0000000000..f2eeadc787 --- /dev/null +++ b/internal/staging/primitives/subsidy_bench_test.go @@ -0,0 +1,96 @@ +// Copyright (c) 2019-2022 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 ( + "testing" +) + +// BenchmarkCalcSubsidyCacheSparse benchmarks calculating the subsidy for +// various heights with a sparse access pattern. +func BenchmarkCalcSubsidyCacheSparse(b *testing.B) { + mockParams := mockMainNetParams() + reductionInterval := mockParams.SubsidyReductionIntervalBlocks() + + b.ResetTimer() + b.ReportAllocs() + const numIterations = 100 + for i := 0; i < b.N; i += (numIterations * 5) { + cache := NewSubsidyCache(mockParams) + for j := int64(0); j < numIterations; j++ { + cache.CalcBlockSubsidy(reductionInterval * (10000 + j)) + cache.CalcBlockSubsidy(reductionInterval * 1) + cache.CalcBlockSubsidy(reductionInterval * 5) + cache.CalcBlockSubsidy(reductionInterval * 25) + cache.CalcBlockSubsidy(reductionInterval * 13) + } + } +} + +// BenchmarkCalcWorkSubsidy benchmarks calculating the work subsidy proportion +// for various heights with a sparse access pattern, varying numbers of votes, +// and with and without DCP0010 active. +func BenchmarkCalcWorkSubsidy(b *testing.B) { + mockParams := mockMainNetParams() + reductionInterval := mockParams.SubsidyReductionIntervalBlocks() + + b.ResetTimer() + b.ReportAllocs() + const numIterations = 100 + for i := 0; i < b.N; i += (numIterations * 5) { + cache := NewSubsidyCache(mockParams) + for j := int64(0); j < numIterations; j++ { + cache.CalcWorkSubsidy(reductionInterval*(10000+j), 5, true) + cache.CalcWorkSubsidy(reductionInterval*1, 4, false) + cache.CalcWorkSubsidy(reductionInterval*5, 3, false) + cache.CalcWorkSubsidy(reductionInterval*25, 4, true) + cache.CalcWorkSubsidy(reductionInterval*13, 5, false) + } + } +} + +// BenchmarkCalcStakeVoteSubsidy benchmarks calculating the stake vote subsidy +// proportion for various heights with a sparse access pattern with and without +// DCP0010 active. +func BenchmarkCalcStakeVoteSubsidy(b *testing.B) { + mockParams := mockMainNetParams() + reductionInterval := mockParams.SubsidyReductionIntervalBlocks() + + b.ResetTimer() + b.ReportAllocs() + const numIterations = 100 + for i := 0; i < b.N; i += (numIterations * 5) { + cache := NewSubsidyCache(mockParams) + for j := int64(0); j < numIterations; j++ { + cache.CalcStakeVoteSubsidy(reductionInterval*(10000+j), true) + cache.CalcStakeVoteSubsidy(reductionInterval*1, false) + cache.CalcStakeVoteSubsidy(reductionInterval*5, false) + cache.CalcStakeVoteSubsidy(reductionInterval*25, true) + cache.CalcStakeVoteSubsidy(reductionInterval*13, false) + } + } +} + +// BenchmarkCalcTreasurySubsidy benchmarks calculating the treasury subsidy +// proportion for various heights with a sparse access pattern, varying numbers +// of votes, and with and without DCP0006 active. +func BenchmarkCalcTreasurySubsidy(b *testing.B) { + mockParams := mockMainNetParams() + reductionInterval := mockParams.SubsidyReductionIntervalBlocks() + + b.ResetTimer() + b.ReportAllocs() + const numIterations = 100 + for i := 0; i < b.N; i += (numIterations * 5) { + cache := NewSubsidyCache(mockParams) + for j := int64(0); j < numIterations; j++ { + cache.CalcTreasurySubsidy(reductionInterval*(10000+j), 5, true) + cache.CalcTreasurySubsidy(reductionInterval*1, 4, false) + cache.CalcTreasurySubsidy(reductionInterval*5, 3, false) + cache.CalcTreasurySubsidy(reductionInterval*25, 4, true) + cache.CalcTreasurySubsidy(reductionInterval*13, 5, true) + } + } +}