Skip to content

benchmark: add WithWork benchmarks for realistic CAS contention testing#1970

Open
raeperd wants to merge 4 commits into
prometheus:mainfrom
raeperd:add-withwork-benchmarks
Open

benchmark: add WithWork benchmarks for realistic CAS contention testing#1970
raeperd wants to merge 4 commits into
prometheus:mainfrom
raeperd:add-withwork-benchmarks

Conversation

@raeperd
Copy link
Copy Markdown

@raeperd raeperd commented Mar 25, 2026

Background

PR #1661 added CAS exponential backoff that showed 53-64% improvement in tight-loop benchmarks, but caused 10x latency regression in production at just 4 goroutines (#1748). The existing benchmarks couldn't detect this because real applications do work between metric updates — tight loops create artificial maximum contention where backoff genuinely helps.

Changes

Add "WithWork" benchmarks that perform ~few μs of CPU work between metric operations, simulating real application behavior. Each benchmark runs at concurrency 1/2/4/8:

Benchmark Operation CAS path?
BenchmarkCounterAddWithWork Add(1.5) Yes
BenchmarkCounterIncWithWork Inc() No (baseline)
BenchmarkGaugeAddWithWork Add(1.5) Yes
BenchmarkGaugeSetWithWork Set(1.5) No (baseline)
BenchmarkHistogramObserveWithWork Observe(1.5) Yes (sum)
BenchmarkSummaryObserveWithWork Observe(1.5) Yes (sum)

Counter uses Add(1.5) specifically to hit the CAS loop — integer values take the atomic.AddUint64 fast path and would bypass the code under test.

Work simulation uses 10,000 iterations matching the regression reporter's test in #1748.

Out of scope

benchstat integration into the release process (acceptance criterion 2 of #1759) is left for separate discussion as a process/CI decision.

I'd appreciate any feedback on the approach — happy to adjust the work simulation, concurrency levels, or benchmark structure based on your preferences.

Relates to #1759.

raeperd added 2 commits March 25, 2026 21:40
Add benchmarks that simulate realistic application usage by performing
CPU work between metric operations, covering Counter.Add, Counter.Inc,
Gauge.Add, Gauge.Set, Histogram.Observe, and Summary.Observe at
concurrency levels 1/2/4/8.

These complement the existing tight-loop benchmarks and are designed to
catch regressions like prometheus#1748, where a CAS backoff optimization caused
10x latency spikes in real workloads that the existing benchmarks missed.

Relates to prometheus#1759.

Signed-off-by: raeperd <raeperd117@gmail.com>
Signed-off-by: raeperd <raeperd117@gmail.com>
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