report.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2014 The etcd Authors
  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 report
  16. import (
  17. "fmt"
  18. "math"
  19. "sort"
  20. "strings"
  21. "time"
  22. )
  23. const (
  24. barChar = "∎"
  25. )
  26. // Result describes the timings for an operation.
  27. type Result struct {
  28. Start time.Time
  29. End time.Time
  30. Err error
  31. }
  32. func (res *Result) Duration() time.Duration { return res.End.Sub(res.Start) }
  33. type report struct {
  34. results chan Result
  35. precision string
  36. avgTotal float64
  37. fastest float64
  38. slowest float64
  39. average float64
  40. stddev float64
  41. rps float64
  42. total time.Duration
  43. errorDist map[string]int
  44. lats []float64
  45. sps *secondPoints
  46. }
  47. // Report processes a result stream until it is closed, then produces a
  48. // string with information about the consumed result data.
  49. type Report interface {
  50. Results() chan<- Result
  51. Run() <-chan string
  52. String() string
  53. }
  54. func NewReport(precision string) Report {
  55. return &report{
  56. results: make(chan Result, 16),
  57. precision: precision,
  58. errorDist: make(map[string]int),
  59. }
  60. }
  61. func NewReportSample(precision string) Report {
  62. r := NewReport(precision).(*report)
  63. r.sps = newSecondPoints()
  64. return r
  65. }
  66. func (r *report) Results() chan<- Result { return r.results }
  67. func (r *report) Run() <-chan string {
  68. donec := make(chan string, 1)
  69. go func() {
  70. defer close(donec)
  71. r.processResults()
  72. donec <- r.String()
  73. }()
  74. return donec
  75. }
  76. func (r *report) String() (s string) {
  77. if len(r.lats) > 0 {
  78. s += fmt.Sprintf("\nSummary:\n")
  79. s += fmt.Sprintf(" Total:\t%s.\n", r.sec2str(r.total.Seconds()))
  80. s += fmt.Sprintf(" Slowest:\t%s.\n", r.sec2str(r.slowest))
  81. s += fmt.Sprintf(" Fastest:\t%s.\n", r.sec2str(r.fastest))
  82. s += fmt.Sprintf(" Average:\t%s.\n", r.sec2str(r.average))
  83. s += fmt.Sprintf(" Stddev:\t%s.\n", r.sec2str(r.stddev))
  84. s += fmt.Sprintf(" Requests/sec:\t"+r.precision+"\n", r.rps)
  85. s += r.histogram()
  86. s += r.latencies()
  87. if r.sps != nil {
  88. s += fmt.Sprintf("%v\n", r.sps.getTimeSeries())
  89. }
  90. }
  91. if len(r.errorDist) > 0 {
  92. s += r.errors()
  93. }
  94. return s
  95. }
  96. func (r *report) sec2str(sec float64) string { return fmt.Sprintf(r.precision+" secs", sec) }
  97. type reportRate struct{ *report }
  98. func NewReportRate(precision string) Report {
  99. return &reportRate{NewReport(precision).(*report)}
  100. }
  101. func (r *reportRate) String() string {
  102. return fmt.Sprintf(" Requests/sec:\t"+r.precision+"\n", r.rps)
  103. }
  104. func (r *report) processResult(res *Result) {
  105. if res.Err != nil {
  106. r.errorDist[res.Err.Error()]++
  107. return
  108. }
  109. dur := res.Duration()
  110. r.lats = append(r.lats, dur.Seconds())
  111. r.avgTotal += dur.Seconds()
  112. if r.sps != nil {
  113. r.sps.Add(res.Start, dur)
  114. }
  115. }
  116. func (r *report) processResults() {
  117. st := time.Now()
  118. for res := range r.results {
  119. r.processResult(&res)
  120. }
  121. r.total = time.Since(st)
  122. r.rps = float64(len(r.lats)) / r.total.Seconds()
  123. r.average = r.avgTotal / float64(len(r.lats))
  124. for i := range r.lats {
  125. dev := r.lats[i] - r.average
  126. r.stddev += dev * dev
  127. }
  128. r.stddev = math.Sqrt(r.stddev / float64(len(r.lats)))
  129. sort.Float64s(r.lats)
  130. if len(r.lats) > 0 {
  131. r.fastest = r.lats[0]
  132. r.slowest = r.lats[len(r.lats)-1]
  133. }
  134. }
  135. func (r *report) latencies() string {
  136. pctls := []int{10, 25, 50, 75, 90, 95, 99}
  137. data := make([]float64, len(pctls))
  138. j := 0
  139. for i := 0; i < len(r.lats) && j < len(pctls); i++ {
  140. current := i * 100 / len(r.lats)
  141. if current >= pctls[j] {
  142. data[j] = r.lats[i]
  143. j++
  144. }
  145. }
  146. s := fmt.Sprintf("\nLatency distribution:\n")
  147. for i := 0; i < len(pctls); i++ {
  148. if data[i] > 0 {
  149. s += fmt.Sprintf(" %v%% in %s.\n", pctls[i], r.sec2str(data[i]))
  150. }
  151. }
  152. return s
  153. }
  154. func (r *report) histogram() string {
  155. bc := 10
  156. buckets := make([]float64, bc+1)
  157. counts := make([]int, bc+1)
  158. bs := (r.slowest - r.fastest) / float64(bc)
  159. for i := 0; i < bc; i++ {
  160. buckets[i] = r.fastest + bs*float64(i)
  161. }
  162. buckets[bc] = r.slowest
  163. var bi int
  164. var max int
  165. for i := 0; i < len(r.lats); {
  166. if r.lats[i] <= buckets[bi] {
  167. i++
  168. counts[bi]++
  169. if max < counts[bi] {
  170. max = counts[bi]
  171. }
  172. } else if bi < len(buckets)-1 {
  173. bi++
  174. }
  175. }
  176. s := fmt.Sprintf("\nResponse time histogram:\n")
  177. for i := 0; i < len(buckets); i++ {
  178. // Normalize bar lengths.
  179. var barLen int
  180. if max > 0 {
  181. barLen = counts[i] * 40 / max
  182. }
  183. s += fmt.Sprintf(" "+r.precision+" [%v]\t|%v\n", buckets[i], counts[i], strings.Repeat(barChar, barLen))
  184. }
  185. return s
  186. }
  187. func (r *report) errors() string {
  188. s := fmt.Sprintf("\nError distribution:\n")
  189. for err, num := range r.errorDist {
  190. s += fmt.Sprintf(" [%d]\t%s\n", num, err)
  191. }
  192. return s
  193. }