additional output formats
This commit is contained in:
parent
89eab3da05
commit
766e407cc3
38
cli/main.go
38
cli/main.go
@ -8,18 +8,38 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FormatOutput(pause time.Duration, ns bool) string {
|
func FormatOutput(pause time.Duration, format string) string {
|
||||||
switch ns {
|
var results string
|
||||||
case true:
|
switch strings.ToLower(format) {
|
||||||
return strconv.FormatInt(pause.Nanoseconds(), 10)
|
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:
|
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() {
|
func main() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
var (
|
var (
|
||||||
@ -28,14 +48,14 @@ func main() {
|
|||||||
multiplier time.Duration
|
multiplier time.Duration
|
||||||
jitter time.Duration
|
jitter time.Duration
|
||||||
iteration int64
|
iteration int64
|
||||||
nanosecondsOutput bool
|
outputformat string
|
||||||
)
|
)
|
||||||
flag.BoolVar(&doPanic, "panic", false, "panic instead of overflowing values (default returns the max value instead of overflowing).")
|
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.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(&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.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.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()
|
flag.Parse()
|
||||||
|
|
||||||
if jitter < 0 {
|
if jitter < 0 {
|
||||||
@ -62,9 +82,9 @@ func main() {
|
|||||||
switch doSleep {
|
switch doSleep {
|
||||||
case true:
|
case true:
|
||||||
<-backoff.After(context.Background())
|
<-backoff.After(context.Background())
|
||||||
response = FormatOutput(time.Since(start), nanosecondsOutput)
|
response = FormatOutput(time.Since(start), outputformat)
|
||||||
default:
|
default:
|
||||||
response = FormatOutput(backoff.Next(), nanosecondsOutput)
|
response = FormatOutput(backoff.Next(), outputformat)
|
||||||
}
|
}
|
||||||
fmt.Println(response)
|
fmt.Println(response)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user