Fixing stats printing with invalid templates
@@ -136,34 +136,44 @@ func (p *StatsTicker) IncrementFailedRequestsBy(count int64) {
func printCallback(stats clistats.StatisticsClient) {
builder := &strings.Builder{}
- builder.WriteRune('[')
- startedAt, _ := stats.GetStatic("startedAt")
- duration := time.Since(startedAt.(time.Time))
- builder.WriteString(fmtDuration(duration))
- builder.WriteRune(']')
- templates, _ := stats.GetStatic("templates")
+ var duration time.Duration
+ if startedAt, ok := stats.GetStatic("startedAt"); ok {
+ if startedAtTime, ok := startedAt.(time.Time); ok {
+ duration = time.Since(startedAtTime)
+ builder.WriteString(fmt.Sprintf("[%s]", fmtDuration(duration)))
+ }
+ }
+
+ if templates, ok := stats.GetStatic("templates"); ok {
builder.WriteString(" | Templates: ")
builder.WriteString(clistats.String(templates))
- hosts, _ := stats.GetStatic("hosts")
+ }
+
+ if hosts, ok := stats.GetStatic("hosts"); ok {
builder.WriteString(" | Hosts: ")
builder.WriteString(clistats.String(hosts))
+ }
- requests, _ := stats.GetCounter("requests")
- total, _ := stats.GetCounter("total")
+ requests, okRequests := stats.GetCounter("requests")
+ total, okTotal := stats.GetCounter("total")
+ if okRequests && okTotal && duration > 0 {
builder.WriteString(" | RPS: ")
builder.WriteString(clistats.String(uint64(float64(requests) / duration.Seconds())))
+ }
- matched, _ := stats.GetCounter("matched")
-
+ if matched, ok := stats.GetCounter("matched"); ok {
builder.WriteString(" | Matched: ")
builder.WriteString(clistats.String(matched))
+ }
- errors, _ := stats.GetCounter("errors")
+ if errors, ok := stats.GetCounter("errors"); ok {
builder.WriteString(" | Errors: ")
builder.WriteString(clistats.String(errors))
+ }
+ if okRequests && okTotal {
builder.WriteString(" | Requests: ")
builder.WriteString(clistats.String(requests))
builder.WriteRune('/')
@@ -175,6 +185,7 @@ func printCallback(stats clistats.StatisticsClient) {
builder.WriteRune('%')
builder.WriteRune(')')
builder.WriteRune('\n')
+ }
fmt.Fprintf(os.Stderr, "%s", builder.String())
}
