github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/stats.go (about)

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