From 5ec37e9339e8292ed5db82d91a9c2d4f0d5454e0 Mon Sep 17 00:00:00 2001 From: gaowenju Date: Tue, 2 Jan 2024 14:19:52 +0800 Subject: [PATCH] test: add benchmark for ValidHeaderFieldValueTable --- internal/bytesconv/bytesconv_timing_test.go | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 internal/bytesconv/bytesconv_timing_test.go diff --git a/internal/bytesconv/bytesconv_timing_test.go b/internal/bytesconv/bytesconv_timing_test.go new file mode 100644 index 000000000..4e0e27ba9 --- /dev/null +++ b/internal/bytesconv/bytesconv_timing_test.go @@ -0,0 +1,36 @@ +package bytesconv + +import ( + "golang.org/x/net/http/httpguts" + "testing" +) + +func BenchmarkValidHeaderFiledValueTable(b *testing.B) { + // Test all characters + allBytes := make([]string, 0) + for i := 0; i < 256; i++ { + allBytes = append(allBytes, string([]byte{byte(i)})) + } + + for i := 0; i < b.N; i++ { + for _, s := range allBytes { + _ = httpguts.ValidHeaderFieldValue(s) + } + } +} + +func BenchmarkValidHeaderFiledValueTableHertz(b *testing.B) { + // Test all characters + allBytes := make([]byte, 0) + for i := 0; i < 256; i++ { + allBytes = append(allBytes, byte(i)) + } + + for i := 0; i < b.N; i++ { + for _, s := range allBytes { + _ = func() bool { + return ValidHeaderFieldValueTable[s] != 0 + }() + } + } +}