package benchunit
import "golang.org/x/perf/benchunit"
Package benchunit manipulates benchmark units and formats numbers in those units.
Index ¶
- Variables
- func Scale(val float64, cls Class) string
- func Tidy(value float64, unit string) (tidiedValue float64, tidiedUnit string)
- type Class
- type Scaler
Variables ¶
var NoOpScaler = Scaler{-1, 1, ""}
NoOpScaler is a Scaler that formats numbers with the smallest number of digits necessary to capture the exact value, and no prefix. This is intended for when the output will be consumed by another program, such as when producing CSV format.
Functions ¶
func Scale ¶
Scale formats val using at least three significant digits, appending an SI or binary prefix. See Scaler.Format for details.
func Tidy ¶
Tidy normalizes a value with a (possibly pre-scaled) unit into base units. For example, if unit is "ns" or "MB", it will re-scale the value to "sec" or "B" units, respectively. It returns the re-scaled value and its new unit. If the value is already in base units, it does nothing.
Types ¶
type Class ¶
type Class int
A Class specifies what class of unit prefixes are in use.
const ( // Decimal indicates values of a given unit should be scaled // by powers of 1000. Decimal units use the International // System of Units SI prefixes, such as "k", and "M". Decimal Class = iota // Binary indicates values of a given unit should be scaled by // powers of 1024. Binary units use the International // Electrotechnical Commission (IEC) binary prefixes, such as // "Ki" and "Mi". Binary )
func ClassOf ¶
ClassOf returns the Class of unit. If unit contains some measure of bytes in the numerator, this is Binary. Otherwise, it is Decimal.
func (Class) String ¶
type Scaler ¶
type Scaler struct { Prec int // Digits after the decimal point Factor float64 // Unscaled value of 1 Prefix (e.g., 1 k => 1000) Prefix string // Unit prefix ("k", "M", "Ki", etc) }
A Scaler represents a scaling factor for a number and its scientific representation.
func CommonScale ¶
CommonScale returns a common Scaler to apply to all values in vals. This scale will show at least three significant digits for every value.
func (Scaler) Format ¶
Format formats val and appends the unit prefix according to the given scale. For example, if the Scaler has class Decimal, Format(123456789) returns "123.4M".
If the value has units, be sure to tidy it first (see Tidy). Otherwise, this can result in nonsense units when the result is displayed with its units; for example, representing 123456789 ns as 123.4M ns ("megananoseconds", aka milliseconds).
Source Files ¶
parse.go scale.go tidy.go
- Version
- v0.0.0-20250414141303-3fc2b901edf3 (latest)
- Published
- Apr 14, 2025
- Platform
- linux/amd64
- Imports
- 6 packages
- Last checked
- 1 month ago –
Tools for package owners.