report.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // the file is borrowed from github.com/rakyll/boom/boomer/print.go
  15. package cmd
  16. import (
  17. "fmt"
  18. "sort"
  19. "strings"
  20. "time"
  21. )
  22. const (
  23. barChar = "∎"
  24. )
  25. type result struct {
  26. errStr string
  27. duration time.Duration
  28. }
  29. type report struct {
  30. avgTotal float64
  31. fastest float64
  32. slowest float64
  33. average float64
  34. rps float64
  35. results chan *result
  36. total time.Duration
  37. errorDist map[string]int
  38. lats []float64
  39. }
  40. func printReport(size int, results chan *result, total time.Duration) {
  41. r := &report{
  42. results: results,
  43. total: total,
  44. errorDist: make(map[string]int),
  45. }
  46. r.finalize()
  47. }
  48. func (r *report) finalize() {
  49. for {
  50. select {
  51. case res := <-r.results:
  52. if res.errStr != "" {
  53. r.errorDist[res.errStr]++
  54. } else {
  55. r.lats = append(r.lats, res.duration.Seconds())
  56. r.avgTotal += res.duration.Seconds()
  57. }
  58. default:
  59. r.rps = float64(len(r.lats)) / r.total.Seconds()
  60. r.average = r.avgTotal / float64(len(r.lats))
  61. r.print()
  62. return
  63. }
  64. }
  65. }
  66. func (r *report) print() {
  67. sort.Float64s(r.lats)
  68. if len(r.lats) > 0 {
  69. r.fastest = r.lats[0]
  70. r.slowest = r.lats[len(r.lats)-1]
  71. fmt.Printf("\nSummary:\n")
  72. fmt.Printf(" Total:\t%4.4f secs.\n", r.total.Seconds())
  73. fmt.Printf(" Slowest:\t%4.4f secs.\n", r.slowest)
  74. fmt.Printf(" Fastest:\t%4.4f secs.\n", r.fastest)
  75. fmt.Printf(" Average:\t%4.4f secs.\n", r.average)
  76. fmt.Printf(" Requests/sec:\t%4.4f\n", r.rps)
  77. r.printHistogram()
  78. r.printLatencies()
  79. }
  80. if len(r.errorDist) > 0 {
  81. r.printErrors()
  82. }
  83. }
  84. // Prints percentile latencies.
  85. func (r *report) printLatencies() {
  86. pctls := []int{10, 25, 50, 75, 90, 95, 99}
  87. data := make([]float64, len(pctls))
  88. j := 0
  89. for i := 0; i < len(r.lats) && j < len(pctls); i++ {
  90. current := i * 100 / len(r.lats)
  91. if current >= pctls[j] {
  92. data[j] = r.lats[i]
  93. j++
  94. }
  95. }
  96. fmt.Printf("\nLatency distribution:\n")
  97. for i := 0; i < len(pctls); i++ {
  98. if data[i] > 0 {
  99. fmt.Printf(" %v%% in %4.4f secs.\n", pctls[i], data[i])
  100. }
  101. }
  102. }
  103. func (r *report) printHistogram() {
  104. bc := 10
  105. buckets := make([]float64, bc+1)
  106. counts := make([]int, bc+1)
  107. bs := (r.slowest - r.fastest) / float64(bc)
  108. for i := 0; i < bc; i++ {
  109. buckets[i] = r.fastest + bs*float64(i)
  110. }
  111. buckets[bc] = r.slowest
  112. var bi int
  113. var max int
  114. for i := 0; i < len(r.lats); {
  115. if r.lats[i] <= buckets[bi] {
  116. i++
  117. counts[bi]++
  118. if max < counts[bi] {
  119. max = counts[bi]
  120. }
  121. } else if bi < len(buckets)-1 {
  122. bi++
  123. }
  124. }
  125. fmt.Printf("\nResponse time histogram:\n")
  126. for i := 0; i < len(buckets); i++ {
  127. // Normalize bar lengths.
  128. var barLen int
  129. if max > 0 {
  130. barLen = counts[i] * 40 / max
  131. }
  132. fmt.Printf(" %4.3f [%v]\t|%v\n", buckets[i], counts[i], strings.Repeat(barChar, barLen))
  133. }
  134. }
  135. func (r *report) printErrors() {
  136. fmt.Printf("\nError distribution:\n")
  137. for err, num := range r.errorDist {
  138. fmt.Printf(" [%d]\t%s\n", num, err)
  139. }
  140. }