Skip to content

Add synthetic-trace allocation benchmark for tag caching - #180

Draft
th0114nd wants to merge 3 commits into
worktree-gostats-taghashfrom
worktree-gostats-taghash-bench
Draft

Add synthetic-trace allocation benchmark for tag caching#180
th0114nd wants to merge 3 commits into
worktree-gostats-taghashfrom
worktree-gostats-taghash-bench

Conversation

@th0114nd

@th0114nd th0114nd commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Stacked on Memoize tag-based Scope/Counter/Gauge/Timer lookups #179. Adds BenchmarkSyntheticTrace(Parallel), BenchmarkSyntheticTracePooled(Parallel), and BenchmarkSyntheticTracePerInstance(Pooled) to stats_synthetic_bench_test.go.
  • The trace simulates a request-serving service holding a long-lived root Scope but re-deriving nested scopes and tagged Counter/Gauge/Timer handles inline on every call - the exact pattern that made gostats' own key-computation path show up heavily under profiling.
  • Pooled variants reuse the same map[string]string tag sets across calls instead of allocating a fresh literal each time, isolating gostats' own internal allocations from caller-side map-literal cost.
  • NewPerInstanceCounter is benchmarked separately as a control, since it isn't memoized by scopeCache - included so the delta is attributable to the cached paths only, not the mixed workload.
  • Pure test-code addition, no production files touched.

Results (benchstat, main vs #179, n=6)

Benchmark time allocs/op
SyntheticTrace -58.8% 48 → 18
SyntheticTracePooled -65.0% 30 → 0
SyntheticTracePerInstance (control) ~0% (noise) unchanged

Test plan

  • go build ./..., go vet ./..., gofmt -l . clean
  • go test ./... -race -count=1 passing
  • go test -bench . -benchmem run against both main and this stack to produce the table above

th0114nd and others added 2 commits July 30, 2026 10:34
Adds BenchmarkSyntheticTrace(Parallel), simulating a service that holds a
long-lived root Scope but re-derives nested scopes and tagged Counter/Gauge/
Timer handles inline on every call (the pattern that made gostats' own
joinScopes/MergeTags/Serialize path show up heavily under profiling). Meant
to be run against both this commit and its parent to measure the allocation
delta from "Memoize tag-based Scope/Counter/Gauge/Timer lookups".

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
BenchmarkSyntheticTracePooled(Parallel) reuses one map[string]string per
distinct tag set (via newPooledTags) across every call instead of building a
fresh literal inline each time, removing the caller-side map allocations
from the measurement. This isolates gostats' own internal allocations - the
scopeCache path added in "Memoize tag-based Scope/Counter/Gauge/Timer
lookups" - from the caller-side cost BenchmarkSyntheticTrace also reports.

NewPerInstanceCounter is pulled out of syntheticTrace/syntheticTracePooled
into its own BenchmarkSyntheticTracePerInstance(Pooled) pair: it isn't
memoized by scopeCache, so mixing it into the main trace masked the true
0-allocs/op steady state of the calls that are cached, and gave a misleading
read on what "internal" cost remains. Benchmarked alone, it's unchanged
between the pre- and post-memoization revisions, as expected for an
untouched path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@th0114nd th0114nd changed the title worktree gostats taghash bench Add synthetic-trace allocation benchmark for tag caching Jul 30, 2026
Use a generic "app" root scope and generic service wording instead of
naming a specific internal service.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@th0114nd
th0114nd marked this pull request as draft July 31, 2026 15:52
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.

1 participant