ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 语法 ### Func 基本都不常用 ``` func AllocsPerRun(runs int, f func()) (avg float64) func CoverMode() string func Coverage() float64 func Init() func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, ...) func RegisterCover(c Cover) func RunBenchmarks(matchString func(pat, str string) (bool, error), ...) func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) func Short() bool func Verbose() bool ``` ### Type ``` type B func (c *B) Cleanup(f func()) //在第一次调用后,和执行完后执行 func (c *B) Error(args ...interface{}) func (c *B) Errorf(format string, args ...interface{}) func (c *B) Fail() func (c *B) FailNow() // 停止并标记为失败 func (c *B) Failed() bool // 是否失败 func (c *B) Fatal(args ...interface{}) func (c *B) Fatalf(format string, args ...interface{}) func (c *B) Log(args ...interface{}) func (c *B) Logf(format string, args ...interface{}) func (c *B) Helper() func (c *B) Name() string // 测试函数名 // 打开当前基准测试的内存统计功能, // 与使用 -test.benchmem 设置类似,但 ReportAllocs 只影响那些调用了该函数的基准测试 func (b *B) ReportAllocs() func (b *B) ReportMetric(n float64, unit string) func (b *B) Run(name string, f func(b *B)) bool // 运行子程序 // 以并行的方式执行给定的基准测试。 RunParallel 会创建出多个 goroutine // 其中 goroutine 数量的默认值为 GOMAXPROCS func (b *B) RunParallel(body func(*PB)) // 将 RunParallel 使用的 goroutine 数量设置为 p*GOMAXPROCS func (b *B) SetParallelism(p int) // 单个操作中处理的字节数, 基准测试将会报告 ns/op 以及 MB/s func (b *B) SetBytes(n int64) // 跳过测试,并调用日志 func (c *B) Skip(args ...interface{}) func (c *B) SkipNow() func (c *B) Skipf(format string, args ...interface{}) func (c *B) Skipped() bool // 是否跳过 // StartTimer 和 StopTimer 控制时间 func (b *B) StartTimer() func (b *B) StopTimer() func (b *B) ResetTimer() // 清除 ns/op 数 type PB func (pb *PB) Next() bool type T func (c *T) Cleanup(f func()) func (t *T) Deadline() (deadline time.Time, ok bool) func (c *T) Error(args ...interface{}) func (c *T) Errorf(format string, args ...interface{}) func (c *T) Log(args ...interface{}) func (c *T) Logf(format string, args ...interface{}) func (c *T) Fatal(args ...interface{}) func (c *T) Fatalf(format string, args ...interface{}) func (c *T) Fail() func (c *T) FailNow() func (c *T) Failed() bool func (c *T) Helper() func (c *T) Name() string func (t *T) Parallel() // 并行测试 func (t *T) Run(name string, f func(t *T)) bool func (c *T) Skip(args ...interface{}) func (c *T) SkipNow() func (c *T) Skipf(format string, args ...interface{}) func (c *T) Skipped() bool ``` ## 实例 ### RunParallel 并行测试 ``` func BenchmarkHello(b *testing.B) { templ := template.Must(template.New("test").Parse("Hello, {{.}}!")) // RunParallel will create GOMAXPROCS goroutines // and distribute work among them. b.RunParallel(func(pb *testing.PB) { // Each goroutine has its own bytes.Buffer. var buf bytes.Buffer for pb.Next() { // The loop body is executed b.N times total across all goroutines. buf.Reset() templ.Execute(&buf, "World") } }) } ``` ### SetBytes 设置n=1 为一次操作, ``` func Benchmark_Api(b *testing.B) { b.SetBytes(1) for i := 0; i < b.N; i++ { Hello() } } // output: // Benchmark_Api-4 1000000000 0.341 ns/op 2932.28 MB/s ```