github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/metric/metric_test.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  	"testing"
    18  	"time"
    19  
    20  	. "github.com/insionng/yougam/libraries/pingcap/check"
    21  	"github.com/insionng/yougam/libraries/rcrowley/go-metrics"
    22  )
    23  
    24  func TestT(t *testing.T) {
    25  	TestingT(t)
    26  }
    27  
    28  type testSuite struct{}
    29  
    30  var _ = Suite(&testSuite{})
    31  
    32  const (
    33  	testMetricName     = "test-metric"
    34  	testTimeMetricName = "time-metric"
    35  )
    36  
    37  func (t *testSuite) TestRegMetric(c *C) {
    38  	Register(testMetricName, metrics.NewCounter())
    39  	Register(testMetricName, metrics.NewHistogram(metrics.NewUniformSample(1000)))
    40  
    41  	v := r.Get(testMetricName)
    42  	c.Assert(v, NotNil)
    43  
    44  	// Will not overwrite
    45  	_, ok := v.(metrics.Counter)
    46  	c.Assert(ok, IsTrue)
    47  
    48  	Inc(testMetricName, 1)
    49  	Inc(testMetricName, 1)
    50  	c.Assert(r.Get(testMetricName).(metrics.Counter).Count(), Equals, int64(2))
    51  
    52  	Register(testTimeMetricName, metrics.NewHistogram(metrics.NewUniformSample(1000)))
    53  	start := time.Now()
    54  	time.Sleep(100 * time.Millisecond)
    55  	RecordTime(testTimeMetricName, start)
    56  
    57  	start = time.Now()
    58  	time.Sleep(20 * time.Millisecond)
    59  	RecordTime(testTimeMetricName, start)
    60  
    61  	c.Assert(r.Get(testTimeMetricName).(metrics.Histogram).Max(), GreaterEqual, int64(100))
    62  	c.Assert(r.Get(testTimeMetricName).(metrics.Histogram).Min(), Less, int64(100))
    63  }
    64  
    65  func (t *testSuite) TestTPSMetrics(c *C) {
    66  
    67  	// Test tpsMetrics with manual tick.
    68  	tm := newTPSMetrics()
    69  	for i := 1; i < 10; i++ {
    70  		for j := 0; j < 5; j++ {
    71  			tm.Add(int64(i))
    72  		}
    73  		tm.tick()
    74  		c.Assert(tm.Get(), Equals, int64(i*5))
    75  		tm.tick()
    76  		c.Assert(tm.Get(), Equals, int64(0))
    77  	}
    78  
    79  	// Test TPSMetrics with auto tick.
    80  	m := NewTPSMetrics()
    81  	for i := 1; i < 6; i++ {
    82  		for j := 0; j < 5; j++ {
    83  			m.Add(int64(i))
    84  			time.Sleep(220 * time.Millisecond)
    85  		}
    86  		// It is hard to get the accurate tps because there is another timeline in tpsMetrics.
    87  		// We could only get the upper/lower boundary for tps
    88  		maxTPS := int64(i * 5)
    89  		minTPS := int64((i - 1) * 4)
    90  		c.Assert(m.Get(), LessEqual, maxTPS)
    91  		c.Assert(m.Get(), GreaterEqual, minTPS)
    92  	}
    93  }