throughput.go
throughput.go - Overview
-
Overview This file defines a
throughputFormatter
struct and related functions for formatting throughput values with appropriate units and scaling. -
Detailed Documentation
throughputFormatter
- Purpose: This struct is a formatter for throughput values.
type throughputFormatter struct {
}
NewThroughputFormatter
- Purpose: Creates a new instance of
throughputFormatter
. - Returns: A
Formatter
interface, which is the newthroughputFormatter
instance.
func NewThroughputFormatter() Formatter {
return &throughputFormatter{}
}
(*throughputFormatter) Name
- Purpose: Returns the name of the formatter, which is "throughput".
- Returns: A string, "throughput".
func (*throughputFormatter) Name() string {
return "throughput"
}
simpleCountUnit
- Purpose: Formats a numeric value with scaled units (e.g., K, M, B, T) and a given symbol.
- Parameters:
value
(float64): The numeric value to format.decimals
(*int): A pointer to an integer that specifies the number of decimal places to use. If nil, the number of decimals are determined automatically.symbol
(string): The unit symbol to append to the formatted value.
- Returns: A string, the formatted value with scaled unit and symbol.
func simpleCountUnit(value float64, decimals *int, symbol string) string {
units := []string{"", "K", "M", "B", "T"}
scaler := scaledUnits(1000, units, 0)
return scaler(value, decimals) + " " + symbol
}
(f *throughputFormatter) Format
- Purpose: Formats a throughput value based on the provided unit.
- Parameters:
value
(float64): The numeric value to format.unit
(string): The unit of the value (e.g., "cps", "ops", "reqps").
- Returns: A string, the formatted throughput value. If the unit is not recognized, it returns the value as a string.
func (f *throughputFormatter) Format(value float64, unit string) string {
switch unit {
case "cps":
return simpleCountUnit(value, nil, "c/s")
case "ops":
return simpleCountUnit(value, nil, "op/s")
case "reqps":
return simpleCountUnit(value, nil, "req/s")
case "rps":
return simpleCountUnit(value, nil, "r/s")
case "wps":
return simpleCountUnit(value, nil, "w/s")
case "iops":
return simpleCountUnit(value, nil, "iops")
case "cpm":
return simpleCountUnit(value, nil, "c/m")
case "opm":
return simpleCountUnit(value, nil, "op/m")
case "rpm":
return simpleCountUnit(value, nil, "r/m")
case "wpm":
return simpleCountUnit(value, nil, "w/m")
}
// When unit is not matched, return the value as it is.
return fmt.Sprintf("%v", value)
}
Include in Getting Started: NO