report.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "math"
  19. "sort"
  20. "strings"
  21. "time"
  22. )
  23. const (
  24. barChar = "∎"
  25. )
  26. type result struct {
  27. errStr string
  28. duration time.Duration
  29. }
  30. type report struct {
  31. avgTotal float64
  32. fastest float64
  33. slowest float64
  34. average float64
  35. stddev float64
  36. rps float64
  37. results chan result
  38. total time.Duration
  39. errorDist map[string]int
  40. lats []float64
  41. }
  42. func printReport(results chan result) <-chan struct{} {
  43. return wrapReport(func() {
  44. r := &report{
  45. results: results,
  46. errorDist: make(map[string]int),
  47. }
  48. r.finalize()
  49. r.print()
  50. })
  51. }
  52. func printRate(results chan result) <-chan struct{} {
  53. return wrapReport(func() {
  54. r := &report{
  55. results: results,
  56. errorDist: make(map[string]int),
  57. }
  58. r.finalize()
  59. fmt.Printf(" Requests/sec:\t%4.4f\n", r.rps)
  60. })
  61. }
  62. func wrapReport(f func()) <-chan struct{} {
  63. donec := make(chan struct{})
  64. go func() {
  65. defer close(donec)
  66. f()
  67. }()
  68. return donec
  69. }
  70. func (r *report) finalize() {
  71. st := time.Now()
  72. for res := range r.results {
  73. if res.errStr != "" {
  74. r.errorDist[res.errStr]++
  75. } else {
  76. r.lats = append(r.lats, res.duration.Seconds())
  77. r.avgTotal += res.duration.Seconds()
  78. }
  79. }
  80. r.total = time.Since(st)
  81. r.rps = float64(len(r.lats)) / r.total.Seconds()
  82. r.average = r.avgTotal / float64(len(r.lats))
  83. for i := range r.lats {
  84. dev := r.lats[i] - r.average
  85. r.stddev += dev * dev
  86. }
  87. r.stddev = math.Sqrt(r.stddev / float64(len(r.lats)))
  88. }
  89. func (r *report) print() {
  90. sort.Float64s(r.lats)
  91. if len(r.lats) > 0 {
  92. r.fastest = r.lats[0]
  93. r.slowest = r.lats[len(r.lats)-1]
  94. fmt.Printf("\nSummary:\n")
  95. fmt.Printf(" Total:\t%4.4f secs.\n", r.total.Seconds())
  96. fmt.Printf(" Slowest:\t%4.4f secs.\n", r.slowest)
  97. fmt.Printf(" Fastest:\t%4.4f secs.\n", r.fastest)
  98. fmt.Printf(" Average:\t%4.4f secs.\n", r.average)
  99. fmt.Printf(" Stddev:\t%4.4f secs.\n", r.stddev)
  100. fmt.Printf(" Requests/sec:\t%4.4f\n", r.rps)
  101. r.printHistogram()
  102. r.printLatencies()
  103. }
  104. if len(r.errorDist) > 0 {
  105. r.printErrors()
  106. }
  107. }
  108. // Prints percentile latencies.
  109. func (r *report) printLatencies() {
  110. pctls := []int{10, 25, 50, 75, 90, 95, 99}
  111. data := make([]float64, len(pctls))
  112. j := 0
  113. for i := 0; i < len(r.lats) && j < len(pctls); i++ {
  114. current := i * 100 / len(r.lats)
  115. if current >= pctls[j] {
  116. data[j] = r.lats[i]
  117. j++
  118. }
  119. }
  120. fmt.Printf("\nLatency distribution:\n")
  121. for i := 0; i < len(pctls); i++ {
  122. if data[i] > 0 {
  123. fmt.Printf(" %v%% in %4.4f secs.\n", pctls[i], data[i])
  124. }
  125. }
  126. }
  127. func (r *report) printHistogram() {
  128. bc := 10
  129. buckets := make([]float64, bc+1)
  130. counts := make([]int, bc+1)
  131. bs := (r.slowest - r.fastest) / float64(bc)
  132. for i := 0; i < bc; i++ {
  133. buckets[i] = r.fastest + bs*float64(i)
  134. }
  135. buckets[bc] = r.slowest
  136. var bi int
  137. var max int
  138. for i := 0; i < len(r.lats); {
  139. if r.lats[i] <= buckets[bi] {
  140. i++
  141. counts[bi]++
  142. if max < counts[bi] {
  143. max = counts[bi]
  144. }
  145. } else if bi < len(buckets)-1 {
  146. bi++
  147. }
  148. }
  149. fmt.Printf("\nResponse time histogram:\n")
  150. for i := 0; i < len(buckets); i++ {
  151. // Normalize bar lengths.
  152. var barLen int
  153. if max > 0 {
  154. barLen = counts[i] * 40 / max
  155. }
  156. fmt.Printf(" %4.3f [%v]\t|%v\n", buckets[i], counts[i], strings.Repeat(barChar, barLen))
  157. }
  158. }
  159. func (r *report) printErrors() {
  160. fmt.Printf("\nError distribution:\n")
  161. for err, num := range r.errorDist {
  162. fmt.Printf(" [%d]\t%s\n", num, err)
  163. }
  164. }