Path: blob/trunk/third_party/closure/goog/stats/basicstat.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A basic statistics tracker.16*17*/1819goog.provide('goog.stats.BasicStat');2021goog.require('goog.asserts');22goog.require('goog.log');23goog.require('goog.string.format');24goog.require('goog.structs.CircularBuffer');25262728/**29* Tracks basic statistics over a specified time interval.30*31* Statistics are kept in a fixed number of slots, each representing32* an equal portion of the time interval.33*34* Most methods optionally allow passing in the current time, so that35* higher level stats can synchronize operations on multiple child36* objects. Under normal usage, the default of goog.now() should be37* sufficient.38*39* @param {number} interval The stat interval, in milliseconds.40* @constructor41* @final42*/43goog.stats.BasicStat = function(interval) {44goog.asserts.assert(interval > 50);4546/**47* The time interval that this statistic aggregates over.48* @type {number}49* @private50*/51this.interval_ = interval;5253/**54* The number of milliseconds in each slot.55* @type {number}56* @private57*/58this.slotInterval_ = Math.floor(interval / goog.stats.BasicStat.NUM_SLOTS_);5960/**61* The array of slots.62* @type {goog.structs.CircularBuffer}63* @private64*/65this.slots_ =66new goog.structs.CircularBuffer(goog.stats.BasicStat.NUM_SLOTS_);67};686970/**71* The number of slots. This value limits the accuracy of the get()72* method to (this.interval_ / NUM_SLOTS). A 1-minute statistic would73* be accurate to within 2 seconds.74* @type {number}75* @private76*/77goog.stats.BasicStat.NUM_SLOTS_ = 50;787980/**81* @type {goog.log.Logger}82* @private83*/84goog.stats.BasicStat.prototype.logger_ =85goog.log.getLogger('goog.stats.BasicStat');868788/**89* @return {number} The interval which over statistics are being90* accumulated, in milliseconds.91*/92goog.stats.BasicStat.prototype.getInterval = function() {93return this.interval_;94};959697/**98* Increments the count of this statistic by the specified amount.99*100* @param {number} amt The amount to increase the count by.101* @param {number=} opt_now The time, in milliseconds, to be treated102* as the "current" time. The current time must always be greater103* than or equal to the last time recorded by this stat tracker.104*/105goog.stats.BasicStat.prototype.incBy = function(amt, opt_now) {106var now = opt_now ? opt_now : goog.now();107this.checkForTimeTravel_(now);108var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.getLast());109if (!slot || now >= slot.end) {110slot = new goog.stats.BasicStat.Slot_(this.getSlotBoundary_(now));111this.slots_.add(slot);112}113slot.count += amt;114slot.min = Math.min(amt, slot.min);115slot.max = Math.max(amt, slot.max);116};117118119/**120* Returns the count of the statistic over its configured time121* interval.122* @param {number=} opt_now The time, in milliseconds, to be treated123* as the "current" time. The current time must always be greater124* than or equal to the last time recorded by this stat tracker.125* @return {number} The total count over the tracked interval.126*/127goog.stats.BasicStat.prototype.get = function(opt_now) {128return this.reduceSlots_(129opt_now, function(sum, slot) { return sum + slot.count; }, 0);130};131132133/**134* Returns the magnitute of the largest atomic increment that occurred135* during the watched time interval.136* @param {number=} opt_now The time, in milliseconds, to be treated137* as the "current" time. The current time must always be greater138* than or equal to the last time recorded by this stat tracker.139* @return {number} The maximum count of this statistic.140*/141goog.stats.BasicStat.prototype.getMax = function(opt_now) {142return this.reduceSlots_(opt_now, function(max, slot) {143return Math.max(max, slot.max);144}, Number.MIN_VALUE);145};146147148/**149* Returns the magnitute of the smallest atomic increment that150* occurred during the watched time interval.151* @param {number=} opt_now The time, in milliseconds, to be treated152* as the "current" time. The current time must always be greater153* than or equal to the last time recorded by this stat tracker.154* @return {number} The minimum count of this statistic.155*/156goog.stats.BasicStat.prototype.getMin = function(opt_now) {157return this.reduceSlots_(opt_now, function(min, slot) {158return Math.min(min, slot.min);159}, Number.MAX_VALUE);160};161162163/**164* Passes each active slot into a function and accumulates the result.165*166* @param {number|undefined} now The current time, in milliseconds.167* @param {function(number, goog.stats.BasicStat.Slot_): number} func168* The function to call for every active slot. This function169* takes two arguments: the previous result and the new slot to170* include in the reduction.171* @param {number} val The initial value for the reduction.172* @return {number} The result of the reduction.173* @private174*/175goog.stats.BasicStat.prototype.reduceSlots_ = function(now, func, val) {176now = now || goog.now();177this.checkForTimeTravel_(now);178var rval = val;179var start = this.getSlotBoundary_(now) - this.interval_;180for (var i = this.slots_.getCount() - 1; i >= 0; --i) {181var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.get(i));182if (slot.end <= start) {183break;184}185rval = func(rval, slot);186}187return rval;188};189190191/**192* Computes the end time for the slot that should contain the count193* around the given time. This method ensures that every bucket is194* aligned on a "this.slotInterval_" millisecond boundary.195* @param {number} time The time to compute a boundary for.196* @return {number} The computed boundary.197* @private198*/199goog.stats.BasicStat.prototype.getSlotBoundary_ = function(time) {200return this.slotInterval_ * (Math.floor(time / this.slotInterval_) + 1);201};202203204/**205* Checks that time never goes backwards. If it does (for example,206* the user changes their system clock), the object state is cleared.207* @param {number} now The current time, in milliseconds.208* @private209*/210goog.stats.BasicStat.prototype.checkForTimeTravel_ = function(now) {211var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.getLast());212if (slot) {213var slotStart = slot.end - this.slotInterval_;214if (now < slotStart) {215goog.log.warning(216this.logger_,217goog.string.format(218'Went backwards in time: now=%d, slotStart=%d. Resetting state.',219now, slotStart));220this.reset_();221}222}223};224225226/**227* Clears any statistics tracked by this object, as though it were228* freshly created.229* @private230*/231goog.stats.BasicStat.prototype.reset_ = function() {232this.slots_.clear();233};234235236237/**238* A struct containing information for each sub-interval.239* @param {number} end The end time for this slot, in milliseconds.240* @constructor241* @private242*/243goog.stats.BasicStat.Slot_ = function(end) {244/**245* End time of this slot, exclusive.246* @type {number}247*/248this.end = end;249};250251252/**253* Aggregated count within this slot.254* @type {number}255*/256goog.stats.BasicStat.Slot_.prototype.count = 0;257258259/**260* The smallest atomic increment of the count within this slot.261* @type {number}262*/263goog.stats.BasicStat.Slot_.prototype.min = Number.MAX_VALUE;264265266/**267* The largest atomic increment of the count within this slot.268* @type {number}269*/270goog.stats.BasicStat.Slot_.prototype.max = Number.MIN_VALUE;271272273