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