From 236b50d6db2e60f248b6ed2e0479998903896764 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Sinha Date: Thu, 25 Aug 2022 09:39:32 +0530 Subject: [PATCH] trim response to two-decimal places in ping command (#4214) --- cmd/ping.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/ping.go b/cmd/ping.go index 4ab2a707..bae00b85 100644 --- a/cmd/ping.go +++ b/cmd/ping.go @@ -20,6 +20,7 @@ package cmd import ( "context" "errors" + "fmt" "math" "net" "net/url" @@ -216,12 +217,12 @@ func ping(ctx context.Context, cliCtx *cli.Context, anonClient *madmin.Anonymous stat := getPingInfo(cliCtx, result, endPointMap) endPointStat := EndPointStats{ Endpoint: endPoint, - Min: time.Duration(stat.min).Round(time.Microsecond).String(), - Max: time.Duration(stat.max).Round(time.Microsecond).String(), - Average: time.Duration(stat.avg).Round(time.Microsecond).String(), + Min: trimToTwoDecimal(time.Duration(stat.min)), + Max: trimToTwoDecimal(time.Duration(stat.max)), + Average: trimToTwoDecimal(time.Duration(stat.avg)), CountErr: strconv.Itoa(stat.errorCount), Error: stat.err, - Roundtrip: result.ResponseTime.Round(time.Microsecond).String(), + Roundtrip: trimToTwoDecimal(result.ResponseTime), } endPointStats = append(endPointStats, endPointStat) endPointMap[result.Endpoint.Host] = stat @@ -236,6 +237,20 @@ func ping(ctx context.Context, cliCtx *cli.Context, anonClient *madmin.Anonymous time.Sleep(time.Duration(cliCtx.Int("interval")) * time.Second) } +func trimToTwoDecimal(d time.Duration) string { + var f float64 + var unit string + switch { + case d >= time.Second: + f = float64(d) / float64(time.Second) + unit = "s" + default: + f = float64(d) / float64(time.Millisecond) + unit = "ms" + } + return fmt.Sprintf("%.02f%s", f, unit) +} + func getPingInfo(cliCtx *cli.Context, result madmin.AliveResult, serverMap map[string]serverStats) serverStats { var errorString string var sum, avg uint64