FormatByteRate outputs the given rate of transfer "r" as the quotient of "s" (the number of bytes transferred) over "d" (the duration of time that those bytes were transferred in). It displays the output as a quantity of a "per-unit-time" unit (i.e., B/s, MiB/s) in the most representative fashion p
(s uint64, d time.Duration)
| 139 | // It displays the output as a quantity of a "per-unit-time" unit (i.e., B/s, |
| 140 | // MiB/s) in the most representative fashion possible, as above. |
| 141 | func FormatByteRate(s uint64, d time.Duration) string { |
| 142 | // e is the index of the most representative unit of storage. |
| 143 | var e float64 |
| 144 | |
| 145 | // f is the floating-point equivalent of "s", so as to avoid more |
| 146 | // conversions than necessary. |
| 147 | f := float64(s) |
| 148 | |
| 149 | if f != 0 { |
| 150 | f = f / math.Max(time.Nanosecond.Seconds(), d.Seconds()) |
| 151 | e = math.Floor(log(f, 1000)) |
| 152 | if e <= eps { |
| 153 | // The result of math.Floor(log(r, 1000)) can be |
| 154 | // "close-enough" to zero that it should be effectively |
| 155 | // considered zero. |
| 156 | e = 0 |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | unit := uint64(math.Pow(1000, e)) |
| 161 | suffix := sizes[int(e)] |
| 162 | |
| 163 | return fmt.Sprintf("%s %s/s", |
| 164 | FormatBytesUnit(uint64(math.Ceil(f)), unit), suffix) |
| 165 | } |
| 166 | |
| 167 | // log takes the log base "b" of "n" (\log_b{n}) |
| 168 | func log(n, b float64) float64 { |