Add synthetic-trace allocation benchmark for tag caching - #180
Draft
th0114nd wants to merge 3 commits into
Draft
Conversation
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>
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
marked this pull request as draft
July 31, 2026 15:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BenchmarkSyntheticTrace(Parallel),BenchmarkSyntheticTracePooled(Parallel), andBenchmarkSyntheticTracePerInstance(Pooled)tostats_synthetic_bench_test.go.map[string]stringtag sets across calls instead of allocating a fresh literal each time, isolating gostats' own internal allocations from caller-side map-literal cost.NewPerInstanceCounteris benchmarked separately as a control, since it isn't memoized byscopeCache- included so the delta is attributable to the cached paths only, not the mixed workload.Results (
benchstat, main vs #179, n=6)Test plan
go build ./...,go vet ./...,gofmt -l .cleango test ./... -race -count=1passinggo test -bench . -benchmemrun against bothmainand this stack to produce the table above