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