stats.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2015 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 v2store
  15. import (
  16. "encoding/json"
  17. "sync/atomic"
  18. )
  19. const (
  20. SetSuccess = iota
  21. SetFail
  22. DeleteSuccess
  23. DeleteFail
  24. CreateSuccess
  25. CreateFail
  26. UpdateSuccess
  27. UpdateFail
  28. CompareAndSwapSuccess
  29. CompareAndSwapFail
  30. GetSuccess
  31. GetFail
  32. ExpireCount
  33. CompareAndDeleteSuccess
  34. CompareAndDeleteFail
  35. )
  36. type Stats struct {
  37. // Number of get requests
  38. GetSuccess uint64 `json:"getsSuccess"`
  39. GetFail uint64 `json:"getsFail"`
  40. // Number of sets requests
  41. SetSuccess uint64 `json:"setsSuccess"`
  42. SetFail uint64 `json:"setsFail"`
  43. // Number of delete requests
  44. DeleteSuccess uint64 `json:"deleteSuccess"`
  45. DeleteFail uint64 `json:"deleteFail"`
  46. // Number of update requests
  47. UpdateSuccess uint64 `json:"updateSuccess"`
  48. UpdateFail uint64 `json:"updateFail"`
  49. // Number of create requests
  50. CreateSuccess uint64 `json:"createSuccess"`
  51. CreateFail uint64 `json:"createFail"`
  52. // Number of testAndSet requests
  53. CompareAndSwapSuccess uint64 `json:"compareAndSwapSuccess"`
  54. CompareAndSwapFail uint64 `json:"compareAndSwapFail"`
  55. // Number of compareAndDelete requests
  56. CompareAndDeleteSuccess uint64 `json:"compareAndDeleteSuccess"`
  57. CompareAndDeleteFail uint64 `json:"compareAndDeleteFail"`
  58. ExpireCount uint64 `json:"expireCount"`
  59. Watchers uint64 `json:"watchers"`
  60. }
  61. func newStats() *Stats {
  62. s := new(Stats)
  63. return s
  64. }
  65. func (s *Stats) clone() *Stats {
  66. return &Stats{
  67. GetSuccess: s.GetSuccess,
  68. GetFail: s.GetFail,
  69. SetSuccess: s.SetSuccess,
  70. SetFail: s.SetFail,
  71. DeleteSuccess: s.DeleteSuccess,
  72. DeleteFail: s.DeleteFail,
  73. UpdateSuccess: s.UpdateSuccess,
  74. UpdateFail: s.UpdateFail,
  75. CreateSuccess: s.CreateSuccess,
  76. CreateFail: s.CreateFail,
  77. CompareAndSwapSuccess: s.CompareAndSwapSuccess,
  78. CompareAndSwapFail: s.CompareAndSwapFail,
  79. CompareAndDeleteSuccess: s.CompareAndDeleteSuccess,
  80. CompareAndDeleteFail: s.CompareAndDeleteFail,
  81. ExpireCount: s.ExpireCount,
  82. Watchers: s.Watchers,
  83. }
  84. }
  85. func (s *Stats) toJson() []byte {
  86. b, _ := json.Marshal(s)
  87. return b
  88. }
  89. func (s *Stats) Inc(field int) {
  90. switch field {
  91. case SetSuccess:
  92. atomic.AddUint64(&s.SetSuccess, 1)
  93. case SetFail:
  94. atomic.AddUint64(&s.SetFail, 1)
  95. case CreateSuccess:
  96. atomic.AddUint64(&s.CreateSuccess, 1)
  97. case CreateFail:
  98. atomic.AddUint64(&s.CreateFail, 1)
  99. case DeleteSuccess:
  100. atomic.AddUint64(&s.DeleteSuccess, 1)
  101. case DeleteFail:
  102. atomic.AddUint64(&s.DeleteFail, 1)
  103. case GetSuccess:
  104. atomic.AddUint64(&s.GetSuccess, 1)
  105. case GetFail:
  106. atomic.AddUint64(&s.GetFail, 1)
  107. case UpdateSuccess:
  108. atomic.AddUint64(&s.UpdateSuccess, 1)
  109. case UpdateFail:
  110. atomic.AddUint64(&s.UpdateFail, 1)
  111. case CompareAndSwapSuccess:
  112. atomic.AddUint64(&s.CompareAndSwapSuccess, 1)
  113. case CompareAndSwapFail:
  114. atomic.AddUint64(&s.CompareAndSwapFail, 1)
  115. case CompareAndDeleteSuccess:
  116. atomic.AddUint64(&s.CompareAndDeleteSuccess, 1)
  117. case CompareAndDeleteFail:
  118. atomic.AddUint64(&s.CompareAndDeleteFail, 1)
  119. case ExpireCount:
  120. atomic.AddUint64(&s.ExpireCount, 1)
  121. }
  122. }