/* * Copyright 2014 Groupon.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* istanbul ignore next */ //ignores inheritance snippet emitted by the TS compiler /// import tsdDef = require("tsdDef"); import tsd = require("tsd-metrics-client"); import tsdUtils = require("./utils"); import sample = require("./tsd-metric-sample"); import units = require("./tsd-units"); import errors = require("./error-reporting"); //aliases import TsdMetricSample = sample.TsdMetricSample; import Units = units.Units; /** * Class for timer. Instances are started on creation and record state when * stopped or closed. * * Each timer instance is bound to a [TsdMetrics]{@linkcode module:tsd-metrics-client~TsdMetrics} instance. After the * [TsdMetrics]{@linkcode module:tsd-metrics-client~TsdMetrics} instance is closed any timing data generated by * Timer instances bound to that [TsdMetrics]{@linkcode module:tsd-metrics-client~TsdMetrics} instance will * be discarded. * * @class * @extends MetricSample * @alias Timer * @author Mohammed Kamel (mkamel at groupon dot com) */ export class TsdTimer extends TsdMetricSample implements tsdDef.Timer { private _isStoppedFlag:boolean = false; private _startTime:number = tsdUtils.getNanoTime(); /** * Constructor. * * @param {string} _name Name of the timer * @param {MetricsStateObject} _metricsStateObject Object holding state of the parent metrics object. * @ignore */ constructor(private _name:string, private _metricsStateObject:tsd.MetricsStateObject) { super(0, Units.NANOSECOND) } /** * Return if the timer was stopped already. * * @method * @return {boolean} Whether the timer was stopped or not. */ public isStopped():boolean { return this._isStoppedFlag; } /** * Stop the timer and record timing data in the associated. * * @method * @emits 'error' if the metrics object is closed or if the timer already stopped. */ public stop():void { var canStop = this._metricsStateObject.assertIsOpen("Cannot stop timer '" + this._name + "'") && errors.assert(!this.isStopped(), "Timer '" + this._name + "' stopped multiple times"); if (canStop) { this._isStoppedFlag = true; (this)._value = tsdUtils.getNanoTime() - this._startTime; } } } /** * Internal class for explicit timers created by [TsdMetrics]{@linkcode module:tsd-metrics-client~TsdMetrics#setTimer}. * * @class * @extends MetricSample * @alias ExplicitTimer * @author Mohammed Kamel (mkamel at groupon dot com) * @ignore */ export class ExplicitTimer extends TsdMetricSample implements tsdDef.Timer { /** * Constructor. * * @param {number} duration The duration of the timer. * @param {Units} unit The unit of time of the duration. * @param _name name of the timer * @param {MetricsStateObject} _metricsStateObject Metrics state object. */ constructor( duration:number, unit:tsdDef.Unit, private _name:string, private _metricsStateObject:tsd.MetricsStateObject) { super(duration, unit); } /** * Do nothing. */ /* istanbul ignore next */ public stop():void { //Do nothing errors.assert(false, "cannot stop and explicitly set timer"); } /** * Always is stopped. */ public isStopped():boolean { return true; } }