@@ -3,10 +3,8 @@ package main
33import (
44 "context"
55 "crypto/rand"
6- "encoding/json"
76 "errors"
87 "fmt"
9- "io"
108 "log/slog"
119 "net"
1210 "sync"
@@ -308,35 +306,7 @@ func (c *Client) connectUDP(ctx context.Context, addrport string) error {
308306 return eg .Wait ()
309307}
310308
311- func toMicroseconds (n int64 ) int64 {
312- return time .Duration (n ).Microseconds ()
313- }
314-
315- func toMicrosecondsf (n float64 ) int64 {
316- return time .Duration (n ).Microseconds ()
317- }
318-
319- func printStatHeader (w io.Writer ) {
320- fmt .Fprintf (w , "%-20s %-10s %-15s %-15s %-15s %-15s %-15s %-15s %-10s\n " ,
321- "PEER" , "CNT" , "LAT_MAX(µs)" , "LAT_MIN(µs)" , "LAT_MEAN(µs)" ,
322- "LAT_90p(µs)" , "LAT_95p(µs)" , "LAT_99p(µs)" , "RATE(/s)" )
323- }
324-
325- func printStatLine (w io.Writer , addr string , stat metrics.Timer ) {
326- fmt .Fprintf (w , "%-20s %-10d %-15d %-15d %-15d %-15d %-15d %-15d %-10.2f\n " ,
327- addr ,
328- stat .Count (),
329- toMicroseconds (stat .Max ()),
330- toMicroseconds (stat .Min ()),
331- toMicrosecondsf (stat .Mean ()),
332- toMicrosecondsf (stat .Percentile (0.9 )),
333- toMicrosecondsf (stat .Percentile (0.95 )),
334- toMicrosecondsf (stat .Percentile (0.99 )),
335- stat .RateMean (),
336- )
337- }
338-
339- func runStatLinePrinter (ctx context.Context , w io.Writer , addr string , intervalStats time.Duration , mergeResultsEachHost bool ) {
309+ func runStatLinePrinter (ctx context.Context , printer * Printer , addr string , intervalStats time.Duration , mergeResultsEachHost bool ) {
340310 go func () {
341311 ticker := time .NewTicker (intervalStats )
342312 defer ticker .Stop ()
@@ -347,78 +317,9 @@ func runStatLinePrinter(ctx context.Context, w io.Writer, addr string, intervalS
347317 return
348318 case <- ticker .C :
349319 ts := getOrRegisterTimer ("tick.latency" , addr , mergeResultsEachHost )
350- printStatLine ( w , addr , ts )
320+ printer . PrintStatLine ( addr , ts )
351321 unregisterTimer ("tick.latency" , addr , mergeResultsEachHost )
352322 }
353323 }
354324 }()
355325}
356-
357- type JSONLinesResult struct {
358- Peer string `json:"peer"`
359- Count int64 `json:"count"`
360- LatencyMax int64 `json:"latency_max_us"`
361- LatencyMin int64 `json:"latency_min_us"`
362- LatencyMean int64 `json:"latency_mean_us"`
363- Latency90p int64 `json:"latency_90p_us"`
364- Latency95p int64 `json:"latency_95p_us"`
365- Latency99p int64 `json:"latency_99p_us"`
366- Rate float64 `json:"rate_per_sec"`
367- Timestamp string `json:"timestamp"`
368- }
369-
370- func printJSONLinesReport (w io.Writer , addrs []string , mergeResultsEachHost bool ) {
371- timestamp := time .Now ().UTC ().Format (time .RFC3339 )
372- results := []JSONLinesResult {}
373-
374- if mergeResultsEachHost {
375- ts := getOrRegisterTimer ("total.latency" , "" , mergeResultsEachHost )
376- results = append (results , JSONLinesResult {
377- Peer : fmt .Sprintf ("merged(%d hosts)" , len (addrs )),
378- Count : ts .Count (),
379- LatencyMax : toMicroseconds (ts .Max ()),
380- LatencyMin : toMicroseconds (ts .Min ()),
381- LatencyMean : toMicrosecondsf (ts .Mean ()),
382- Latency90p : toMicrosecondsf (ts .Percentile (0.9 )),
383- Latency95p : toMicrosecondsf (ts .Percentile (0.95 )),
384- Latency99p : toMicrosecondsf (ts .Percentile (0.99 )),
385- Rate : ts .RateMean (),
386- Timestamp : timestamp ,
387- })
388- } else {
389- for _ , addr := range addrs {
390- ts := getOrRegisterTimer ("total.latency" , addr , mergeResultsEachHost )
391- results = append (results , JSONLinesResult {
392- Peer : addr ,
393- Count : ts .Count (),
394- LatencyMax : toMicroseconds (ts .Max ()),
395- LatencyMin : toMicroseconds (ts .Min ()),
396- LatencyMean : toMicrosecondsf (ts .Mean ()),
397- Latency90p : toMicrosecondsf (ts .Percentile (0.9 )),
398- Latency95p : toMicrosecondsf (ts .Percentile (0.95 )),
399- Latency99p : toMicrosecondsf (ts .Percentile (0.99 )),
400- Rate : ts .RateMean (),
401- Timestamp : timestamp ,
402- })
403- }
404- }
405-
406- for _ , result := range results {
407- if err := json .NewEncoder (w ).Encode (result ); err != nil {
408- slog .Error ("Failed to encode JSON result" , "error" , err )
409- }
410- }
411- }
412-
413- func printReport (w io.Writer , addrs []string , mergeResultsEachHost bool ) {
414- fmt .Fprintln (w , "--- A result during total execution time ---" )
415- if mergeResultsEachHost {
416- ts := getOrRegisterTimer ("total.latency" , "" , mergeResultsEachHost )
417- printStatLine (w , fmt .Sprintf ("merged(%d hosts)" , len (addrs )), ts )
418- return
419- }
420- for _ , addr := range addrs {
421- ts := getOrRegisterTimer ("total.latency" , addr , mergeResultsEachHost )
422- printStatLine (w , addr , ts )
423- }
424- }
0 commit comments