github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/link/internal/benchmark/bench.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package benchmark provides a Metrics object that enables memory and CPU 6 // profiling for the linker. The Metrics objects can be used to mark stages 7 // of the code, and name the measurements during that stage. There is also 8 // optional GCs that can be performed at the end of each stage, so you 9 // can get an accurate measurement of how each stage changes live memory. 10 package benchmark 11 12 import ( 13 "github.com/shogo82148/std/io" 14 "github.com/shogo82148/std/os" 15 ) 16 17 type Flags int 18 19 const ( 20 GC = 1 << iota 21 NoGC Flags = 0 22 ) 23 24 type Metrics struct { 25 gc Flags 26 marks []*mark 27 curMark *mark 28 filebase string 29 pprofFile *os.File 30 } 31 32 // New creates a new Metrics object. 33 // 34 // Typical usage should look like: 35 // 36 // func main() { 37 // filename := "" // Set to enable per-phase pprof file output. 38 // bench := benchmark.New(benchmark.GC, filename) 39 // defer bench.Report(os.Stdout) 40 // // etc 41 // bench.Start("foo") 42 // foo() 43 // bench.Start("bar") 44 // bar() 45 // } 46 // 47 // Note that a nil Metrics object won't cause any errors, so one could write 48 // code like: 49 // 50 // func main() { 51 // enableBenchmarking := flag.Bool("enable", true, "enables benchmarking") 52 // flag.Parse() 53 // var bench *benchmark.Metrics 54 // if *enableBenchmarking { 55 // bench = benchmark.New(benchmark.GC) 56 // } 57 // bench.Start("foo") 58 // // etc. 59 // } 60 func New(gc Flags, filebase string) *Metrics 61 62 // Report reports the metrics. 63 // Closes the currently Start(ed) range, and writes the report to the given io.Writer. 64 func (m *Metrics) Report(w io.Writer) 65 66 // Start marks the beginning of a new measurement phase. 67 // Once a metric is started, it continues until either a Report is issued, or another Start is called. 68 func (m *Metrics) Start(name string)