timeseries.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 CoreOS, Inc.
  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. "sort"
  21. "sync"
  22. "time"
  23. )
  24. type timeSeries struct {
  25. timestamp int64
  26. avgLatency time.Duration
  27. throughPut int64
  28. }
  29. type TimeSeries []timeSeries
  30. func (t TimeSeries) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
  31. func (t TimeSeries) Len() int { return len(t) }
  32. func (t TimeSeries) Less(i, j int) bool { return t[i].timestamp < t[j].timestamp }
  33. type secondPoint struct {
  34. totalLatency time.Duration
  35. count int64
  36. }
  37. type secondPoints struct {
  38. mu sync.Mutex
  39. tm map[int64]secondPoint
  40. }
  41. func newSecondPoints() *secondPoints {
  42. return &secondPoints{tm: make(map[int64]secondPoint)}
  43. }
  44. func (sp *secondPoints) Add(ts time.Time, lat time.Duration) {
  45. sp.mu.Lock()
  46. defer sp.mu.Unlock()
  47. tk := ts.Unix()
  48. if v, ok := sp.tm[tk]; !ok {
  49. sp.tm[tk] = secondPoint{totalLatency: lat, count: 1}
  50. } else {
  51. v.totalLatency += lat
  52. v.count += 1
  53. sp.tm[tk] = v
  54. }
  55. }
  56. func (sp *secondPoints) getTimeSeries() TimeSeries {
  57. sp.mu.Lock()
  58. defer sp.mu.Unlock()
  59. tslice := make(TimeSeries, len(sp.tm))
  60. i := 0
  61. for k, v := range sp.tm {
  62. tslice[i] = timeSeries{
  63. timestamp: k,
  64. avgLatency: time.Duration(v.totalLatency) / time.Duration(v.count),
  65. throughPut: v.count,
  66. }
  67. i++
  68. }
  69. sort.Sort(tslice)
  70. return tslice
  71. }
  72. func (ts TimeSeries) String() string {
  73. buf := new(bytes.Buffer)
  74. wr := csv.NewWriter(buf)
  75. if err := wr.Write([]string{"unix_ts", "avg_latency", "throughput"}); err != nil {
  76. log.Fatal(err)
  77. }
  78. rows := [][]string{}
  79. for i := range ts {
  80. row := []string{
  81. fmt.Sprintf("%d", ts[i].timestamp),
  82. fmt.Sprintf("%s", ts[i].avgLatency),
  83. fmt.Sprintf("%d", ts[i].throughPut),
  84. }
  85. rows = append(rows, row)
  86. }
  87. if err := wr.WriteAll(rows); err != nil {
  88. log.Fatal(err)
  89. }
  90. wr.Flush()
  91. if err := wr.Error(); err != nil {
  92. log.Fatal(err)
  93. }
  94. return fmt.Sprintf("\nSample in one second (unix latency throughput):\n%s", buf.String())
  95. }