Skip to content

Commit c2f1815

Browse files
obs/ash: add BenchmarkSetWorkState microbenchmark
Add a microbenchmark for the SetWorkState + clearWorkState hot path. This complements the existing end-to-end BenchmarkASH in pkg/bench by isolating the activeWorkStates sync.Map contention that degrades on high-core machines. Varying GOMAXPROCS via -test.cpu shows how per-operation latency scales with core count due to cache coherence traffic on the map's internal mutex. Fixes: #164683 Release note: None Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
1 parent d02da49 commit c2f1815

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

pkg/obs/ash/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ go_test(
4242
name = "ash_test",
4343
srcs = [
4444
"aggregate_test.go",
45+
"bench_test.go",
4546
"buffer_test.go",
4647
"report_test.go",
4748
"sampler_test.go",

pkg/obs/ash/bench_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2026 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package ash
7+
8+
import (
9+
"testing"
10+
11+
"github.com/cockroachdb/cockroach/pkg/roachpb"
12+
)
13+
14+
// BenchmarkSetWorkState measures the per-operation cost of a
15+
// SetWorkState + clearWorkState cycle — the pair of calls every
16+
// registered goroutine makes on the ASH hot path.
17+
//
18+
// activeWorkStates is a global sync.Map. On high-core machines,
19+
// concurrent Stores from different cores cause cache-line
20+
// invalidations on the map's internal mutex and dirty-map pointer,
21+
// degrading throughput as core count grows. This benchmark
22+
// quantifies that degradation by varying GOMAXPROCS via -test.cpu.
23+
// RunParallel spawns one goroutine per core, which is the right
24+
// model: contention scales with cores executing simultaneously,
25+
// not goroutines queued in the scheduler.
26+
//
27+
// See #168289 for the sharded-map proposal to address this.
28+
//
29+
// Example:
30+
//
31+
// ./dev bench pkg/obs/ash -f BenchmarkSetWorkState --test-args='-test.cpu=1,4,16,64'
32+
func BenchmarkSetWorkState(b *testing.B) {
33+
enabled.Store(true)
34+
defer enabled.Store(false)
35+
36+
tenantID := roachpb.MustMakeTenantID(5)
37+
info := WorkloadInfo{WorkloadID: 12345}
38+
39+
b.ReportAllocs()
40+
b.RunParallel(func(pb *testing.PB) {
41+
for pb.Next() {
42+
clear := SetWorkState(tenantID, info, WorkCPU, "BenchmarkOp")
43+
clear()
44+
}
45+
})
46+
47+
// Drain retired states so subsequent benchmarks start clean.
48+
reclaimRetiredWorkStates()
49+
}

0 commit comments

Comments
 (0)