GO語言"testing"包中"B.ReportMetric"類型的用法及代碼示例。
用法:
func(b *B) ReportMetric(n float64, unit string)
ReportMetric 將"n unit" 添加到報告的基準測試結果中。如果度量是per-iteration,調用者應該除以b.N,並且按照慣例,單位應該以"/op"結尾。 ReportMetric 覆蓋同一單位的任何先前報告的值。 ReportMetric 如果 unit 是空字符串或 unit 包含任何空格,則會出現Panics。如果 unit 是基準框架本身通常報告的單位(例如"allocs/op"),ReportMetric 將覆蓋該指標。將 "ns/op" 設置為 0 將抑製該內置指標。
例子:
package main
import (
"sort"
"testing"
)
func main() {
// This reports a custom benchmark metric relevant to a
// specific algorithm (in this case, sorting).
testing.Benchmark(func(b *testing.B) {
var compares int64
for i := 0; i < b.N; i++ {
s := []int{5, 4, 3, 2, 1}
sort.Slice(s, func(i, j int) bool {
compares++
return s[i] < s[j]
})
}
// This metric is per-operation, so divide by b.N and
// report it as a "/op" unit.
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
})
}
相關用法
- GO B.RunParallel用法及代碼示例
- GO Buffer.Bytes用法及代碼示例
- GO Buffer.Read用法及代碼示例
- GO Buffer.Len用法及代碼示例
- GO Buffer用法及代碼示例
- GO Buffer.Next用法及代碼示例
- GO Buffer.ReadByte用法及代碼示例
- GO Base用法及代碼示例
- GO Buffer.Cap用法及代碼示例
- GO Builder用法及代碼示例
- GO ByteOrder用法及代碼示例
- GO Buffer.Grow用法及代碼示例
- GO BinaryOp用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO NewFromFiles用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Time.Sub用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 B.ReportMetric。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。