timeseries.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 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. package report
  15. import (
  16. "bytes"
  17. "encoding/csv"
  18. "fmt"
  19. "log"
  20. "math"
  21. "sort"
  22. "sync"
  23. "time"
  24. )
  25. type DataPoint struct {
  26. Timestamp int64
  27. MinLatency time.Duration
  28. AvgLatency time.Duration
  29. MaxLatency time.Duration
  30. ThroughPut int64
  31. }
  32. type TimeSeries []DataPoint
  33. func (t TimeSeries) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
  34. func (t TimeSeries) Len() int { return len(t) }
  35. func (t TimeSeries) Less(i, j int) bool { return t[i].Timestamp < t[j].Timestamp }
  36. type secondPoint struct {
  37. minLatency time.Duration
  38. maxLatency time.Duration
  39. totalLatency time.Duration
  40. count int64
  41. }
  42. type secondPoints struct {
  43. mu sync.Mutex
  44. tm map[int64]secondPoint
  45. }
  46. func newSecondPoints() *secondPoints {
  47. return &secondPoints{tm: make(map[int64]secondPoint)}
  48. }
  49. func (sp *secondPoints) Add(ts time.Time, lat time.Duration) {
  50. sp.mu.Lock()
  51. defer sp.mu.Unlock()
  52. tk := ts.Unix()
  53. if v, ok := sp.tm[tk]; !ok {
  54. sp.tm[tk] = secondPoint{minLatency: lat, maxLatency: lat, totalLatency: lat, count: 1}
  55. } else {
  56. if lat != time.Duration(0) {
  57. v.minLatency = minDuration(v.minLatency, lat)
  58. }
  59. v.maxLatency = maxDuration(v.maxLatency, lat)
  60. v.totalLatency += lat
  61. v.count++
  62. sp.tm[tk] = v
  63. }
  64. }
  65. func (sp *secondPoints) getTimeSeries() TimeSeries {
  66. sp.mu.Lock()
  67. defer sp.mu.Unlock()
  68. var (
  69. minTs int64 = math.MaxInt64
  70. maxTs int64 = -1
  71. )
  72. for k := range sp.tm {
  73. if minTs > k {
  74. minTs = k
  75. }
  76. if maxTs < k {
  77. maxTs = k
  78. }
  79. }
  80. for ti := minTs; ti < maxTs; ti++ {
  81. if _, ok := sp.tm[ti]; !ok { // fill-in empties
  82. sp.tm[ti] = secondPoint{totalLatency: 0, count: 0}
  83. }
  84. }
  85. var (
  86. tslice = make(TimeSeries, len(sp.tm))
  87. i int
  88. )
  89. for k, v := range sp.tm {
  90. var lat time.Duration
  91. if v.count > 0 {
  92. lat = time.Duration(v.totalLatency) / time.Duration(v.count)
  93. }
  94. tslice[i] = DataPoint{
  95. Timestamp: k,
  96. MinLatency: v.minLatency,
  97. AvgLatency: lat,
  98. MaxLatency: v.maxLatency,
  99. ThroughPut: v.count,
  100. }
  101. i++
  102. }
  103. sort.Sort(tslice)
  104. return tslice
  105. }
  106. func (ts TimeSeries) String() string {
  107. buf := new(bytes.Buffer)
  108. wr := csv.NewWriter(buf)
  109. if err := wr.Write([]string{"UNIX-SECOND", "MIN-LATENCY-MS", "AVG-LATENCY-MS", "MAX-LATENCY-MS", "AVG-THROUGHPUT"}); err != nil {
  110. log.Fatal(err)
  111. }
  112. rows := [][]string{}
  113. for i := range ts {
  114. row := []string{
  115. fmt.Sprintf("%d", ts[i].Timestamp),
  116. ts[i].MinLatency.String(),
  117. ts[i].AvgLatency.String(),
  118. ts[i].MaxLatency.String(),
  119. fmt.Sprintf("%d", ts[i].ThroughPut),
  120. }
  121. rows = append(rows, row)
  122. }
  123. if err := wr.WriteAll(rows); err != nil {
  124. log.Fatal(err)
  125. }
  126. wr.Flush()
  127. if err := wr.Error(); err != nil {
  128. log.Fatal(err)
  129. }
  130. return fmt.Sprintf("\nSample in one second (unix latency throughput):\n%s", buf.String())
  131. }
  132. func minDuration(a, b time.Duration) time.Duration {
  133. if a < b {
  134. return a
  135. }
  136. return b
  137. }
  138. func maxDuration(a, b time.Duration) time.Duration {
  139. if a > b {
  140. return a
  141. }
  142. return b
  143. }