Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize BuildFQName function #1665

Merged
merged 1 commit into from
Nov 1, 2024
Merged

Conversation

jkroepke
Copy link
Contributor

The BuildFQName function is used on many exporters multiple times.

The overall benefit might be quite low, but the I'm happy to contribute an optimization.

strings.Builder is available since go 1.10 and should me the current requirements.

% benchstat BenchmarkBuildFQName1.txt BenchmarkBuildFQName2.txt                                             
goos: darwin
goarch: amd64
pkg: github.com/prometheus/client_golang/prometheus
cpu: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
               │ BenchmarkBuildFQName1.txt │      BenchmarkBuildFQName2.txt      │
               │          sec/op           │   sec/op     vs base                │
BuildFQName1-8                 60.43n ± 0%   45.05n ± 0%  -25.44% (p=0.000 n=20)
BuildFQName2-8                 2.310n ± 1%   2.333n ± 1%   +1.00% (p=0.038 n=20)
BuildFQName3-8                 71.14n ± 1%   50.13n ± 0%  -29.53% (p=0.000 n=20)
geomean                        21.49n        17.40n       -19.04%

Go test commands:

go test -bench="BenchmarkBuildFQName" -run=^BenchmarkBuildFQName -count=20 | tee BenchmarkBuildFQName1.txt

Benchmark File:

func BenchmarkBuildFQName1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		prometheus.BuildFQName("node", "", "usage_seconds_total")
	}
}

func BenchmarkBuildFQName2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		prometheus.BuildFQName("node", "cpu", "")
	}
}

func BenchmarkBuildFQName3(b *testing.B) {
	for i := 0; i < b.N; i++ {
		prometheus.BuildFQName("node", "cpu", "usage_seconds_total")
	}
}

Signed-off-by: Jan-Otto Kröpke <[email protected]>
Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful, thanks!

@bwplotka bwplotka merged commit d3f2840 into prometheus:main Nov 1, 2024
10 checks passed
@jkroepke jkroepke deleted the BuildFQName branch November 1, 2024 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants