From b65356b565286bb1d161cf88d1170168fc9b024c Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 13 Aug 2019 17:02:48 +0200 Subject: prog: measure memory allocation in benchmarks Enable ReportAllocs. Also factor out common code into a helper function (duplicated in 3 places now). --- prog/export_test.go | 11 +++++++++++ prog/hints_test.go | 9 ++------- prog/mutation_test.go | 18 ++++-------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/prog/export_test.go b/prog/export_test.go index 1aaea30dc..9ea3f612e 100644 --- a/prog/export_test.go +++ b/prog/export_test.go @@ -89,3 +89,14 @@ func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs }) } } + +func initBench(b *testing.B) (*Target, func()) { + olddebug := debug + debug = false + target, err := GetTarget("linux", "amd64") + if err != nil { + b.Fatal(err) + } + b.ReportAllocs() + return target, func() { debug = olddebug } +} diff --git a/prog/hints_test.go b/prog/hints_test.go index 67a250431..78b86e57c 100644 --- a/prog/hints_test.go +++ b/prog/hints_test.go @@ -480,13 +480,8 @@ func TestHintsData(t *testing.T) { } func BenchmarkHints(b *testing.B) { - olddebug := debug - debug = false - defer func() { debug = olddebug }() - target, err := GetTarget("linux", "amd64") - if err != nil { - b.Fatal(err) - } + target, cleanup := initBench(b) + defer cleanup() rs := rand.NewSource(0) r := newRand(target, rs) p := target.Generate(rs, 30, nil) diff --git a/prog/mutation_test.go b/prog/mutation_test.go index 6b50b975e..b041f02d2 100644 --- a/prog/mutation_test.go +++ b/prog/mutation_test.go @@ -220,13 +220,8 @@ func runMutationTests(t *testing.T, tests [][2]string) { } func BenchmarkMutate(b *testing.B) { - olddebug := debug - debug = false - defer func() { debug = olddebug }() - target, err := GetTarget("linux", "amd64") - if err != nil { - b.Fatal(err) - } + target, cleanup := initBench(b) + defer cleanup() ct := linuxAmd64ChoiceTable(target) const progLen = 30 p := target.Generate(rand.NewSource(0), progLen, nil) @@ -240,13 +235,8 @@ func BenchmarkMutate(b *testing.B) { } func BenchmarkGenerate(b *testing.B) { - olddebug := debug - debug = false - defer func() { debug = olddebug }() - target, err := GetTarget("linux", "amd64") - if err != nil { - b.Fatal(err) - } + target, cleanup := initBench(b) + defer cleanup() ct := linuxAmd64ChoiceTable(target) const progLen = 30 b.ResetTimer() -- cgit mrf-deployment