Skip to content

Commit

Permalink
Merge pull request #17 from kffl/fix/latency-format
Browse files Browse the repository at this point in the history
Fix/latency format
  • Loading branch information
kffl authored Nov 11, 2021
2 parents c94beec + f37e1b5 commit 790cae5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ license: "Apache-2.0"
message: "Please use the following citation metadata."
repository-code: "https://github.com/kffl/gocannon"
title: "Gocannon - Performance-focused HTTP benchmarking tool"
version: "0.1.0"
version: "0.2.1"
2 changes: 1 addition & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ var (
)

func parseArgs() {
kingpin.Version("0.1.0")
kingpin.Version("0.2.1")
kingpin.Parse()
}
23 changes: 18 additions & 5 deletions reqlog/stats_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ package reqlog

import (
"fmt"
"time"
)

func printStatsHeader() {
fmt.Println("|--REQS--| |------------------------LATENCY-------------------------|")
fmt.Println(" Count AVG P50 P75 P90 P99")
}

var durationUnits = []string{"s ", "ms", "μs", "ns"}
var durationValues = []int64{1000000000, 1000000, 1000, 1}

func formatDuration(d float64) string {
if d == -1 {
return "-"
}
for i, unit := range durationUnits {
value := durationValues[i]
if int64(d)/value > 0 {
return fmt.Sprintf("%.4f%s", d/float64(value), unit)
}
}
return fmt.Sprintf("%.4f%s", d/float64(1), "ns")
}

func formatLatency(latency float64) string {
d := time.Duration(latency) * time.Nanosecond
return d.String()
return formatDuration(latency)
}

func formatLatencyI64(latency int64) string {
d := time.Duration(latency) * time.Nanosecond
return d.String()
return formatDuration(float64(latency))
}

func (s *statistics) print() {
Expand Down
20 changes: 20 additions & 0 deletions reqlog/stats_print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reqlog

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatDuration(t *testing.T) {
durations := []float64{123456789, 999999, 1, 1100, 0.5, -1}
expected := []string{"123.4568ms", "999.9990μs", "1.0000ns", "1.1000μs", "0.5000ns", "-"}

var obtained []string

for _, duration := range durations {
obtained = append(obtained, formatDuration(duration))
}

assert.ElementsMatch(t, obtained, expected)
}

0 comments on commit 790cae5

Please sign in to comment.