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

[ENHANCEMENT] TSDB: Faster exemplars #14080

Merged
merged 6 commits into from
May 30, 2024

Conversation

bboreham
Copy link
Member

Mostly to change from a slice of pointers to a slice of exemplar entries.
Since the data structure is a circular buffer, every entry will be in-use anyway, except at startup before the buffer fills for the first time.

Also:

  • Reduce map lookups on exemplar index
  • Re-use buffer in Resize(), saves having to zero it every time.
  • Save map lookup on validation.

Couple of changes to the benchmarks, to make them more realistic. One exemplar per series is not a typical workload.
BenchmarkResizeExemplars shows 4x more memory, because the memory before resize is not counted by the benchmark.

Benchmarks:

goos: darwin
goarch: arm64
pkg: github.com/prometheus/prometheus/tsdb
                                           │  before.txt   │             after.txt              │
                                           │    sec/op     │   sec/op     vs base               │
AddExemplar/10000/1000-8                     1317.5µ ±  2%   749.0µ ± 1%  -43.15% (p=0.002 n=6)
AddExemplar/100000/1000-8                    13.076m ±  1%   7.412m ± 2%  -43.32% (p=0.002 n=6)
AddExemplar/1000000/1000-8                   129.91m ±  0%   73.86m ± 2%  -43.15% (p=0.002 n=6)
AddExemplar/10000/10000-8                    1189.6µ ±  1%   707.9µ ± 2%  -40.50% (p=0.002 n=6)
AddExemplar/100000/10000-8                   13.000m ±  0%   7.449m ± 1%  -42.70% (p=0.002 n=6)
AddExemplar/1000000/10000-8                  132.26m ±  1%   75.30m ± 1%  -43.06% (p=0.002 n=6)
AddExemplar/10000/100000-8                   1201.4µ ±  1%   703.0µ ± 2%  -41.49% (p=0.002 n=6)
AddExemplar/100000/100000-8                  11.699m ±  0%   6.849m ± 2%  -41.45% (p=0.002 n=6)
AddExemplar/1000000/100000-8                 131.36m ±  0%   75.10m ± 2%  -42.83% (p=0.002 n=6)
ResizeExemplars/grow-100000-to-200000-8       2.858m ±  1%   2.152m ± 3%  -24.70% (p=0.002 n=6)
ResizeExemplars/shrink-100000-to-50000-8     1407.2µ ±  1%   795.8µ ± 2%  -43.45% (p=0.002 n=6)
ResizeExemplars/grow-1000000-to-2000000-8     33.62m ± 10%   21.65m ± 1%  -35.59% (p=0.002 n=6)
ResizeExemplars/shrink-1000000-to-500000-8   13.785m ±  1%   7.575m ± 0%  -45.05% (p=0.002 n=6)
geomean                                       10.36m         6.110m       -41.00%

                                           │   before.txt   │               after.txt               │
                                           │      B/op      │     B/op       vs base                │
