Debian Codegraph @

closure-library/closure/goog/stats/basicstat_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.stats.BasicStatTest');
16 goog.setTestOnly('goog.stats.BasicStatTest');
17
18 goog.require('goog.array');
19 goog.require('goog.stats.BasicStat');
20 goog.require('goog.string.format');
21 goog.require('goog.testing.PseudoRandom');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.userAgent');
24
25 function testGetSlotBoundary() {
26 var stat = new goog.stats.BasicStat(1654);
27 assertEquals('Checking interval', 33, stat.slotInterval_);
28
29 assertEquals(132, stat.getSlotBoundary_(125));
30 assertEquals(165, stat.getSlotBoundary_(132));
31 assertEquals(132, stat.getSlotBoundary_(99));
32 assertEquals(99, stat.getSlotBoundary_(98));
33 }
34
35 function testCheckForTimeTravel() {
36 var stat = new goog.stats.BasicStat(1000);
37
38 // no slots yet, should always be OK
39 stat.checkForTimeTravel_(100);
40 stat.checkForTimeTravel_(-1);
41
42 stat.incBy(1, 125); // creates a first bucket, ending at t=140
43
44 // Even though these go backwards in time, our basic fuzzy check passes
45 // because we just check that the time is within the latest interval bucket.
46 stat.checkForTimeTravel_(141);
47 stat.checkForTimeTravel_(140);
48 stat.checkForTimeTravel_(139);
49 stat.checkForTimeTravel_(125);
50 stat.checkForTimeTravel_(124);
51 stat.checkForTimeTravel_(120);
52
53 // State should still be the same, all of the above times are valid.
54 assertEquals('State unchanged when called with good times', 1, stat.get(125));
55
56 stat.checkForTimeTravel_(119);
57 assertEquals('Reset after called with a bad time', 0, stat.get(125));
58 }
59
60 function testConstantIncrementPerSlot() {
61 var stat = new goog.stats.BasicStat(1000);
62
63 var now = 1000;
64 for (var i = 0; i < 50; ++i) {
65 var newMax = 1000 + i;
66 var newMin = 1000 - i;
67 stat.incBy(newMin, now);
68 stat.incBy(newMax, now);
69
70 var msg = goog.string.format(
71 'now=%d i=%d newMin=%d newMax=%d', now, i, newMin, newMax);
72 assertEquals(msg, 2000 * (i + 1), stat.get(now));
73 assertEquals(msg, newMax, stat.getMax(now));
74 assertEquals(msg, newMin, stat.getMin(now));
75
76 now += 20; // push into the next slots
77 }
78
79 // The next increment should cause old data to fall off.
80 stat.incBy(1, now);
81 assertEquals(2000 * 49 + 1, stat.get(now));
82 assertEquals(1, stat.getMin(now));
83 assertEquals(1049, stat.getMax(now));
84
85 now += 20; // drop off another bucket
86 stat.incBy(1, now);
87 assertEquals(2000 * 48 + 2, stat.get(now));
88 assertEquals(1, stat.getMin(now));
89 assertEquals(1049, stat.getMax(now));
90 }
91
92 function testSparseBuckets() {
93 var stat = new goog.stats.BasicStat(1000);
94 var now = 1000;
95
96 stat.incBy(10, now);
97 assertEquals(10, stat.get(now));
98
99 now += 5000; // the old slot is now still in memory, but should be ignored
100 stat.incBy(1, now);
101 assertEquals(1, stat.get(now));
102 }
103
104 function testFuzzy() {
105 var stat = new goog.stats.BasicStat(1000);
106 var test = new PerfectlySlowStat(1000);
107 var rand = new goog.testing.PseudoRandom(58849020);
108 var eventCount = 0;
109
110 // test over 5 simulated seconds (2 for IE, due to timeouts)
111 var simulationDuration = goog.userAgent.IE ? 2000 : 5000;
112 for (var now = 1000; now < simulationDuration;) {
113 var count = Math.floor(rand.random() * 2147483648);
114 var delay = Math.floor(rand.random() * 25);
115 for (var i = 0; i <= delay; ++i) {
116 var time = now + i;
117 var msg = goog.string.format('now=%d eventCount=%d', time, eventCount);
118 var expected = test.getStats(now + i);
119 assertEquals(expected.count, stat.get(time));
120 assertEquals(expected.min, stat.getMin(time));
121 assertEquals(expected.max, stat.getMax(time));
122 }
123
124 now += delay;
125 stat.incBy(count, now);
126 test.incBy(count, now);
127 eventCount++;
128 }
129 }
130
131
132
133 /**
134 * A horribly inefficient implementation of BasicStat that stores
135 * every event in an array and dynamically filters to perform
136 * aggregations.
137 * @constructor
138 */
139 var PerfectlySlowStat = function(interval) {
140 this.interval_ = interval;
141 this.slotSize_ = Math.floor(interval / goog.stats.BasicStat.NUM_SLOTS_);
142 this.events_ = [];
143 };
144
145 PerfectlySlowStat.prototype.incBy = function(amt, now) {
146 this.events_.push({'time': now, 'count': amt});
147 };
148
149 PerfectlySlowStat.prototype.getStats = function(now) {
150 var end = Math.floor(now / this.slotSize_) * this.slotSize_ + this.slotSize_;
151 var start = end - this.interval_;
152 var events =
153 goog.array.filter(this.events_, function(e) { return e.time >= start });
154 return {
155 'count':
156 goog.array.reduce(events, function(sum, e) { return sum + e.count }, 0),
157 'min': goog.array.reduce(
158 events, function(min, e) { return Math.min(min, e.count); },
159 Number.MAX_VALUE),
160 'max': goog.array.reduce(
161 events, function(max, e) { return Math.max(max, e.count); },
162 Number.MIN_VALUE)
163 };
164 };