1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
package nettrace
import (
"testing"
)
// Our benchmark loop is similar to the one from golang.org/x/net/trace, so we
// can compare results.
func runBench(b *testing.B, events int) {
nTraces := (b.N + events + 1) / events
for i := 0; i < nTraces; i++ {
tr := New("bench", "test")
for j := 0; j < events; j++ {
tr.Printf("%d", j)
}
tr.Finish()
}
}
func BenchmarkTrace_2(b *testing.B) {
runBench(b, 2)
}
func BenchmarkTrace_10(b *testing.B) {
runBench(b, 10)
}
func BenchmarkTrace_100(b *testing.B) {
runBench(b, 100)
}
func BenchmarkTrace_1000(b *testing.B) {
runBench(b, 1000)
}
func BenchmarkTrace_10000(b *testing.B) {
runBench(b, 10000)
}
func BenchmarkNewAndFinish(b *testing.B) {
for i := 0; i < b.N; i++ {
tr := New("bench", "test")
tr.Finish()
}
}
func BenchmarkPrintf(b *testing.B) {
tr := New("bench", "test")
defer tr.Finish()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Keep this without any formatting, so we measure our code instead of
// the performance of fmt.Sprintf.
tr.Printf("this is printf")
}
}
|