report.go 3.8 KB

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