github.com/cloudwego/hertz@v0.9.3/internal/bytesconv/bytesconv_timing_test.go (about) 1 /* 2 * Copyright 2024 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package bytesconv 18 19 import ( 20 "strings" 21 "testing" 22 ) 23 24 // For test only, but it will import golang.org/x/net/http. 25 // So comment out all this code. Keep this for the full context. 26 //func BenchmarkValidHeaderFiledValueTable(b *testing.B) { 27 // // Test all characters 28 // allBytes := make([]string, 0) 29 // for i := 0; i < 256; i++ { 30 // allBytes = append(allBytes, string([]byte{byte(i)})) 31 // } 32 // 33 // for i := 0; i < b.N; i++ { 34 // for _, s := range allBytes { 35 // _ = httpguts.ValidHeaderFieldValue(s) 36 // } 37 // } 38 //} 39 40 func BenchmarkValidHeaderFiledValueTableHertz(b *testing.B) { 41 // Test all characters 42 allBytes := make([]byte, 0) 43 for i := 0; i < 256; i++ { 44 allBytes = append(allBytes, byte(i)) 45 } 46 47 for i := 0; i < b.N; i++ { 48 for _, s := range allBytes { 49 _ = func() bool { 50 return ValidHeaderFieldValueTable[s] != 0 51 }() 52 } 53 } 54 } 55 56 func BenchmarkNewlineToSpace(b *testing.B) { 57 // Test all characters 58 allBytes := make([]byte, 0) 59 for i := 0; i < 256; i++ { 60 allBytes = append(allBytes, byte(i)) 61 } 62 headerNewlineToSpace := strings.NewReplacer("\n", " ", "\r", " ") 63 64 for i := 0; i < b.N; i++ { 65 _ = headerNewlineToSpace.Replace(string(allBytes)) 66 } 67 } 68 69 func BenchmarkNewlineToSpaceHertz01(b *testing.B) { 70 // Test all characters 71 allBytes := make([]byte, 0) 72 for i := 0; i < 256; i++ { 73 allBytes = append(allBytes, byte(i)) 74 } 75 76 for i := 0; i < b.N; i++ { 77 filteredVal := make([]byte, 0, len(allBytes)) 78 for i := 0; i < len(allBytes); i++ { 79 filteredVal = append(filteredVal, NewlineToSpaceTable[allBytes[i]]) 80 } 81 _ = filteredVal 82 } 83 } 84 85 func BenchmarkNewlineToSpaceHertz02(b *testing.B) { 86 // Test all characters 87 allBytes := make([]byte, 0) 88 for i := 0; i < 256; i++ { 89 allBytes = append(allBytes, byte(i)) 90 } 91 92 for i := 0; i < b.N; i++ { 93 filteredVal := make([]byte, len(allBytes)) 94 copy(filteredVal, allBytes) 95 for ii := 0; ii < len(allBytes); ii++ { 96 filteredVal[ii] = NewlineToSpaceTable[filteredVal[ii]] 97 } 98 } 99 }