timeseries.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 cmd
  15. import (
  16. "bytes"
  17. "encoding/csv"
  18. "fmt"
  19. "log"
  20. "math"
  21. "sort"
  22. "sync"
  23. "time"
  24. )
  25. type timeSeries struct {
  26. timestamp int64
  27. avgLatency time.Duration
  28. throughPut int64
  29. }
  30. type TimeSeries []timeSeries
  31. func (t TimeSeries) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
  32. func (t TimeSeries) Len() int { return len(t) }
  33. func (t TimeSeries) Less(i, j int) bool { return t[i].timestamp < t[j].timestamp }
  34. type secondPoint struct {
  35. totalLatency time.Duration
  36. count int64
  37. }
  38. type secondPoints struct {
  39. mu sync.Mutex
  40. tm map[int64]secondPoint
  41. }
  42. func newSecondPoints() *secondPoints {
  43. return &secondPoints{tm: make(map[int64]secondPoint)}
  44. }
  45. func (sp *secondPoints) Add(ts time.Time, lat time.Duration) {
  46. sp.mu.Lock()
  47. defer sp.mu.Unlock()
  48. tk := ts.Unix()
  49. if v, ok := sp.tm[tk]; !ok {
  50. sp.tm[tk] = secondPoint{totalLatency: lat, count: 1}
  51. } else {
  52. v.totalLatency += lat
  53. v.count += 1
  54. sp.tm[tk] = v
  55. }
  56. }
  57. func (sp *secondPoints) getTimeSeries() TimeSeries {
  58. sp.mu.Lock()
  59. defer sp.mu.Unlock()
  60. var (
  61. minTs int64 = math.MaxInt64
  62. maxTs int64 = -1
  63. )
  64. for k := range sp.tm {
  65. if minTs > k {
  66. minTs = k
  67. }
  68. if maxTs < k {
  69. maxTs = k
  70. }
  71. }
  72. for ti := minTs; ti < maxTs; ti++ {
  73. if _, ok := sp.tm[ti]; !ok { // fill-in empties
  74. sp.tm[ti] = secondPoint{totalLatency: 0, count: 0}
  75. }
  76. }
  77. var (
  78. tslice = make(TimeSeries, len(sp.tm))
  79. i int
  80. )
  81. for k, v := range sp.tm {
  82. var lat time.Duration
  83. if v.count > 0 {
  84. lat = time.Duration(v.totalLatency) / time.Duration(v.count)
  85. }
  86. tslice[i] = timeSeries{
  87. timestamp: k,
  88. avgLatency: lat,
  89. throughPut: v.count,
  90. }
  91. i++
  92. }
  93. sort.Sort(tslice)
  94. return tslice
  95. }
  96. func (ts TimeSeries) String() string {
  97. buf := new(bytes.Buffer)
  98. wr := csv.NewWriter(buf)
  99. if err := wr.Write([]string{"unix_ts", "avg_latency", "throughput"}); err != nil {
  100. log.Fatal(err)
  101. }
  102. rows := [][]string{}
  103. for i := range ts {
  104. row := []string{
  105. fmt.Sprintf("%d", ts[i].timestamp),
  106. fmt.Sprintf("%s", ts[i].avgLatency),
  107. fmt.Sprintf("%d", ts[i].throughPut),
  108. }
  109. rows = append(rows, row)
  110. }
  111. if err := wr.WriteAll(rows); err != nil {
  112. log.Fatal(err)
  113. }
  114. wr.Flush()
  115. if err := wr.Error(); err != nil {
  116. log.Fatal(err)
  117. }
  118. return fmt.Sprintf("\nSample in one second (unix latency throughput):\n%s", buf.String())
  119. }