stats.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package store
  2. import (
  3. "encoding/json"
  4. "sync/atomic"
  5. )
  6. const (
  7. SetSuccess = iota
  8. SetFail
  9. DeleteSuccess
  10. DeleteFail
  11. CreateSuccess
  12. CreateFail
  13. UpdateSuccess
  14. UpdateFail
  15. CompareAndSwapSuccess
  16. CompareAndSwapFail
  17. GetSuccess
  18. GetFail
  19. ExpireCount
  20. )
  21. type Stats struct {
  22. // Number of get requests
  23. GetSuccess uint64 `json:"getsSuccess"`
  24. GetFail uint64 `json:"getsFail"`
  25. // Number of sets requests
  26. SetSuccess uint64 `json:"setsSuccess"`
  27. SetFail uint64 `json:"setsFail"`
  28. // Number of delete requests
  29. DeleteSuccess uint64 `json:"deleteSuccess"`
  30. DeleteFail uint64 `json:"deleteFail"`
  31. // Number of update requests
  32. UpdateSuccess uint64 `json:"updateSuccess"`
  33. UpdateFail uint64 `json:"updateFail"`
  34. // Number of create requests
  35. CreateSuccess uint64 `json:"createSuccess"`
  36. CreateFail uint64 `json:"createFail"`
  37. // Number of testAndSet requests
  38. CompareAndSwapSuccess uint64 `json:"compareAndSwapSuccess"`
  39. CompareAndSwapFail uint64 `json:"compareAndSwapFail"`
  40. ExpireCount uint64 `json:"expireCount"`
  41. Watchers uint64 `json:"watchers"`
  42. }
  43. func newStats() *Stats {
  44. s := new(Stats)
  45. return s
  46. }
  47. func (s *Stats) clone() *Stats {
  48. return &Stats{s.GetSuccess, s.GetFail, s.SetSuccess, s.SetFail,
  49. s.DeleteSuccess, s.DeleteFail, s.UpdateSuccess, s.UpdateFail, s.CreateSuccess,
  50. s.CreateFail, s.CompareAndSwapSuccess, s.CompareAndSwapFail, s.Watchers, s.ExpireCount}
  51. }
  52. // Status() return the statistics info of etcd storage its recent start
  53. func (s *Stats) toJson() []byte {
  54. b, _ := json.Marshal(s)
  55. return b
  56. }
  57. func (s *Stats) TotalReads() uint64 {
  58. return s.GetSuccess + s.GetFail
  59. }
  60. func (s *Stats) TotalWrites() uint64 {
  61. return s.SetSuccess + s.SetFail +
  62. s.DeleteSuccess + s.DeleteFail +
  63. s.CompareAndSwapSuccess + s.CompareAndSwapFail +
  64. s.UpdateSuccess + s.UpdateFail
  65. }
  66. func (s *Stats) Inc(field int) {
  67. switch field {
  68. case SetSuccess:
  69. atomic.AddUint64(&s.SetSuccess, 1)
  70. case SetFail:
  71. atomic.AddUint64(&s.SetFail, 1)
  72. case CreateSuccess:
  73. atomic.AddUint64(&s.CreateSuccess, 1)
  74. case CreateFail:
  75. atomic.AddUint64(&s.CreateFail, 1)
  76. case DeleteSuccess:
  77. atomic.AddUint64(&s.DeleteSuccess, 1)
  78. case DeleteFail:
  79. atomic.AddUint64(&s.DeleteFail, 1)
  80. case GetSuccess:
  81. atomic.AddUint64(&s.GetSuccess, 1)
  82. case GetFail:
  83. atomic.AddUint64(&s.GetFail, 1)
  84. case UpdateSuccess:
  85. atomic.AddUint64(&s.UpdateSuccess, 1)
  86. case UpdateFail:
  87. atomic.AddUint64(&s.UpdateFail, 1)
  88. case CompareAndSwapSuccess:
  89. atomic.AddUint64(&s.CompareAndSwapSuccess, 1)
  90. case CompareAndSwapFail:
  91. atomic.AddUint64(&s.CompareAndSwapFail, 1)
  92. case ExpireCount:
  93. atomic.AddUint64(&s.ExpireCount, 1)
  94. }
  95. }