github.com/m3db/m3@v1.5.0/src/metrics/carbon/parser_benchmark_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package carbon 22 23 import ( 24 "fmt" 25 "math" 26 "regexp" 27 "strconv" 28 "strings" 29 "testing" 30 "time" 31 ) 32 33 var ( 34 testCarbonLine = []byte("foo.bar.baz 0 1441317285") 35 testCarbonLineSpaces = []byte("foo.bar.baz 0 1441317285") 36 ) 37 38 var ( 39 // nolint: gosimple 40 reCarbon = regexp.MustCompile("(?i)^([^\\s]+)\\s+(-?[0-9\\.]+|\\-?nan)\\s+([0-9]+)\\s*$") 41 ) 42 43 func BenchmarkParse(b *testing.B) { 44 for n := 0; n < b.N; n++ { 45 Parse(testCarbonLine) 46 } 47 } 48 49 func BenchmarkParseSpaces(b *testing.B) { 50 for n := 0; n < b.N; n++ { 51 Parse(testCarbonLineSpaces) 52 } 53 } 54 55 func BenchmarkRegex(b *testing.B) { 56 for n := 0; n < b.N; n++ { 57 regexParse(string(testCarbonLine)) 58 } 59 } 60 61 func BenchmarkParseName(b *testing.B) { 62 for i := 0; i < b.N; i++ { 63 ParseName(testCarbonLine) 64 } 65 } 66 67 func BenchmarkParsePacket(b *testing.B) { 68 var test string 69 for i := 0; i < 10; i++ { 70 test += string(testCarbonLine) + "\n" 71 } 72 testBytes := []byte(test) 73 b.ResetTimer() 74 for i := 0; i < b.N; i++ { 75 ParsePacket(testBytes) 76 } 77 } 78 79 // regexParse parses a carbon line into the corresponding parts using regex 80 func regexParse(line string) (name string, timestamp time.Time, value float64, err error) { 81 parts := reCarbon.FindStringSubmatch(line) 82 if len(parts) == 0 { 83 err = errInvalidLine 84 return 85 } 86 87 name = parts[1] 88 tsInSecs, err := strconv.ParseInt(parts[3], 10, 64) 89 if err != nil { 90 err = fmt.Errorf("invalid timestamp %s: %v", parts[3], err) 91 return 92 } 93 94 timestamp = time.Unix(tsInSecs, 0) 95 if strings.ToLower(parts[2]) == "-nan" || strings.ToLower(parts[2]) == "nan" { 96 value = math.NaN() 97 } else { 98 value, err = strconv.ParseFloat(parts[2], 64) 99 } 100 101 return 102 }