github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/metric/metric.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package metric
    15  
    16  import (
    17  	"os"
    18  	"sync"
    19  	"time"
    20  
    21  	"github.com/insionng/yougam/libraries/rcrowley/go-metrics"
    22  )
    23  
    24  var (
    25  	r    = metrics.NewRegistry()
    26  	once sync.Once
    27  )
    28  
    29  // Register registers a new metric for observation.
    30  func Register(name string, m interface{}) {
    31  	r.Register(name, m)
    32  }
    33  
    34  // Inc increases specific counter metric.
    35  func Inc(name string, i int64) {
    36  	if c := r.GetOrRegister(name, metrics.NewCounter()); c != nil {
    37  		c.(metrics.Counter).Inc(i)
    38  	}
    39  }
    40  
    41  // RecordTime records time elapse from startTime for given metric.
    42  func RecordTime(name string, startTime time.Time) {
    43  	if h := r.GetOrRegister(name, metrics.NewHistogram(metrics.NewUniformSample(100))); h != nil {
    44  		elapse := time.Since(startTime).Nanoseconds() / int64(time.Millisecond)
    45  		h.(metrics.Histogram).Update(elapse)
    46  	}
    47  }
    48  
    49  // RunMetric reports metric result over a given time interval.
    50  func RunMetric(interval time.Duration) {
    51  	once.Do(func() {
    52  		go func() {
    53  			metrics.Write(r, interval, os.Stdout)
    54  		}()
    55  	})
    56  }
    57  
    58  // TPSMetrics is the metrics for tps (Transaction Per Second)
    59  type TPSMetrics interface {
    60  	// Add c transactions
    61  	Add(c int64)
    62  	// Get current tps
    63  	Get() int64
    64  }
    65  
    66  // Simple tps metrics
    67  // Accumulate txn count in a second and reset the counter at each second.
    68  type tpsMetrics struct {
    69  	meter metrics.Counter
    70  	tps   int64
    71  	mu    sync.Mutex
    72  }
    73  
    74  func (tm *tpsMetrics) Add(c int64) {
    75  	tm.meter.Inc(c)
    76  }
    77  
    78  func (tm *tpsMetrics) Get() int64 {
    79  	tm.mu.Lock()
    80  	defer tm.mu.Unlock()
    81  	return tm.tps
    82  }
    83  
    84  func (tm *tpsMetrics) tick() {
    85  	tm.mu.Lock()
    86  	defer tm.mu.Unlock()
    87  	t := tm.meter.Count()
    88  	tm.meter.Clear()
    89  	tm.tps = t
    90  
    91  }
    92  
    93  func (tm *tpsMetrics) updateTPS() {
    94  	for {
    95  		tm.tick()
    96  		time.Sleep(1 * time.Second)
    97  	}
    98  }
    99  
   100  func newTPSMetrics() *tpsMetrics {
   101  	return &tpsMetrics{
   102  		meter: metrics.NewCounter(),
   103  	}
   104  }
   105  
   106  // NewTPSMetrics creates a tpsMetrics and starts its ticker.
   107  func NewTPSMetrics() TPSMetrics {
   108  	m := newTPSMetrics()
   109  	// Tick and update tps
   110  	go m.updateTPS()
   111  	return m
   112  }