github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/timer.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  package metrics
    10  
    11  import (
    12  	"sync"
    13  	"time"
    14  )
    15  
    16  //计时器捕获事件的持续时间和速率。
    17  type Timer interface {
    18  	Count() int64
    19  	Max() int64
    20  	Mean() float64
    21  	Min() int64
    22  	Percentile(float64) float64
    23  	Percentiles([]float64) []float64
    24  	Rate1() float64
    25  	Rate5() float64
    26  	Rate15() float64
    27  	RateMean() float64
    28  	Snapshot() Timer
    29  	StdDev() float64
    30  	Stop()
    31  	Sum() int64
    32  	Time(func())
    33  	Update(time.Duration)
    34  	UpdateSince(time.Time)
    35  	Variance() float64
    36  }
    37  
    38  //GetOrRegisterTimer返回现有计时器或构造并注册
    39  //新标准计时器。
    40  //一旦没有必要从注册表中注销仪表
    41  //允许垃圾收集。
    42  func GetOrRegisterTimer(name string, r Registry) Timer {
    43  	if nil == r {
    44  		r = DefaultRegistry
    45  	}
    46  	return r.GetOrRegister(name, NewTimer).(Timer)
    47  }
    48  
    49  //NewCustomTimer从柱状图和仪表构造新的StandardTimer。
    50  //确保在计时器不允许垃圾收集时调用stop()。
    51  func NewCustomTimer(h Histogram, m Meter) Timer {
    52  	if !Enabled {
    53  		return NilTimer{}
    54  	}
    55  	return &StandardTimer{
    56  		histogram: h,
    57  		meter:     m,
    58  	}
    59  }
    60  
    61  //NewRegisteredTimer构造并注册新的StandardTimer。
    62  //一旦没有必要从注册表中注销仪表
    63  //允许垃圾收集。
    64  func NewRegisteredTimer(name string, r Registry) Timer {
    65  	c := NewTimer()
    66  	if nil == r {
    67  		r = DefaultRegistry
    68  	}
    69  	r.Register(name, c)
    70  	return c
    71  }
    72  
    73  //NewTimer使用指数衰减构造新的StandardTimer
    74  //具有与Unix平均负荷相同的水库大小和alpha的样本。
    75  //确保在计时器不允许垃圾收集时调用stop()。
    76  func NewTimer() Timer {
    77  	if !Enabled {
    78  		return NilTimer{}
    79  	}
    80  	return &StandardTimer{
    81  		histogram: NewHistogram(NewExpDecaySample(1028, 0.015)),
    82  		meter:     NewMeter(),
    83  	}
    84  }
    85  
    86  //niltimer是一个无操作计时器。
    87  type NilTimer struct {
    88  	h Histogram
    89  	m Meter
    90  }
    91  
    92  //计数是不允许的。
    93  func (NilTimer) Count() int64 { return 0 }
    94  
    95  //马克斯不是一个OP。
    96  func (NilTimer) Max() int64 { return 0 }
    97  
    98  //平均值是不允许的。
    99  func (NilTimer) Mean() float64 { return 0.0 }
   100  
   101  //min是NO-OP。
   102  func (NilTimer) Min() int64 { return 0 }
   103  
   104  //百分位数是不允许的。
   105  func (NilTimer) Percentile(p float64) float64 { return 0.0 }
   106  
   107  //百分位数是不允许的。
   108  func (NilTimer) Percentiles(ps []float64) []float64 {
   109  	return make([]float64, len(ps))
   110  }
   111  
   112  //RATE1是不可操作的。
   113  func (NilTimer) Rate1() float64 { return 0.0 }
   114  
   115  //RATE5是不允许的。
   116  func (NilTimer) Rate5() float64 { return 0.0 }
   117  
   118  //RATE15是不允许的。
   119  func (NilTimer) Rate15() float64 { return 0.0 }
   120  
   121  //RateMean是一个不允许的人。
   122  func (NilTimer) RateMean() float64 { return 0.0 }
   123  
   124  //快照是不可操作的。
   125  func (NilTimer) Snapshot() Timer { return NilTimer{} }
   126  
   127  //stdev是一个no-op。
   128  func (NilTimer) StdDev() float64 { return 0.0 }
   129  
   130  //停止是不允许的。
   131  func (NilTimer) Stop() {}
   132  
   133  //和是一个NO-op.
   134  func (NilTimer) Sum() int64 { return 0 }
   135  
   136  //时间是不允许的。
   137  func (NilTimer) Time(func()) {}
   138  
   139  //更新是不可操作的。
   140  func (NilTimer) Update(time.Duration) {}
   141  
   142  //updateSince是一个no-op。
   143  func (NilTimer) UpdateSince(time.Time) {}
   144  
   145  //方差是不可操作的。
   146  func (NilTimer) Variance() float64 { return 0.0 }
   147  
   148  //StandardTimer是计时器的标准实现,使用柱状图
   149  //和米。
   150  type StandardTimer struct {
   151  	histogram Histogram
   152  	meter     Meter
   153  	mutex     sync.Mutex
   154  }
   155  
   156  //count返回记录的事件数。
   157  func (t *StandardTimer) Count() int64 {
   158  	return t.histogram.Count()
   159  }
   160  
   161  //max返回样本中的最大值。
   162  func (t *StandardTimer) Max() int64 {
   163  	return t.histogram.Max()
   164  }
   165  
   166  //mean返回样本值的平均值。
   167  func (t *StandardTimer) Mean() float64 {
   168  	return t.histogram.Mean()
   169  }
   170  
   171  //Min返回样本中的最小值。
   172  func (t *StandardTimer) Min() int64 {
   173  	return t.histogram.Min()
   174  }
   175  
   176  //Percentile返回样本中任意百分位数的值。
   177  func (t *StandardTimer) Percentile(p float64) float64 {
   178  	return t.histogram.Percentile(p)
   179  }
   180  
   181  //Percentiles返回
   182  //样品。
   183  func (t *StandardTimer) Percentiles(ps []float64) []float64 {
   184  	return t.histogram.Percentiles(ps)
   185  }
   186  
   187  //Rate1返回每秒事件的一分钟移动平均速率。
   188  func (t *StandardTimer) Rate1() float64 {
   189  	return t.meter.Rate1()
   190  }
   191  
   192  //Rate5返回每秒事件的5分钟移动平均速率。
   193  func (t *StandardTimer) Rate5() float64 {
   194  	return t.meter.Rate5()
   195  }
   196  
   197  //Rate15返回每秒事件的15分钟移动平均速率。
   198  func (t *StandardTimer) Rate15() float64 {
   199  	return t.meter.Rate15()
   200  }
   201  
   202  //RateMean返回仪表每秒事件的平均速率。
   203  func (t *StandardTimer) RateMean() float64 {
   204  	return t.meter.RateMean()
   205  }
   206  
   207  //快照返回计时器的只读副本。
   208  func (t *StandardTimer) Snapshot() Timer {
   209  	t.mutex.Lock()
   210  	defer t.mutex.Unlock()
   211  	return &TimerSnapshot{
   212  		histogram: t.histogram.Snapshot().(*HistogramSnapshot),
   213  		meter:     t.meter.Snapshot().(*MeterSnapshot),
   214  	}
   215  }
   216  
   217  //stdev返回样本值的标准偏差。
   218  func (t *StandardTimer) StdDev() float64 {
   219  	return t.histogram.StdDev()
   220  }
   221  
   222  //停止停止计时表。
   223  func (t *StandardTimer) Stop() {
   224  	t.meter.Stop()
   225  }
   226  
   227  //sum返回样本中的和。
   228  func (t *StandardTimer) Sum() int64 {
   229  	return t.histogram.Sum()
   230  }
   231  
   232  //记录给定函数执行的持续时间。
   233  func (t *StandardTimer) Time(f func()) {
   234  	ts := time.Now()
   235  	f()
   236  	t.Update(time.Since(ts))
   237  }
   238  
   239  //记录事件的持续时间。
   240  func (t *StandardTimer) Update(d time.Duration) {
   241  	t.mutex.Lock()
   242  	defer t.mutex.Unlock()
   243  	t.histogram.Update(int64(d))
   244  	t.meter.Mark(1)
   245  }
   246  
   247  //记录一次开始到现在结束的事件的持续时间。
   248  func (t *StandardTimer) UpdateSince(ts time.Time) {
   249  	t.mutex.Lock()
   250  	defer t.mutex.Unlock()
   251  	t.histogram.Update(int64(time.Since(ts)))
   252  	t.meter.Mark(1)
   253  }
   254  
   255  //方差返回样本中值的方差。
   256  func (t *StandardTimer) Variance() float64 {
   257  	return t.histogram.Variance()
   258  }
   259  
   260  //TimersSnapshot是另一个计时器的只读副本。
   261  type TimerSnapshot struct {
   262  	histogram *HistogramSnapshot
   263  	meter     *MeterSnapshot
   264  }
   265  
   266  //count返回快照时记录的事件数
   267  //拿。
   268  func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
   269  
   270  //max返回快照拍摄时的最大值。
   271  func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
   272  
   273  //mean返回拍摄快照时的平均值。
   274  func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
   275  
   276  //Min返回拍摄快照时的最小值。
   277  func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
   278  
   279  //percentile返回在
   280  //已拍摄快照。
   281  func (t *TimerSnapshot) Percentile(p float64) float64 {
   282  	return t.histogram.Percentile(p)
   283  }
   284  
   285  //Percentiles返回在
   286  //拍摄快照的时间。
   287  func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
   288  	return t.histogram.Percentiles(ps)
   289  }
   290  
   291  //RATE1返回在
   292  //拍摄快照的时间。
   293  func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
   294  
   295  //RATE5返回每秒5分钟的移动平均事件速率
   296  //拍摄快照的时间。
   297  func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
   298  
   299  //Rate15返回每秒15分钟的事件移动平均速率
   300  //拍摄快照时。
   301  func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
   302  
   303  //RateMean返回在
   304  //已拍摄快照。
   305  func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
   306  
   307  //快照返回快照。
   308  func (t *TimerSnapshot) Snapshot() Timer { return t }
   309  
   310  //stdev返回快照时值的标准偏差
   311  //被带走了。
   312  func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
   313  
   314  //停止是不允许的。
   315  func (t *TimerSnapshot) Stop() {}
   316  
   317  //sum返回拍摄快照时的总和。
   318  func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }
   319  
   320  //时间恐慌。
   321  func (*TimerSnapshot) Time(func()) {
   322  	panic("Time called on a TimerSnapshot")
   323  }
   324  
   325  //更新恐慌。
   326  func (*TimerSnapshot) Update(time.Duration) {
   327  	panic("Update called on a TimerSnapshot")
   328  }
   329  
   330  //更新自恐慌。
   331  func (*TimerSnapshot) UpdateSince(time.Time) {
   332  	panic("UpdateSince called on a TimerSnapshot")
   333  }
   334  
   335  //variance返回快照时值的方差
   336  //拿。
   337  func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }