From eebd8f51e8f358575bb5fe2867c96a8fe4605ca7 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 14 Feb 2017 17:06:57 -0500 Subject: [PATCH] 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 --- src/mime/type_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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) + } + } + }) + }) + } +} -- 2.50.0