stats.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "encoding/json"
  16. "sync/atomic"
  17. )
  18. const (
  19. SetSuccess = iota
  20. SetFail
  21. DeleteSuccess
  22. DeleteFail
  23. CreateSuccess
  24. CreateFail
  25. UpdateSuccess
  26. UpdateFail
  27. CompareAndSwapSuccess
  28. CompareAndSwapFail
  29. GetSuccess
  30. GetFail
  31. ExpireCount
  32. CompareAndDeleteSuccess
  33. CompareAndDeleteFail
  34. )
  35. type Stats struct {
  36. // Number of get requests
  37. GetSuccess uint64 `json:"getsSuccess"`
  38. GetFail uint64 `json:"getsFail"`
  39. // Number of sets requests
  40. SetSuccess uint64 `json:"setsSuccess"`
  41. SetFail uint64 `json:"setsFail"`
  42. // Number of delete requests
  43. DeleteSuccess uint64 `json:"deleteSuccess"`
  44. DeleteFail uint64 `json:"deleteFail"`
  45. // Number of update requests
  46. UpdateSuccess uint64 `json:"updateSuccess"`
  47. UpdateFail uint64 `json:"updateFail"`
  48. // Number of create requests
  49. CreateSuccess uint64 `json:"createSuccess"`
  50. CreateFail uint64 `json:"createFail"`
  51. // Number of testAndSet requests
  52. CompareAndSwapSuccess uint64 `json:"compareAndSwapSuccess"`
  53. CompareAndSwapFail uint64 `json:"compareAndSwapFail"`
  54. // Number of compareAndDelete requests
  55. CompareAndDeleteSuccess uint64 `json:"compareAndDeleteSuccess"`
  56. CompareAndDeleteFail uint64 `json:"compareAndDeleteFail"`
  57. ExpireCount uint64 `json:"expireCount"`
  58. Watchers uint64 `json:"watchers"`
  59. }
  60. func newStats() *Stats {
  61. s := new(Stats)
  62. return s
  63. }
  64. func (s *Stats) clone() *Stats {
  65. return &Stats{s.GetSuccess, s.GetFail, s.SetSuccess, s.SetFail,
  66. s.DeleteSuccess, s.DeleteFail, s.UpdateSuccess, s.UpdateFail, s.CreateSuccess,
  67. s.CreateFail, s.CompareAndSwapSuccess, s.CompareAndSwapFail,
  68. s.CompareAndDeleteSuccess, s.CompareAndDeleteFail, s.Watchers, s.ExpireCount}
  69. }
  70. // Status() return the statistics info of etcd storage its recent start
  71. func (s *Stats) toJson() []byte {
  72. b, _ := json.Marshal(s)
  73. return b
  74. }
  75. func (s *Stats) TotalReads() uint64 {
  76. return s.GetSuccess + s.GetFail
  77. }
  78. func (s *Stats) TotalTranscations() uint64 {
  79. return s.SetSuccess + s.SetFail +
  80. s.DeleteSuccess + s.DeleteFail +
  81. s.CompareAndSwapSuccess + s.CompareAndSwapFail +
  82. s.CompareAndDeleteSuccess + s.CompareAndDeleteFail +
  83. s.UpdateSuccess + s.UpdateFail
  84. }
  85. func (s *Stats) Inc(field int) {
  86. switch field {
  87. case SetSuccess:
  88. atomic.AddUint64(&s.SetSuccess, 1)
  89. case SetFail:
  90. atomic.AddUint64(&s.SetFail, 1)
  91. case CreateSuccess:
  92. atomic.AddUint64(&s.CreateSuccess, 1)
  93. case CreateFail:
  94. atomic.AddUint64(&s.CreateFail, 1)
  95. case DeleteSuccess:
  96. atomic.AddUint64(&s.DeleteSuccess, 1)
  97. case DeleteFail:
  98. atomic.AddUint64(&s.DeleteFail, 1)
  99. case GetSuccess:
  100. atomic.AddUint64(&s.GetSuccess, 1)
  101. case GetFail:
  102. atomic.AddUint64(&s.GetFail, 1)
  103. case UpdateSuccess:
  104. atomic.AddUint64(&s.UpdateSuccess, 1)
  105. case UpdateFail:
  106. atomic.AddUint64(&s.UpdateFail, 1)
  107. case CompareAndSwapSuccess:
  108. atomic.AddUint64(&s.CompareAndSwapSuccess, 1)
  109. case CompareAndSwapFail:
  110. atomic.AddUint64(&s.CompareAndSwapFail, 1)
  111. case CompareAndDeleteSuccess:
  112. atomic.AddUint64(&s.CompareAndDeleteSuccess, 1)
  113. case CompareAndDeleteFail:
  114. atomic.AddUint64(&s.CompareAndDeleteFail, 1)
  115. case ExpireCount:
  116. atomic.AddUint64(&s.ExpireCount, 1)
  117. }
  118. }