From: Bryan C. Mills Date: Tue, 14 Feb 2017 22:06:57 +0000 (-0500) Subject: mime: add benchmarks for TypeByExtension and ExtensionsByType X-Git-Tag: go1.9beta1~1559 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=eebd8f51e8f358575bb5fe2867c96a8fe4605ca7;p=gostls13.git mime: add benchmarks for TypeByExtension and ExtensionsByType These are possible use-cases for sync.Map. Updates golang/go#18177 Change-Id: I5e2a3d1249967c37d3f89a41122bf4a90522db11 Reviewed-on: https://go-review.googlesource.com/36964 Reviewed-by: Ian Lance Taylor --- diff --git a/src/mime/type_test.go b/src/mime/type_test.go index c6c1491a98..e7aef9a196 100644 --- a/src/mime/type_test.go +++ b/src/mime/type_test.go @@ -148,3 +148,43 @@ func TestLookupMallocs(t *testing.T) { t.Errorf("allocs = %v; want 0", n) } } + +func BenchmarkTypeByExtension(b *testing.B) { + initMime() + b.ResetTimer() + + for _, ext := range []string{ + ".html", + ".HTML", + ".unused", + } { + b.Run(ext, func(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + TypeByExtension(ext) + } + }) + }) + } +} + +func BenchmarkExtensionsByType(b *testing.B) { + initMime() + b.ResetTimer() + + for _, typ := range []string{ + "text/html", + "text/html; charset=utf-8", + "application/octet-stream", + } { + b.Run(typ, func(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if _, err := ExtensionsByType(typ); err != nil { + b.Fatal(err) + } + } + }) + }) + } +}