AddExemplar/10000/1000-8                      72.235Ki ± 0%    9.734Ki ± 0%   -86.52% (p=0.002 n=6)
AddExemplar/100000/1000-8                     161.30Ki ± 0%    98.80Ki ± 0%   -38.75% (p=0.002 n=6)
AddExemplar/1000000/1000-8                    1075.4Ki ± 0%   1012.9Ki ± 0%    -5.81% (p=0.002 n=6)
AddExemplar/10000/10000-8                    634.743Ki ± 0%    9.740Ki ± 0%   -98.47% (p=0.002 n=6)
AddExemplar/100000/10000-8                    723.80Ki ± 0%    98.80Ki ± 0%   -86.35% (p=0.002 n=6)
AddExemplar/1000000/10000-8                   1637.9Ki ± 0%   1012.9Ki ± 0%   -38.16% (p=0.002 n=6)
AddExemplar/10000/100000-8                   634.741Ki ± 0%    9.734Ki ± 0%   -98.47% (p=0.002 n=6)
AddExemplar/100000/100000-8                  6348.85Ki ± 0%    98.80Ki ± 0%   -98.44% (p=0.002 n=6)
AddExemplar/1000000/100000-8                  7262.9Ki ± 0%   1012.9Ki ± 0%   -86.05% (p=0.002 n=6)
ResizeExemplars/grow-100000-to-200000-8        1.984Mi ± 0%   11.140Mi ± 0%  +461.49% (p=0.002 n=6)
ResizeExemplars/shrink-100000-to-50000-8       511.9Ki ± 0%   2855.9Ki ± 0%  +457.92% (p=0.002 n=6)
ResizeExemplars/grow-1000000-to-2000000-8      22.32Mi ± 0%   113.87Mi ± 0%  +410.07% (p=0.002 n=6)
ResizeExemplars/shrink-1000000-to-500000-8     5.623Mi ± 0%   28.506Mi ± 0%  +406.93% (p=0.002 n=6)
geomean                                        1.291Mi         493.3Ki        -62.69%

                                           │  before.txt   │              after.txt               │
                                           │   allocs/op   │  allocs/op   vs base                 │
AddExemplar/10000/1000-8                       1499.0 ± 0%    499.0 ± 0%  -66.71% (p=0.002 n=6)
AddExemplar/100000/1000-8                      5.999k ± 0%   4.999k ± 0%  -16.67% (p=0.002 n=6)
AddExemplar/1000000/1000-8                     51.00k ± 0%   50.00k ± 0%   -1.96% (p=0.002 n=6)
AddExemplar/10000/10000-8                     10499.0 ± 0%    499.0 ± 0%  -95.25% (p=0.002 n=6)
AddExemplar/100000/10000-8                    14.999k ± 0%   4.999k ± 0%  -66.67% (p=0.002 n=6)
AddExemplar/1000000/10000-8                    60.00k ± 0%   50.00k ± 0%  -16.67% (p=0.002 n=6)
AddExemplar/10000/100000-8                    10499.0 ± 0%    499.0 ± 0%  -95.25% (p=0.002 n=6)
AddExemplar/100000/100000-8                  104.999k ± 0%   4.999k ± 0%  -95.24% (p=0.002 n=6)
AddExemplar/1000000/100000-8                  150.00k ± 0%   50.00k ± 0%  -66.67% (p=0.002 n=6)
ResizeExemplars/grow-100000-to-200000-8        1.004k ± 0%   1.004k ± 0%        ~ (p=1.000 n=6) ¹
ResizeExemplars/shrink-100000-to-50000-8        504.0 ± 0%    504.0 ± 0%        ~ (p=1.000 n=6) ¹
ResizeExemplars/grow-1000000-to-2000000-8      10.00k ± 0%   10.00k ± 0%        ~ (p=1.000 n=6) ¹
ResizeExemplars/shrink-1000000-to-500000-8     5.004k ± 0%   5.004k ± 0%        ~ (p=1.000 n=6) ¹
geomean                                        10.47k        3.905k       -62.70%
¹ all samples are equal

@bboreham bboreham requested review from codesome and removed request for jesusvazquez May 11, 2024 16:53
Copy link
Member

@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.

Very nice.

Copy link
Member

@codesome codesome left a comment

Choose a reason for hiding this comment

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

LGTM. Can you fix the conflict?

Test with different amounts of capacity and exemplars, so that sometimes
new exemplars are evicting older exemplars.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
One exemplar per series is not a typical workload. Make it the same as
`BenchmarkAddExemplar`.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
In many cases we already have a pointer to the entry.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Saves memory and effort.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This saves having to zero the buffer every time.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Goes faster.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
@bboreham
Copy link
Member Author

Rebased to fix conflict.

@codesome codesome merged commit e47474d into prometheus:main May 30, 2024
25 checks passed
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.

None yet

3 participants