Imports #
"flag"
"fmt"
"os"
"runtime"
"time"
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
"flag"
"fmt"
"os"
"runtime"
"time"
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
var benchTime = *ast.CallExprReport as tests are run; default is silent for success.
var chatty = *ast.CallExprvar cpuList []intvar cpuListStr = *ast.CallExprvar cpuProfile = *ast.CallExprvar match = *ast.CallExprvar matchBenchmarks = *ast.CallExprvar memProfile = *ast.CallExprvar memProfileRate = *ast.CallExprvar parallel = *ast.CallExprThe short flag requests that tests run more quickly, but its functionality is provided by test writers themselves. The testing package is just its home. The all.bash installation script sets it to make installation more efficient, but by default the flag is off so a plain "go test" will do a full test of the package.
var short = *ast.CallExprvar timeout = *ast.CallExprvar timer *time.TimerB is a type passed to Benchmark functions to manage benchmark timing and to specify the number of iterations to run.
type B struct {
common
N int
benchmark InternalBenchmark
bytes int64
timerOn bool
result BenchmarkResult
}The results of a benchmark run.
type BenchmarkResult struct {
N int
T time.Duration
Bytes int64
}An internal type but exported because it is cross-package; part of the implementation of go test.
type InternalBenchmark struct {
Name string
F func(b *B)
}type InternalExample struct {
Name string
F func()
Output string
}An internal type but exported because it is cross-package; part of the implementation of go test.
type InternalTest struct {
Name string
F func(*T)
}T is a type passed to Test functions to manage test state and support formatted test logs. Logs are accumulated during execution and dumped to standard error when done.
type T struct {
common
name string
startParallel chan bool
}common holds the elements common between T and B and captures common methods such as Errorf.
type common struct {
output []byte
failed bool
start time.Time
duration time.Duration
self any
signal chan any
}Error is equivalent to Log() followed by Fail().
func (c *common) Error(args ...any)Errorf is equivalent to Logf() followed by Fail().
func (c *common) Errorf(format string, args ...any)Fail marks the function as having failed but continues execution.
func (c *common) Fail()FailNow marks the function as having failed and stops its execution. Execution will continue at the next Test.
func (c *common) FailNow()Failed reports whether the function has failed.
func (c *common) Failed() boolFatal is equivalent to Log() followed by FailNow().
func (c *common) Fatal(args ...any)Fatalf is equivalent to Logf() followed by FailNow().
func (c *common) Fatalf(format string, args ...any)Log formats its arguments using default formatting, analogous to Println(), and records the text in the error log.
func (c *common) Log(args ...any)Logf formats its arguments according to the format, analogous to Printf(), and records the text in the error log.
func (c *common) Logf(format string, args ...any)An internal function but exported because it is cross-package; part of the implementation of go test.
func Main(matchString func(pat string, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)func (r BenchmarkResult) NsPerOp() int64Parallel signals that this test is to be run in parallel with (and only with) other parallel tests in this CPU group.
func (t *T) Parallel()ResetTimer sets the elapsed benchmark time to zero. It does not affect whether the timer is running.
func (b *B) ResetTimer()An internal function but exported because it is cross-package; part of the implementation of go test.
func RunBenchmarks(matchString func(pat string, str string) (bool, error), benchmarks []InternalBenchmark)func RunExamples(examples []InternalExample) (ok bool)func RunTests(matchString func(pat string, str string) (bool, error), tests []InternalTest) (ok bool)SetBytes records the number of bytes processed in a single operation. If this is called, the benchmark will report ns/op and MB/s.
func (b *B) SetBytes(n int64)Short reports whether the -test.short flag is set.
func Short() boolStartTimer starts timing a test. This function is called automatically before a benchmark starts, but it can also used to resume timing after a call to StopTimer.
func (b *B) StartTimer()StopTimer stops timing a test. This can be used to pause the timer while performing complex initialization that you don't want to measure.
func (b *B) StopTimer()func (r BenchmarkResult) String() stringafter runs after all testing.
func after()alarm is called if the timeout expires.
func alarm()before runs before all testing.
func before()decorate inserts the final newline if needed and indentation tabs for formatting. If addFileLine is true, it also prefixes the string with the file and line of the call site.
func decorate(s string, addFileLine bool) stringlaunch launches the benchmark function. It gradually increases the number of benchmark iterations until the benchmark runs for a second in order to get a reasonable measurement. It prints timing information in this form testing.BenchmarkHello 100000 19 ns/op launch is run by the fun function as a separate goroutine.
func (b *B) launch()log generates the output. It's always at the same stack depth.
func (c *common) log(s string)func max(x int, y int) intfunc (r BenchmarkResult) mbPerSec() float64func min(x int, y int) intfunc (b *B) nsPerOp() int64func parseCpuList()func (t *T) report()roundDown10 rounds a number down to the nearest power of 10.
func roundDown10(n int) introundUp rounds x up to a number of the form [1eX, 2eX, 5eX].
func roundUp(n int) intrun times the benchmark function in a separate goroutine.
func (b *B) run() BenchmarkResultrunN runs a single benchmark for the specified number of iterations.
func (b *B) runN(n int)startAlarm starts an alarm if requested.
func startAlarm()stopAlarm turns off the alarm.
func stopAlarm()func tRunner(t *T, test *InternalTest)trimOutput shortens the output from a benchmark, which can be very long.
func (b *B) trimOutput()Generated with Arrow