github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/bench/tools/aisloader/stats/stats.go (about) 1 // Package stats provides various structs for collecting stats 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package stats 6 7 import ( 8 "math" 9 "time" 10 ) 11 12 // HTTPReq is used for keeping track of http requests stats including number of ops, latency, throughput, etc. 13 // Assume single threaded access, it doesn't provide any locking on updates. 14 type HTTPReq struct { 15 start time.Time // time current stats started 16 cnt int64 // total # of requests 17 bytes int64 // total bytes by all requests 18 errs int64 // number of failed requests 19 latency time.Duration // Accumulated request latency 20 21 // self maintained fields 22 minLatency time.Duration 23 maxLatency time.Duration 24 } 25 26 // NewHTTPReq returns a new stats object with given time as the starting point 27 func NewHTTPReq(t time.Time) HTTPReq { 28 return HTTPReq{ 29 start: t, 30 minLatency: time.Duration(math.MaxInt64), 31 } 32 } 33 34 // Add adds a request's result to the stats 35 func (s *HTTPReq) Add(size int64, delta time.Duration) { 36 s.cnt++ 37 s.bytes += size 38 s.latency += delta 39 s.minLatency = min(s.minLatency, delta) 40 s.maxLatency = max(s.maxLatency, delta) 41 } 42 43 // AddErr increases the number of failed count by 1 44 func (s *HTTPReq) AddErr() { 45 s.errs++ 46 } 47 48 // Total returns the total number of requests. 49 func (s *HTTPReq) Total() int64 { 50 return s.cnt 51 } 52 53 // TotalBytes returns the total number of bytes by all requests. 54 func (s *HTTPReq) TotalBytes() int64 { 55 return s.bytes 56 } 57 58 // MinLatency returns the minimal latency in nano second. 59 func (s *HTTPReq) MinLatency() int64 { 60 if s.cnt == 0 { 61 return 0 62 } 63 return int64(s.minLatency) 64 } 65 66 // MaxLatency returns the maximum latency in nano second. 67 func (s *HTTPReq) MaxLatency() int64 { 68 if s.cnt == 0 { 69 return 0 70 } 71 return int64(s.maxLatency) 72 } 73 74 // AvgLatency returns the avg latency in nano second. 75 func (s *HTTPReq) AvgLatency() int64 { 76 if s.cnt == 0 { 77 return 0 78 } 79 return int64(s.latency) / s.cnt 80 } 81 82 // Throughput returns throughput of requests (bytes/per second). 83 func (s *HTTPReq) Throughput(start, end time.Time) int64 { 84 if start == end { 85 return 0 86 } 87 return int64(float64(s.bytes) / end.Sub(start).Seconds()) 88 } 89 90 // Start returns the start time of the stats. 91 func (s *HTTPReq) Start() time.Time { 92 return s.start 93 } 94 95 // TotalErrs returns the total number of failed requests. 96 func (s *HTTPReq) TotalErrs() int64 { 97 return s.errs 98 } 99 100 // Aggregate adds another stats to self 101 func (s *HTTPReq) Aggregate(other HTTPReq) { 102 s.cnt += other.cnt 103 s.bytes += other.bytes 104 s.errs += other.errs 105 s.latency += other.latency 106 107 s.minLatency = min(s.minLatency, other.minLatency) 108 s.maxLatency = max(s.maxLatency, other.maxLatency) 109 }