report.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.sprintLatencies()
  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. var pctls = []float64{10, 25, 50, 75, 90, 95, 99, 99.9}
  136. // percentiles returns percentile distribution of float64 slice.
  137. func percentiles(nums []float64) (data []float64) {
  138. data = make([]float64, len(pctls))
  139. j := 0
  140. n := len(nums)
  141. for i := 0; i < n && j < len(pctls); i++ {
  142. current := float64(i) * 100.0 / float64(n)
  143. if current >= pctls[j] {
  144. data[j] = nums[i]
  145. j++
  146. }
  147. }
  148. return
  149. }
  150. func (r *report) sprintLatencies() string {
  151. data := percentiles(r.lats)
  152. s := fmt.Sprintf("\nLatency distribution:\n")
  153. for i := 0; i < len(pctls); i++ {
  154. if data[i] > 0 {
  155. s += fmt.Sprintf(" %v%% in %s.\n", pctls[i], r.sec2str(data[i]))
  156. }
  157. }
  158. return s
  159. }
  160. func (r *report) histogram() string {
  161. bc := 10
  162. buckets := make([]float64, bc+1)
  163. counts := make([]int, bc+1)
  164. bs := (r.slowest - r.fastest) / float64(bc)
  165. for i := 0; i < bc; i++ {
  166. buckets[i] = r.fastest + bs*float64(i)
  167. }
  168. buckets[bc] = r.slowest
  169. var bi int
  170. var max int
  171. for i := 0; i < len(r.lats); {
  172. if r.lats[i] <= buckets[bi] {
  173. i++
  174. counts[bi]++
  175. if max < counts[bi] {
  176. max = counts[bi]
  177. }
  178. } else if bi < len(buckets)-1 {
  179. bi++
  180. }
  181. }
  182. s := fmt.Sprintf("\nResponse time histogram:\n")
  183. for i := 0; i < len(buckets); i++ {
  184. // Normalize bar lengths.
  185. var barLen int
  186. if max > 0 {
  187. barLen = counts[i] * 40 / max
  188. }
  189. s += fmt.Sprintf(" "+r.precision+" [%v]\t|%v\n", buckets[i], counts[i], strings.Repeat(barChar, barLen))
  190. }
  191. return s
  192. }
  193. func (r *report) errors() string {
  194. s := fmt.Sprintf("\nError distribution:\n")
  195. for err, num := range r.errorDist {
  196. s += fmt.Sprintf(" [%d]\t%s\n", num, err)
  197. }
  198. return s
  199. }