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

Generator performance #4232

Merged
merged 7 commits into from
Oct 25, 2024
Merged

Generator performance #4232

merged 7 commits into from
Oct 25, 2024

Conversation

mdisibio
Copy link
Contributor

@mdisibio mdisibio commented Oct 24, 2024

What this PR does:
Metrics generator span metrics and service graphs are very popular, but memory usage can be quite high for high-cardinality setups, for example 1+ million active series in a single pod. I noticed some areas for improvement to reduce memory.

This PR contains 2 updates:

(1) on the surface it updates histograms to pre-compute all prometheus labels during creation, instead of during collection time. This way we alloc the labels once instead of on every scrape. This is possible because all of the labels for a specific histogram bucket are fixed, even external labels which are configured via runtime config.

(2) Almost more importantly in mind, it's adding a suite of benchmarks for the generator, which include WAL and non-mock registry. These benchmarks will help us identify more improvements like (1). Skimming through the module I've left several TODOs large and small of next areas to update.

Benchmarks show a large reduction in mem in Collect. Tested this in an internal cluster and it was ~15% total working set savings.

          │  before.txt  │              after.txt              │
          │    sec/op    │    sec/op     vs base               │
PushSpans   1.421m ±  1%   1.441m ±  1%   +1.36% (p=0.002 n=6)
Collect     31.30µ ± 13%   10.34µ ± 14%  -66.96% (p=0.002 n=6)
geomean     210.9µ         122.1µ        -42.13%

          │ before.txt  │             after.txt             │
          │ heap_in_use │ heap_in_use  vs base              │
PushSpans   15.69M ± 1%   15.50M ± 0%  -1.20% (p=0.002 n=6)
Collect     15.68M ± 0%   15.49M ± 0%  -1.20% (p=0.002 n=6)
geomean     15.68M        15.50M       -1.20%

          │  before.txt   │              after.txt              │
          │     B/op      │     B/op      vs base               │
PushSpans   705.8Ki ±  0%   705.9Ki ± 0%        ~ (p=0.310 n=6)
Collect     31.27Ki ± 11%   14.47Ki ± 4%  -53.73% (p=0.002 n=6)
geomean     148.6Ki         101.1Ki       -31.98%

          │  before.txt  │              after.txt              │
          │  allocs/op   │  allocs/op    vs base               │
PushSpans   18.58k ±  0%   18.58k ±  0%        ~ (p=1.000 n=6)
Collect      71.00 ± 11%    21.00 ± 14%  -70.42% (p=0.002 n=6)
geomean     1.149k          624.7        -45.61%

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Copy link
Contributor

@zalegrala zalegrala left a comment

Choose a reason for hiding this comment

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

Looks good. I appreciate the TODOs also.

@@ -129,3 +137,140 @@ func (l testLogger) Log(keyvals ...interface{}) error {
l.t.Log(keyvals...)
return nil
}

func BenchmarkPushSpans(b *testing.B) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I love the full stack benchmark. This is something we might want to do for other components as well.

}

b.StopTimer()
runtime.GC()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why force a GC after the benchmark has been timed? Is this to avoid impact on later benchmarks, or to get accurate memory summary below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case I was trying to see if the benchmarks could help measure inuse memory, so it is recording HeapInUse at the bottom. Without the GC it just keeps growing in between runs of the benchmark (-count=5 for example). So this was an attempt to make the inuse metric more useful.

@mdisibio mdisibio merged commit 2f2a35e into grafana:main Oct 25, 2024
16 checks passed
@zalegrala zalegrala mentioned this pull request Oct 29, 2024
3 tasks
knylander-grafana pushed a commit that referenced this pull request Oct 29, 2024
* todos

* more todos and print inuse stats

* Benchmark report heapinuse, ensure cleanup between benchmarks

* Improve memory usage by changing histograms to precompute all labels for all sub-series instead of during each collection

* changelog
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