annotate libgo/go/testing/allocs.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2013 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package testing
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 import (
kono
parents:
diff changeset
8 "runtime"
kono
parents:
diff changeset
9 )
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 // AllocsPerRun returns the average number of allocations during calls to f.
kono
parents:
diff changeset
12 // Although the return value has type float64, it will always be an integral value.
kono
parents:
diff changeset
13 //
kono
parents:
diff changeset
14 // To compute the number of allocations, the function will first be run once as
kono
parents:
diff changeset
15 // a warm-up. The average number of allocations over the specified number of
kono
parents:
diff changeset
16 // runs will then be measured and returned.
kono
parents:
diff changeset
17 //
kono
parents:
diff changeset
18 // AllocsPerRun sets GOMAXPROCS to 1 during its measurement and will restore
kono
parents:
diff changeset
19 // it before returning.
kono
parents:
diff changeset
20 func AllocsPerRun(runs int, f func()) (avg float64) {
kono
parents:
diff changeset
21 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 // Warm up the function
kono
parents:
diff changeset
24 f()
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 // Measure the starting statistics
kono
parents:
diff changeset
27 var memstats runtime.MemStats
kono
parents:
diff changeset
28 runtime.ReadMemStats(&memstats)
kono
parents:
diff changeset
29 mallocs := 0 - memstats.Mallocs
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 // Run the function the specified number of times
kono
parents:
diff changeset
32 for i := 0; i < runs; i++ {
kono
parents:
diff changeset
33 f()
kono
parents:
diff changeset
34 }
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 // Read the final statistics
kono
parents:
diff changeset
37 runtime.ReadMemStats(&memstats)
kono
parents:
diff changeset
38 mallocs += memstats.Mallocs
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 // Average the mallocs over the runs (not counting the warm-up).
kono
parents:
diff changeset
41 // We are forced to return a float64 because the API is silly, but do
kono
parents:
diff changeset
42 // the division as integers so we can ask if AllocsPerRun()==1
kono
parents:
diff changeset
43 // instead of AllocsPerRun()<2.
kono
parents:
diff changeset
44 return float64(mallocs / uint64(runs))
kono
parents:
diff changeset
45 }