HumanBytes formats bytes as a human readable string
(bytes float64, si bool)
| 321 | |
| 322 | // HumanBytes formats bytes as a human readable string |
| 323 | func HumanBytes(bytes float64, si bool) string { |
| 324 | var base = 1024 |
| 325 | pre := []string{"K", "M", "G", "T", "P", "E"} |
| 326 | var post = "B" |
| 327 | if si { |
| 328 | base = 1000 |
| 329 | pre = []string{"k", "M", "G", "T", "P", "E"} |
| 330 | post = "iB" |
| 331 | } |
| 332 | if bytes < float64(base) { |
| 333 | return fmt.Sprintf("%.2f B", bytes) |
| 334 | } |
| 335 | exp := int(math.Log(bytes) / math.Log(float64(base))) |
| 336 | index := exp - 1 |
| 337 | units := pre[index] + post |
| 338 | return fmt.Sprintf("%.2f %s", bytes/math.Pow(float64(base), float64(exp)), units) |
| 339 | } |
| 340 | |
| 341 | // MsgsPerClient divides the number of messages by the number of clients and tries to distribute them as evenly as possible |
| 342 | func MsgsPerClient(numMsgs, numClients int) []int { |