Benchmarking with details

func BenchmarkReport(b *testing.B) {
runtime.GC()
for i := 0; i < b.N; i++ {
r := fmt.Sprintf("hello, world %d", 123)
runtime.KeepAlive(r)
}
}
BenchmarkReport-32      20000000               107 ns/op

github.com/loov/hrtime

Histogram

func main() {
bench := hrtime.NewBenchmark(20000000)
for bench.Next() {
r := fmt.Sprintf("hello, world %d", 123)
runtime.KeepAlive(r)
}
fmt.Println(bench.Histogram(10))
}
avg 372ns;  min 300ns;  p50 400ns;  max 295µs;
p90 400ns; p99 500ns; p999 1.8µs; p9999 4.3µs;
300ns [ 7332554] ███████████████████████
400ns [12535735] ████████████████████████████████████████
600ns [ 18955]
800ns [ 2322]
1µs [ 20413]
1.2µs [ 34854]
1.4µs [ 25096]
1.6µs [ 10009]
1.8µs [ 4688]
2µs+[ 15374]
func main() {
bench := hrtime.NewBenchmark(20000000)
var back [1024]byte
for bench.Next() {
buffer := back[:0]
buffer = append(buffer, []byte("hello, world ")...)
buffer = strconv.AppendInt(buffer, 123, 10)
runtime.KeepAlive(buffer)
}
fmt.Println(bench.Histogram(10))
}
avg 267ns;  min 200ns;  p50 300ns;  max 216µs;
p90 300ns; p99 300ns; p999 1.1µs; p9999 3.6µs;
200ns [ 7211285] ██████████████████████▌
300ns [12658260] ████████████████████████████████████████
400ns [ 81076]
500ns [ 3226]
600ns [ 343]
700ns [ 136]
800ns [ 729]
900ns [ 8108]
1µs [ 15436]
1.1µs+[ 21401]

Stopwatch

func main() {
const numberOfExperiments = 1000
bench := hrtime.NewStopwatch(numberOfExperiments)
ch := make(chan int32, 10)
wait := make(chan struct{})
// start senders
for i := 0; i < numberOfExperiments; i++ {
go func() {
<-wait
ch <- bench.Start()
}()
}
// start one receiver
go func() {
for lap := range ch {
bench.Stop(lap)
}
}()
// wait for all goroutines to be created
time.Sleep(time.Second)
// release all goroutines at the same time
close(wait)
// wait for all measurements to be completed
bench.Wait()
fmt.Println(bench.Histogram(10))
}

hrtesting

func BenchmarkReport(b *testing.B) {
bench := hrtesting.NewBenchmark(b)
defer bench.Report()
for bench.Next() {
r := fmt.Sprintf("hello, world %d", 123)
runtime.KeepAlive(r)
}
}
BenchmarkReport-32               3000000               427 ns/op
--- BENCH: BenchmarkReport-32
benchmark_old.go:11: 24.5µs₅₀ 24.5µs₉₀ 24.5µs₉₉ N=1
benchmark_old.go:11: 400ns₅₀ 500ns₉₀ 12.8µs₉₉ N=100
benchmark_old.go:11: 400ns₅₀ 500ns₉₀ 500ns₉₉ N=10000
benchmark_old.go:11: 400ns₅₀ 500ns₉₀ 600ns₉₉ N=1000000
benchmark_old.go:11: 400ns₅₀ 500ns₉₀ 500ns₉₉ N=3000000
BenchmarkReport-32   3174566  379 ns/op  400 ns/p50  400 ns/p90 ...

hrplot

func BenchmarkReport(b *testing.B) {
bench := hrtesting.NewBenchmark(b)
defer bench.Report()
defer hrplot.All("all.svg", bench)

runtime.GC()
for bench.Next() {
r := fmt.Sprintf("hello, world %d", 123)
runtime.KeepAlive(r)
}
}

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Spark: Streaming Datasets

A Tale of BFS: Going Parallel

Apigee: Introduction to OAuth 2.0 Grant Types

Client Credentials Image

Create Single Payload Using Multiple Responses.

SMS & eFax Guru

Creating Forms Using Survey Monkey

4 Easy steps to load configuration with pure-config

Knoldus-blog-footer-image

popshop customer care number/8584892730//8584892730/popshop customer care…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Egon Elbre

Egon Elbre

More from Medium

Gin 101: Enable CORS

Creating a Linkerd Controller in Golang

Authentication and Authorization (OpenID Connect)of Go Rest Api’s using an open-source IAM called…

HTTP Content Negotiation in Golang reverse proxy