天天看點

go語言基準測試對比c,Go語言中單元測試、基準測試及http 測試

func (c *B) Log(args ...interface{})

func (c *B) Logf(format string, args ...interface{})

func (c *B) Fail()

func (c *B) FailNow()

func (c *B) Failed() bool

func (c *B) Error(args ...interface{})

func (c *B) Errorf(format string, args ...interface{})

func (c *B) Fatal(args ...interface{})

func (c *B) Fatalf(format string, args ...interface{})

func (c *B) Helper()

func (c *B) Name() string

func (b *B) Run(name string, f func(b *B)) bool

func (c *B) Skip(args ...interface{})

func (c *B) SkipNow()

func (c *B) Skipf(format string, args ...interface{})

func (c *B) Skipped() bool

// 打開目前基準測試的記憶體統計功能,與使用 -test.benchmem 設定類似,

// 但 ReportAllocs 隻影響那些調用了該函數的基準測試。

func (b *B) ReportAllocs()

// 對已經逝去的基準測試時間以及記憶體配置設定計數器進行清零。對于正在運作中的計時器,這個方法不會産生任何效果。

func (b *B) ResetTimer()

例:

func BenchmarkBigLen(b *testing.B) {

big := NewBig()

b.ResetTimer()

for i := 0; i < b.N; i++ {

big.Len()

}

}

// 以并行的方式執行給定的基準測試。RunParallel 會建立出多個 goroutine,并将 b.N 個疊代配置設定給這些 goroutine 執行,

// 其中 goroutine 數量的預設值為 GOMAXPROCS。使用者如果想要增加非CPU受限(non-CPU-bound)基準測試的并行性,

// 那麼可以在 RunParallel 之前調用 SetParallelism。RunParallel 通常會與 -cpu 标志一同使用。

// body 函數将在每個 goroutine 中執行,這個函數需要設定所有 goroutine 本地的狀态,

// 并疊代直到 pb.Next 傳回 false 值為止。因為 StartTimer、StopTimer 和 ResetTimer 這三個函數都帶有全局作用,是以 body函數不應該調用這些函數;

// 除此之外,body 函數也不應該調用 Run 函數。

func (b *B) RunParallel(body func(*PB))

例:

func BenchmarkTemplateParallel(b *testing.B) {

templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))

b.RunParallel(func(pb *testing.PB) {

var buf bytes.Buffer

for pb.Next() {

buf.Reset()

templ.Execute(&buf, "World")

}

})

}

// 記錄在單個操作中處理的位元組數量。 在調用了這個方法之後, 基準測試将會報告 ns/op 以及 MB/s

func (b *B) SetBytes(n int64)

// 将 RunParallel 使用的 goroutine 數量設定為 p*GOMAXPROCS,如果 p 小于 1,那麼調用将不産生任何效果。

// CPU受限(CPU-bound)的基準測試通常不需要調用這個方法。

func (b *B) SetParallelism(p int)

// 開始對測試進行計時。

// 這個函數在基準測試開始時會自動被調用,它也可以在調用 StopTimer 之後恢複進行計時。

func (b *B) StartTimer()

// 停止對測試進行計時。

func (b *B) StopTimer()