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

perf: optimize label sanitization #95

Merged
merged 2 commits into from
Jun 30, 2023

Conversation

kruskall
Copy link
Member

The following code was used to microbenchmark the sanitizeLabel function:

package modelpb

import "testing"

func BenchmarkSanitize(b *testing.B) {
        small := "foobar"
        l := "foobarfoobarfoobarfoobarfoobarfoobarfoobar"
        b.Run("small", func(b *testing.B) {
                for i := 0; i < b.N; i++ {
                        sanitizeLabelKey(small)
                }
        })
        b.Run("l", func(b *testing.B) {
                for i := 0; i < b.N; i++ {
                        sanitizeLabelKey(l)
                }
        })
}

Benchmarks show a performance improvement for both small and large label keys:

                  │  before.txt  │              after.txt              │
                  │    sec/op    │   sec/op     vs base                │
Sanitize/small-20   16.100n ± 1%   9.191n ± 1%  -42.91% (p=0.000 n=10)
Sanitize/l-20        81.55n ± 1%   29.35n ± 2%  -64.01% (p=0.000 n=10)
geomean              36.23n        16.42n       -54.67%

                  │  before.txt  │              after.txt              │
                  │     B/op     │    B/op     vs base                 │
Sanitize/small-20   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Sanitize/l-20       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                  │  before.txt  │              after.txt              │
                  │  allocs/op   │ allocs/op   vs base                 │
Sanitize/small-20   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Sanitize/l-20       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹

Closes #49

Benchmarks show a performance improvement for both small and large label keys:

                  │  before.txt  │              after.txt              │
                  │    sec/op    │   sec/op     vs base                │
Sanitize/small-20   16.100n ± 1%   9.191n ± 1%  -42.91% (p=0.000 n=10)
Sanitize/l-20        81.55n ± 1%   29.35n ± 2%  -64.01% (p=0.000 n=10)
geomean              36.23n        16.42n       -54.67%

                  │  before.txt  │              after.txt              │
                  │     B/op     │    B/op     vs base                 │
Sanitize/small-20   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Sanitize/l-20       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                  │  before.txt  │              after.txt              │
                  │  allocs/op   │ allocs/op   vs base                 │
Sanitize/small-20   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
Sanitize/l-20       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
@kruskall kruskall changed the title Perf/optimize labels perf: optimize label sanitisation Jun 30, 2023
@kruskall kruskall changed the title perf: optimize label sanitisation perf: optimize label sanitization Jun 30, 2023
@kruskall kruskall requested a review from a team June 30, 2023 13:08
@kruskall kruskall merged commit 60568a2 into elastic:main Jun 30, 2023
@kruskall kruskall deleted the perf/optimize-labels branch June 30, 2023 13:27
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.

investigate sanitizelabel optimization
2 participants