Skip to content

Instantly share code, notes, and snippets.

@SpencerMalone
Forked from bakins/escape_test.go
Last active April 4, 2019 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpencerMalone/18bb987a0e0de3e7de40a23b1a56b561 to your computer and use it in GitHub Desktop.
Save SpencerMalone/18bb987a0e0de3e7de40a23b1a56b561 to your computer and use it in GitHub Desktop.
compare escapeMetricName
package main
import (
"golang.org/x/text/unicode/rangetable"
"regexp"
"testing"
"unicode"
)
var validMetric = rangetable.New([]rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")...)
var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
func convertValidMatricRegex(metricName string) string {
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
metricName = "_" + metricName
}
metricName = illegalCharsRE.ReplaceAllString(metricName, "_")
return metricName
}
func convertValidMatricNew(metricName string) string {
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
metricName = "_" + metricName
}
out := make([]byte, len(metricName))
j := 0
for _, c := range metricName {
if unicode.Is(validMetric, c) {
out[j] = byte(c)
} else {
out[j] = byte('_')
}
j++
}
return string(out[:j])
}
func convertValidMatricHack(metricName string) string {
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
metricName = "_" + metricName
}
out := make([]byte, len(metricName))
j := 0
for _, c := range metricName {
if (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') {
out[j] = byte(c)
} else {
out[j] = byte('_')
}
j++
}
return string(out[:j])
}
func benmarkRegex(b *testing.B, metricName string) {
for n := 0; n < b.N; n++ {
convertValidMatricRegex(metricName)
}
}
func benchmarkNew(b *testing.B, metricName string) {
for n := 0; n < b.N; n++ {
convertValidMatricHack(metricName)
}
}
func benchmarkHack(b *testing.B, metricName string) {
for n := 0; n < b.N; n++ {
convertValidMatricNew(metricName)
}
}
func BenchmarkNewClean(b *testing.B) { benchmarkNew(b, "clean") }
func BenchmarkHackClean(b *testing.B) { benchmarkHack(b, "clean") }
func BenchmarkRegexClean(b *testing.B) { benmarkRegex(b, "clean") }
func BenchmarkNewDigit(b *testing.B) { benchmarkNew(b, "0starts_with_digit") }
func BenchmarkHackDigit(b *testing.B) { benchmarkHack(b, "0starts_with_digit") }
func BenchmarkRegexDigit(b *testing.B) { benmarkRegex(b, "0starts_with_digit") }
func BenchmarkNewDot(b *testing.B) { benchmarkNew(b, "with.dot") }
func BenchmarkHackDot(b *testing.B) { benchmarkHack(b, "with.dot") }
func BenchmarkRegexDot(b *testing.B) { benmarkRegex(b, "with.dot") }
func BenchmarkNewEmoji(b *testing.B) { benchmarkNew(b, "with😱emoji") }
func BenchmarkHackEmoji(b *testing.B) { benchmarkHack(b, "with😱emoji") }
func BenchmarkRegexEmoji(b *testing.B) { benmarkRegex(b, "with😱emoji") }
> go test -bench=. -test.benchmem
goos: darwin
goarch: amd64
BenchmarkNewClean-12 50000000 30.9 ns/op 10 B/op 2 allocs/op
BenchmarkHackClean-12 20000000 60.9 ns/op 10 B/op 2 allocs/op
BenchmarkRegexClean-12 5000000 274 ns/op 32 B/op 3 allocs/op
BenchmarkNewDigit-12 20000000 73.1 ns/op 64 B/op 2 allocs/op
BenchmarkHackDigit-12 10000000 183 ns/op 64 B/op 2 allocs/op
BenchmarkRegexDigit-12 2000000 631 ns/op 112 B/op 4 allocs/op
BenchmarkNewDot-12 50000000 34.1 ns/op 16 B/op 2 allocs/op
BenchmarkHackDot-12 20000000 80.7 ns/op 16 B/op 2 allocs/op
BenchmarkRegexDot-12 5000000 392 ns/op 32 B/op 3 allocs/op
BenchmarkNewEmoji-12 30000000 48.7 ns/op 32 B/op 2 allocs/op
BenchmarkHackEmoji-12 20000000 105 ns/op 32 B/op 2 allocs/op
BenchmarkRegexEmoji-12 3000000 528 ns/op 56 B/op 4 allocs/op
PASS
ok github.com/SpencerMalone/test-runes 21.613s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment