import { MetricType } from '../MetricType' ; import { IReservoir , ReservoirType } from '../Reservoir' ; import { BaseMeter , IMeter } from './Meter' ; import { BaseHistogram , IHistogram } from './Histogram' ; import { Metered , Sampling , Snapshot } from '../domain' ; /**
 * A timer metric which aggregates timing durations and provides duration statistics, plus
 * throughput statistics via  BaseMeter .
 * Timer相当于Meter+Histogram的组合，同时统计一段代码，一个方法的qps，以及执行时间的分布情况
  export interface ITimer extends Metered , Sampling { param duration update ( duration ) ; } /**
 * A timer metric which aggregates timing durations and provides duration statistics, plus
 * throughput statistics via  Meter .
  export class BaseTimer implements ITimer { type = MetricType . TIMER ; meter : IMeter ; histogram : IHistogram ; constructor ( interval = 60 , reservoir = ReservoirType . EXPONENTIALLY_DECAYING ) { this . meter = new BaseMeter ( interval ) ; this . histogram = new BaseHistogram ( reservoir , interval , 10 ) ; } getCount ( ) { return < number > this . histogram . getCount ( ) ; } getFifteenMinuteRate ( ) { return this . meter . getFifteenMinuteRate ( ) ; } getFiveMinuteRate ( ) { return this . meter . getFiveMinuteRate ( ) ; } getMeanRate ( ) { return this . meter . getMeanRate ( ) ; } getOneMinuteRate ( ) { return this . meter . getOneMinuteRate ( ) ; } getSnapshot ( ) { return this . histogram . getSnapshot ( ) ; } getInstantCountInterval ( ) { return this . meter . getInstantCountInterval ( ) ; } getInstantCount ( startTime ? ) { return this . meter . getInstantCount ( startTime ) ; } update ( duration ) { if ( duration >= 0 ) { this . histogram . update ( duration ) ; this . meter . mark ( ) ; } } }