Reference valyala/fasthttp#1014
Don't forget about collisions problem.
package ctxbench | |
import ( | |
"context" | |
"testing" | |
) | |
var ( | |
set = 1 | |
get interface{} | |
) | |
func Benchmark_strings_map(b *testing.B) { | |
var ctx = make(map[string]interface{}, 100) | |
b.Run("set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
ctx[v] = set | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("overwrite", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
ctx[v] = set | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("get", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
get = ctx[v] | |
} | |
} | |
b.ReportAllocs() | |
}) | |
ctx = make(map[string]interface{}, 100) | |
ctx["one"], ctx["two"], ctx["three"] = 1, 2, 3 | |
b.Run("miss", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
get = ctx[v] | |
} | |
} | |
b.ReportAllocs() | |
}) | |
} | |
func Benchmark_interfaces_map(b *testing.B) { | |
var ctx = make(map[interface{}]interface{}, 100) | |
b.Run("set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
ctx[v] = set | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("overwrite", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
ctx[v] = set | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("get", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
get = ctx[v] | |
} | |
} | |
b.ReportAllocs() | |
}) | |
type ( | |
one struct{} | |
two struct{} | |
three struct{} | |
) | |
ctx = make(map[interface{}]interface{}, 100) | |
ctx[one{}], ctx[two{}], ctx[three{}] = 1, 2, 3 | |
b.Run("miss", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
get = ctx[v] | |
} | |
} | |
b.ReportAllocs() | |
}) | |
} | |
func Benchmark_strings_standard_context(b *testing.B) { | |
// the standard context can't be preallocated | |
var ctx = context.Background() | |
b.Run("set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
ctx = context.WithValue(ctx, v, set) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("overwrite", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
ctx = context.WithValue(ctx, v, set) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("get", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
get = ctx.Value(v) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
ctx = context.Background() | |
ctx = context.WithValue(ctx, "one", 1) | |
ctx = context.WithValue(ctx, "two", 2) | |
ctx = context.WithValue(ctx, "three", 3) | |
b.Run("miss", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allStrings { | |
get = ctx.Value(v) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
} | |
func Benchmark_interfaces_standard_context(b *testing.B) { | |
// the standard context can't be preallocated | |
var ctx = context.Background() | |
b.Run("set", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
ctx = context.WithValue(ctx, v, set) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("overwrite", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
ctx = context.WithValue(ctx, v, set) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
b.Run("get", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
get = ctx.Value(v) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
type ( | |
one struct{} | |
two struct{} | |
three struct{} | |
) | |
ctx = context.Background() | |
ctx = context.WithValue(ctx, one{}, 1) | |
ctx = context.WithValue(ctx, two{}, 2) | |
ctx = context.WithValue(ctx, three{}, 3) | |
b.Run("miss", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, v := range allInterfaces { | |
get = ctx.Value(v) | |
} | |
} | |
b.ReportAllocs() | |
}) | |
} | |
type ( | |
t00Type struct{} | |
t01Type struct{} | |
t02Type struct{} | |
t03Type struct{} | |
t04Type struct{} | |
t05Type struct{} | |
t06Type struct{} | |
t07Type struct{} | |
t08Type struct{} | |
t09Type struct{} | |
t10Type struct{} | |
t11Type struct{} | |
t12Type struct{} | |
t13Type struct{} | |
t14Type struct{} | |
t15Type struct{} | |
t16Type struct{} | |
t17Type struct{} | |
t18Type struct{} | |
t19Type struct{} | |
t20Type struct{} | |
t21Type struct{} | |
t22Type struct{} | |
t23Type struct{} | |
t24Type struct{} | |
t25Type struct{} | |
t26Type struct{} | |
t27Type struct{} | |
t28Type struct{} | |
t29Type struct{} | |
t30Type struct{} | |
t31Type struct{} | |
t32Type struct{} | |
t33Type struct{} | |
t34Type struct{} | |
t35Type struct{} | |
t36Type struct{} | |
t37Type struct{} | |
t38Type struct{} | |
t39Type struct{} | |
t40Type struct{} | |
t41Type struct{} | |
t42Type struct{} | |
t43Type struct{} | |
t44Type struct{} | |
t45Type struct{} | |
t46Type struct{} | |
t47Type struct{} | |
t48Type struct{} | |
t49Type struct{} | |
t50Type struct{} | |
t51Type struct{} | |
t52Type struct{} | |
t53Type struct{} | |
t54Type struct{} | |
t55Type struct{} | |
t56Type struct{} | |
t57Type struct{} | |
t58Type struct{} | |
t59Type struct{} | |
t60Type struct{} | |
t61Type struct{} | |
t62Type struct{} | |
t63Type struct{} | |
t64Type struct{} | |
t65Type struct{} | |
t66Type struct{} | |
t67Type struct{} | |
t68Type struct{} | |
t69Type struct{} | |
t70Type struct{} | |
t71Type struct{} | |
t72Type struct{} | |
t73Type struct{} | |
t74Type struct{} | |
t75Type struct{} | |
t76Type struct{} | |
t77Type struct{} | |
t78Type struct{} | |
t79Type struct{} | |
t80Type struct{} | |
t81Type struct{} | |
t82Type struct{} | |
t83Type struct{} | |
t84Type struct{} | |
t85Type struct{} | |
t86Type struct{} | |
t87Type struct{} | |
t88Type struct{} | |
t89Type struct{} | |
t90Type struct{} | |
t91Type struct{} | |
t92Type struct{} | |
t93Type struct{} | |
t94Type struct{} | |
t95Type struct{} | |
t96Type struct{} | |
t97Type struct{} | |
t98Type struct{} | |
t99Type struct{} | |
) | |
var allInterfaces = []interface{}{ | |
t00Type{}, | |
t01Type{}, | |
t02Type{}, | |
t03Type{}, | |
t04Type{}, | |
t05Type{}, | |
t06Type{}, | |
t07Type{}, | |
t08Type{}, | |
t09Type{}, | |
t10Type{}, | |
t11Type{}, | |
t12Type{}, | |
t13Type{}, | |
t14Type{}, | |
t15Type{}, | |
t16Type{}, | |
t17Type{}, | |
t18Type{}, | |
t19Type{}, | |
t20Type{}, | |
t21Type{}, | |
t22Type{}, | |
t23Type{}, | |
t24Type{}, | |
t25Type{}, | |
t26Type{}, | |
t27Type{}, | |
t28Type{}, | |
t29Type{}, | |
t30Type{}, | |
t31Type{}, | |
t32Type{}, | |
t33Type{}, | |
t34Type{}, | |
t35Type{}, | |
t36Type{}, | |
t37Type{}, | |
t38Type{}, | |
t39Type{}, | |
t40Type{}, | |
t41Type{}, | |
t42Type{}, | |
t43Type{}, | |
t44Type{}, | |
t45Type{}, | |
t46Type{}, | |
t47Type{}, | |
t48Type{}, | |
t49Type{}, | |
t50Type{}, | |
t51Type{}, | |
t52Type{}, | |
t53Type{}, | |
t54Type{}, | |
t55Type{}, | |
t56Type{}, | |
t57Type{}, | |
t58Type{}, | |
t59Type{}, | |
t60Type{}, | |
t61Type{}, | |
t62Type{}, | |
t63Type{}, | |
t64Type{}, | |
t65Type{}, | |
t66Type{}, | |
t67Type{}, | |
t68Type{}, | |
t69Type{}, | |
t70Type{}, | |
t71Type{}, | |
t72Type{}, | |
t73Type{}, | |
t74Type{}, | |
t75Type{}, | |
t76Type{}, | |
t77Type{}, | |
t78Type{}, | |
t79Type{}, | |
t80Type{}, | |
t81Type{}, | |
t82Type{}, | |
t83Type{}, | |
t84Type{}, | |
t85Type{}, | |
t86Type{}, | |
t87Type{}, | |
t88Type{}, | |
t89Type{}, | |
t90Type{}, | |
t91Type{}, | |
t92Type{}, | |
t93Type{}, | |
t94Type{}, | |
t95Type{}, | |
t96Type{}, | |
t97Type{}, | |
t98Type{}, | |
t99Type{}, | |
} | |
var ( | |
s00Val = "00" | |
s01Val = "01" | |
s02Val = "02" | |
s03Val = "03" | |
s04Val = "04" | |
s05Val = "05" | |
s06Val = "06" | |
s07Val = "07" | |
s08Val = "08" | |
s09Val = "09" | |
s10Val = "10" | |
s11Val = "11" | |
s12Val = "12" | |
s13Val = "13" | |
s14Val = "14" | |
s15Val = "15" | |
s16Val = "16" | |
s17Val = "17" | |
s18Val = "18" | |
s19Val = "19" | |
s20Val = "20" | |
s21Val = "21" | |
s22Val = "22" | |
s23Val = "23" | |
s24Val = "24" | |
s25Val = "25" | |
s26Val = "26" | |
s27Val = "27" | |
s28Val = "28" | |
s29Val = "29" | |
s30Val = "30" | |
s31Val = "31" | |
s32Val = "32" | |
s33Val = "33" | |
s34Val = "34" | |
s35Val = "35" | |
s36Val = "36" | |
s37Val = "37" | |
s38Val = "38" | |
s39Val = "39" | |
s40Val = "40" | |
s41Val = "41" | |
s42Val = "42" | |
s43Val = "43" | |
s44Val = "44" | |
s45Val = "45" | |
s46Val = "46" | |
s47Val = "47" | |
s48Val = "48" | |
s49Val = "49" | |
s50Val = "50" | |
s51Val = "51" | |
s52Val = "52" | |
s53Val = "53" | |
s54Val = "54" | |
s55Val = "55" | |
s56Val = "56" | |
s57Val = "57" | |
s58Val = "58" | |
s59Val = "59" | |
s60Val = "60" | |
s61Val = "61" | |
s62Val = "62" | |
s63Val = "63" | |
s64Val = "64" | |
s65Val = "65" | |
s66Val = "66" | |
s67Val = "67" | |
s68Val = "68" | |
s69Val = "69" | |
s70Val = "70" | |
s71Val = "71" | |
s72Val = "72" | |
s73Val = "73" | |
s74Val = "74" | |
s75Val = "75" | |
s76Val = "76" | |
s77Val = "77" | |
s78Val = "78" | |
s79Val = "79" | |
s80Val = "80" | |
s81Val = "81" | |
s82Val = "82" | |
s83Val = "83" | |
s84Val = "84" | |
s85Val = "85" | |
s86Val = "86" | |
s87Val = "87" | |
s88Val = "88" | |
s89Val = "89" | |
s90Val = "90" | |
s91Val = "91" | |
s92Val = "92" | |
s93Val = "93" | |
s94Val = "94" | |
s95Val = "95" | |
s96Val = "96" | |
s97Val = "97" | |
s98Val = "98" | |
s99Val = "99" | |
) | |
var allStrings = []string{ | |
s00Val, | |
s01Val, | |
s02Val, | |
s03Val, | |
s04Val, | |
s05Val, | |
s06Val, | |
s07Val, | |
s08Val, | |
s09Val, | |
s10Val, | |
s11Val, | |
s12Val, | |
s13Val, | |
s14Val, | |
s15Val, | |
s16Val, | |
s17Val, | |
s18Val, | |
s19Val, | |
s20Val, | |
s21Val, | |
s22Val, | |
s23Val, | |
s24Val, | |
s25Val, | |
s26Val, | |
s27Val, | |
s28Val, | |
s29Val, | |
s30Val, | |
s31Val, | |
s32Val, | |
s33Val, | |
s34Val, | |
s35Val, | |
s36Val, | |
s37Val, | |
s38Val, | |
s39Val, | |
s40Val, | |
s41Val, | |
s42Val, | |
s43Val, | |
s44Val, | |
s45Val, | |
s46Val, | |
s47Val, | |
s48Val, | |
s49Val, | |
s50Val, | |
s51Val, | |
s52Val, | |
s53Val, | |
s54Val, | |
s55Val, | |
s56Val, | |
s57Val, | |
s58Val, | |
s59Val, | |
s60Val, | |
s61Val, | |
s62Val, | |
s63Val, | |
s64Val, | |
s65Val, | |
s66Val, | |
s67Val, | |
s68Val, | |
s69Val, | |
s70Val, | |
s71Val, | |
s72Val, | |
s73Val, | |
s74Val, | |
s75Val, | |
s76Val, | |
s77Val, | |
s78Val, | |
s79Val, | |
s80Val, | |
s81Val, | |
s82Val, | |
s83Val, | |
s84Val, | |
s85Val, | |
s86Val, | |
s87Val, | |
s88Val, | |
s89Val, | |
s90Val, | |
s91Val, | |
s92Val, | |
s93Val, | |
s94Val, | |
s95Val, | |
s96Val, | |
s97Val, | |
s98Val, | |
s99Val, | |
} |
module ctxbench | |
go 1.16 |
Reference valyala/fasthttp#1014
Don't forget about collisions problem.
go test -benchtime 2s -bench .
goos: linux
goarch: amd64
pkg: ctxbench
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
Benchmark_strings_map/set-4 977221 2432 ns/op 0 B/op 0 allocs/op
Benchmark_strings_map/overwrite-4 986012 2381 ns/op 0 B/op 0 allocs/op
Benchmark_strings_map/get-4 1273322 1897 ns/op 0 B/op 0 allocs/op
Benchmark_strings_map/miss-4 1219234 1980 ns/op 0 B/op 0 allocs/op
Benchmark_interfaces_map/set-4 63344 38618 ns/op 0 B/op 0 allocs/op
Benchmark_interfaces_map/overwrite-4 63654 37307 ns/op 0 B/op 0 allocs/op
Benchmark_interfaces_map/get-4 78903 29970 ns/op 0 B/op 0 allocs/op
Benchmark_interfaces_map/miss-4 573855 3604 ns/op 0 B/op 0 allocs/op
Benchmark_strings_standard_context/set-4 103000 24890 ns/op 6400 B/op 200 allocs/op
Benchmark_strings_standard_context/overwrite-4 213064 22171 ns/op 6400 B/op 200 allocs/op
Benchmark_strings_standard_context/get-4 29756 81099 ns/op 1600 B/op 100 allocs/op
Benchmark_strings_standard_context/miss-4 313033 8694 ns/op 1600 B/op 100 allocs/op
Benchmark_interfaces_standard_context/set-4 94843 22131 ns/op 4800 B/op 100 allocs/op
Benchmark_interfaces_standard_context/overwrite-4 286615 12636 ns/op 4800 B/op 100 allocs/op
Benchmark_interfaces_standard_context/get-4 87330 28342 ns/op 0 B/op 0 allocs/op
Benchmark_interfaces_standard_context/miss-4 1416162 1687 ns/op 0 B/op 0 allocs/op
PASS
ok ctxbench 85.710s