measurement: count runes when stripping the plural suffix in sniffUnit - #1019
Merged
Conversation
sniffUnit strips a trailing "s" only when the unit is longer than two
characters, but len() counts bytes. The micro sign spelling registered
as an alias of the microsecond unit,
{"us", []string{"μs", "us", "microsecond"}, float64(time.Microsecond)},
is two runes and three bytes, so the guard fires and the alias is
trimmed to "μ", which matches nothing. The alias has therefore never
resolved.
Three consequences, all silent:
Scale(2000, "μs", "ms") -> (2000, "ms") no scaling applied
pprof -unit=μs -> prints 0 unknown toUnit falls back to seconds
merging a μs profile -> incompatible types: {cpu ms} {cpu μs}
Count runes so a two rune alias is left alone.
aalexand
approved these changes
Aug 25, 2026
aalexand
enabled auto-merge (squash)
August 25, 2026 17:10
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1019 +/- ##
==========================================
+ Coverage 67.20% 67.38% +0.17%
==========================================
Files 45 45
Lines 7846 7846
==========================================
+ Hits 5273 5287 +14
+ Misses 2133 2115 -18
- Partials 440 444 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The bug
sniffUnitstrips a trailing"s"only when the unit is longer than two characters:len()on a string counts bytes. The micro sign spelling is deliberately registered as an alias of the microsecond unit:{"us", []string{"μs", "us", "microsecond"}, float64(time.Microsecond)},"μs"is two runes but three bytes, becauseμis U+03BC (0xCE 0xBC). So the guard fires, the alias is trimmed to"μ", andfindByAliasmatches nothing. That alias has never resolved.Why it matters
sniffUnitis the entry point for every unit lookup, so the failure shows up three ways, all silent:Wrong numbers from
Scale. With noUnitTypeclaiming"μs",Scalefalls through to the non-interesting-unit path and returns the value unchanged while still attaching the target unit's name:-unit=μsprints zero.cfg.Unitreachesmeasurement.Scale(v, o.SampleUnit, o.OutputUnit)ininternal/report/report.go. An unrecognizedtoUnitfalls back to the time type's default of seconds, so a 2.5 ms sample scales to0.0025andScaledLabeltruncates it:Profile merging fails.
compatibleValueTypesasks whether both units sniff to the sameUnitType. Since"μs"sniffs to nothing:The change
Count runes instead of bytes, so a two-rune alias is left alone.
I considered simply dropping the
"μs"alias instead, but the rune count is the more faithful reading: the alias was clearly written on purpose, and counting runes also fixes thetoUnitdirection andcompatibleValueTypes, which removing the alias would not.Tests
Four cases added to the existing
TestScaletable, plus a newTestCommonValueTypecovering the merge path. Without the change:With it, the package passes.
gofmt -l internal/measurement/andgo vet ./internal/measurement/are clean.Full
go test ./...matches a clean checkout: the only failures are pre-existing and environmental on my machine (internal/binutilsTestPEFileandinternal/reportTestPrintAssemblyErrorMessage, both becausenm/objdumpare not on PATH here). No new failures.