From 766e407cc3e1dd84620c3eda371b5de88a15e6a1 Mon Sep 17 00:00:00 2001 From: William Dillon Date: Mon, 9 Feb 2026 19:36:33 -0500 Subject: [PATCH] additional output formats --- cli/main.go | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/cli/main.go b/cli/main.go index aff3aaa..db95402 100644 --- a/cli/main.go +++ b/cli/main.go @@ -8,34 +8,54 @@ import ( "math" "os" "strconv" + "strings" "time" ) -func FormatOutput(pause time.Duration, ns bool) string { - switch ns { - case true: - return strconv.FormatInt(pause.Nanoseconds(), 10) +func FormatOutput(pause time.Duration, format string) string { + var results string + switch strings.ToLower(format) { + case "string": + results = pause.String() + case "s": + results = fmt.Sprintf("%f", pause.Seconds()) + case "ms": + results = strconv.FormatInt(pause.Milliseconds(), 10) + case "us", "µs": + results = strconv.FormatInt(pause.Microseconds(), 10) + case "ns": + results = strconv.FormatInt(pause.Nanoseconds(), 10) default: - return pause.String() + fmt.Fprintf(os.Stderr, "warning: unknown format %q - using 'string'\n", format) + results = pause.String() } + return results } +type OutputFormat string + +const ( + SECONDS OutputFormat = "s" + MILLISECONDS OutputFormat = "ms" + MICROSECONDS OutputFormat = "us" +) + func main() { start := time.Now() var ( - doPanic bool - doSleep bool - multiplier time.Duration - jitter time.Duration - iteration int64 - nanosecondsOutput bool + doPanic bool + doSleep bool + multiplier time.Duration + jitter time.Duration + iteration int64 + outputformat string ) flag.BoolVar(&doPanic, "panic", false, "panic instead of overflowing values (default returns the max value instead of overflowing).") flag.BoolVar(&doSleep, "sleep", false, "sleep for the resulting duration after calculating it. prints the run duration of the program instead of the calculated pause duration.") flag.DurationVar(&multiplier, "multiplier", time.Millisecond, "pause multiplier.") flag.DurationVar(&jitter, "jitter", 0, "jitter boundary. pause duration is randomly selected in the range of resultDuration+-jitter.") flag.Int64Var(&iteration, "iteration", 1, "iteration to use for calculating pause time.") - flag.BoolVar(&nanosecondsOutput, "ns", false, "outputs int64 nanoseconds instead of string.") + flag.StringVar(&outputformat, "format", "string", "output format - s, ms, us, µs, ns, string") flag.Parse() if jitter < 0 { @@ -62,9 +82,9 @@ func main() { switch doSleep { case true: <-backoff.After(context.Background()) - response = FormatOutput(time.Since(start), nanosecondsOutput) + response = FormatOutput(time.Since(start), outputformat) default: - response = FormatOutput(backoff.Next(), nanosecondsOutput) + response = FormatOutput(backoff.Next(), outputformat) } fmt.Println(response)