Skip to content

Instantly share code, notes, and snippets.

@freeformz
Last active January 13, 2017 23:01
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 freeformz/91106f99bc9f52a29fa4fef6802183f1 to your computer and use it in GitHub Desktop.
Save freeformz/91106f99bc9f52a29fa4fef6802183f1 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"testing"
"github.com/pressly/chi"
)
type contextKey struct {
name string
}
func (k *contextKey) String() string {
return "my chi context value " + k.name
}
func ContextLevel(lvl int) context.Context {
c := context.WithValue(context.Background(), chi.RouteCtxKey, &chi.Context{})
for i := 0; i < lvl; i++ {
c = context.WithValue(c, &contextKey{fmt.Sprintf("%d", i)}, i)
}
return c
}
func BenchmarkContext0Level(b *testing.B) {
c := ContextLevel(0)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext1Level(b *testing.B) {
c := ContextLevel(1)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext1LevelWithAssertionOK(b *testing.B) {
c := ContextLevel(1)
var ok bool
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, ok = c.Value(chi.RouteCtxKey).(*chi.Context)
}
_ = ok
}
func BenchmarkContext1LevelWithAssertionValue(b *testing.B) {
c := ContextLevel(1)
var v *chi.Context
b.ResetTimer()
for i := 0; i < b.N; i++ {
v, _ = c.Value(chi.RouteCtxKey).(*chi.Context)
}
_ = v
}
func BenchmarkContext2Level(b *testing.B) {
c := ContextLevel(2)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext3Level(b *testing.B) {
c := ContextLevel(3)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext4Level(b *testing.B) {
c := ContextLevel(4)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext5Level(b *testing.B) {
c := ContextLevel(5)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext10Level(b *testing.B) {
c := ContextLevel(10)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext100Level(b *testing.B) {
c := ContextLevel(100)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
func BenchmarkContext1000Level(b *testing.B) {
c := ContextLevel(1000)
var v interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
v = c.Value(chi.RouteCtxKey)
}
_ = v
}
BenchmarkContext0Level-4                                                    	200000000	         9.62 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext1Level-4                                                    	100000000	        14.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext1LevelWithAssertionOK-4                                     	100000000	        14.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext1LevelWithAssertionValue-4                                  	100000000	        13.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext2Level-4                                                    	100000000	        18.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext3Level-4                                                    	100000000	        23.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext4Level-4                                                    	50000000	        28.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext5Level-4                                                    	50000000	        36.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext10Level-4                                                   	20000000	        65.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext100Level-4                                                  	 2000000	       657 ns/op	       0 B/op	       0 allocs/op
BenchmarkContext1000Level-4                                                 	  200000	      7268 ns/op	       0 B/op	       0 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment