Skip to content

Instantly share code, notes, and snippets.

@zeebo
Last active April 30, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeebo/dda7cfc1df8c3403baedf01583f5bb86 to your computer and use it in GitHub Desktop.
Save zeebo/dda7cfc1df8c3403baedf01583f5bb86 to your computer and use it in GitHub Desktop.
time.Now latency distribution
nothing 50: 11ns 75: 12ns 90: 13ns 99: 22ns 99.9: 169ns 99.99: 253ns 99.999: 15487ns 99.9999: 24362ns
package whatever
import (
"fmt"
"runtime"
"sync"
"testing"
"time"
"github.com/codahale/hdrhistogram"
)
func BenchmarkNow(b *testing.B) {
startNanos := make([]int64, b.N)
endNanos := make([]int64, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
startNanos[i] = time.Now().UnixNano()
endNanos[i] = time.Now().UnixNano()
}
b.StopTimer()
recordLatencyDistribution("nothing", b.N, startNanos, endNanos)
}
func recordLatencyDistribution(name string, count int, startNanos []int64, endNanos []int64) {
histogram := hdrhistogram.New(1, 1000000, 5)
for i := 0; i < count; i++ {
diff := endNanos[i] - startNanos[i]
histogram.RecordValue(diff)
}
fmt.Printf("%s 50: %dns\t75: %dns\t90: %dns\t99: %dns\t99.9: %dns\t99.99: %dns\t99.999: %dns\t99.9999: %dns\n",
name,
histogram.ValueAtQuantile(50),
histogram.ValueAtQuantile(75),
histogram.ValueAtQuantile(90),
histogram.ValueAtQuantile(99),
histogram.ValueAtQuantile(99.9),
histogram.ValueAtQuantile(99.99),
histogram.ValueAtQuantile(99.999),
histogram.ValueAtQuantile(99.9999))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment