benchunit

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package benchunit manipulates benchmark units and formats numbers in those units.

Index

Constants

This section is empty.

Variables

View Source
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

func Scale(val float64, cls Class) string

Scale formats val using at least three significant digits, appending an SI or binary prefix. See Scaler.Format for details.

func Tidy

func Tidy(value float64, unit string) (tidiedValue float64, tidiedUnit string)

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

func ClassOf(unit string) Class

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

func (c Class) String() 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

func CommonScale(vals []float64, cls Class) Scaler

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

func (s Scaler) Format(val float64) string

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL