Skip to content

Commit

Permalink
metrics: use regexp.MatchString for metric name check (#4047)
Browse files Browse the repository at this point in the history
`(*Regexp).Match` have a string-based equivalent
`(*Regexp).MatchString`. We should use the string version to avoid
unnecessary `[]byte` conversions.

Benchmark:

func BenchmarkMatch(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := compileNameRegex.Match([]byte("my_metric_name1")); !match {
			b.Fail()
		}
	}
}

func BenchmarkMatchString(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if match := compileNameRegex.MatchString("my_metric_name1"); !match {
			b.Fail()
		}
	}
}

Result:

goos: linux
goarch: amd64
pkg: go.k6.io/k6/metrics
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16          	 1457095	       820.7 ns/op	      16 B/op	       1 allocs/op
BenchmarkMatchString-16    	 1664458	       726.6 ns/op	       0 B/op	       0 allocs/op
PASS
coverage: 0.2% of statements
ok  	go.k6.io/k6/metrics	3.983s

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Nov 13, 2024
1 parent 90bb941 commit 13561fa
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
var compileNameRegex = regexp.MustCompile(nameRegexString)

func checkName(name string) bool {
return compileNameRegex.Match([]byte(name))
return compileNameRegex.MatchString(name)
}

// NewMetric returns new metric registered to this registry
Expand Down

0 comments on commit 13561fa

Please sign in to comment.