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

pkg/metrics: Tune NewMetrics function #567

Merged
merged 2 commits into from
Oct 29, 2018

Conversation

mxinden
Copy link
Contributor

@mxinden mxinden commented Oct 24, 2018

What this PR does / why we need it:

This patch tunes the NewMetrics function based on optimizations discussed in prometheus/common#148 and #498 (comment) (//CC @beorn7).

  • Use strings.Builder minimizing memory allocations.

  • Use strconv to render float64 in combination with a []byte buffer pool
    instead of fmt.Sprintf reducing memory allocations.

  • Only sort labels of metrics in test framework, not by default (e.g. in
    production).

Benchmark patch:

go get golang.org/x/tools/cmd/benchcmp

git checkout perf-exp~1
go test -v -bench=NewMetric  -run=^$  -benchmem ./pkg/metrics/... > old.txt

git checkout perf-exp
go test -v -bench=NewMetric  -run=^$  -benchmem ./pkg/metrics/... > new.txt

benchcmp old.txt new.txt

benchmark                           old ns/op     new ns/op     delta
BenchmarkNewMetric/value-1-4        2652          612           -76.92%
BenchmarkNewMetric/value-35.7-4     2706          755           -72.10%

benchmark                           old allocs     new allocs     delta
BenchmarkNewMetric/value-1-4        32             6              -81.25%
BenchmarkNewMetric/value-35.7-4     32             6              -81.25%

benchmark                           old bytes     new bytes     delta
BenchmarkNewMetric/value-1-4        1616          528           -67.33%
BenchmarkNewMetric/value-35.7-4     1616          528           -67.33%

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 24, 2018
@k8s-ci-robot k8s-ci-robot requested review from brancz and zouyee October 24, 2018 08:26
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 24, 2018
if len(keys) > 0 {
labels := []string{}
separator := "{"
Copy link
Contributor

Choose a reason for hiding this comment

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

Just minor, but you could write separater := '{' here and use WriteByte below.

Copy link
Contributor

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Just minor things to comment.


return ""
m.WriteString("}")
Copy link
Contributor

Choose a reason for hiding this comment

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

m.writeByte('}') is fundamentally easier.

m.WriteString(keys[i])
m.WriteString("=\"")
escapeString(m, values[i])
m.WriteString("\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

m.writeByte('"') is fundamentally easier.

m.WriteString("=\"")
escapeString(m, values[i])
m.WriteString("\"")
separator = ","
Copy link
Contributor

Choose a reason for hiding this comment

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

And then ',', of course...

@@ -77,8 +91,34 @@ var (
// escapeString replaces '\' by '\\', new line character by '\n', and - if
// includeDoubleQuote is true - '"' by '\"'.
Copy link
Contributor

Choose a reason for hiding this comment

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

This doc comment is not quite correct.

- Use strings.Builder minimizing memory allocations.

- Use strconv to render float64 in combination with a []byte buffer pool
instead of fmt.Sprintf reducing memory allocations.

- Only sort labels of metrics in test framework, not by default (e.g. in
production).

Benchmark patch:

```bash
go get golang.org/x/tools/cmd/benchcmp

git checkout perf-exp~1
go test -v -bench=NewMetric  -run=^$  -benchmem ./pkg/metrics/... > old.txt

git checkout perf-exp
go test -v -bench=NewMetric  -run=^$  -benchmem ./pkg/metrics/... > new.txt

benchcmp old.txt new.txt

benchmark                           old ns/op     new ns/op     delta
BenchmarkNewMetric/value-1-4        2652          612           -76.92%
BenchmarkNewMetric/value-35.7-4     2706          755           -72.10%

benchmark                           old allocs     new allocs     delta
BenchmarkNewMetric/value-1-4        32             6              -81.25%
BenchmarkNewMetric/value-35.7-4     32             6              -81.25%

benchmark                           old bytes     new bytes     delta
BenchmarkNewMetric/value-1-4        1616          528           -67.33%
BenchmarkNewMetric/value-35.7-4     1616          528           -67.33%
```
@mxinden
Copy link
Contributor Author

mxinden commented Oct 29, 2018

Applied all comments @beorn7, thanks for the review.

Also looked into converting WriteString of length two with two WriteByte as mentioned here, but improvement seems not worth it.

@beorn7 @brancz any further comments?

@brancz
Copy link
Member

brancz commented Oct 29, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 29, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brancz, mxinden

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 29, 2018
@k8s-ci-robot k8s-ci-robot merged commit 8eee921 into kubernetes:master Oct 29, 2018

m = m + "\n"
m.WriteString("\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

DIdn't catch this in my first round of review: This could be written as

m.WriteByte('\n')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, sorry about that. Follow up: #572

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants