This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 419
/
metric-test.js
209 lines (183 loc) · 6.33 KB
/
metric-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var vows = require("vows"),
assert = require("assert"),
test = require("./test"),
event = require("../lib/cube/server/event"),
metric = require("../lib/cube/server/metric");
var suite = vows.describe("metric");
var steps = {
3e5: function(date, n) { return new Date((Math.floor(date / 3e5) + n) * 3e5); },
36e5: function(date, n) { return new Date((Math.floor(date / 36e5) + n) * 36e5); },
864e5: function(date, n) { return new Date((Math.floor(date / 864e5) + n) * 864e5); },
6048e5: function(date, n) { (date = steps[864e5](date, n * 7)).setUTCDate(date.getUTCDate() - date.getUTCDay()); return date; },
2592e6: function(date, n) { return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + n, 1)); }
};
steps[3e5].description = "5-minute";
steps[36e5].description = "1-hour";
steps[864e5].description = "1-day";
steps[6048e5].description = "1-week";
steps[2592e6].description = "1-month";
suite.addBatch(test.batch({
topic: function(test) {
var emit = event.putter(test.db);
for (var i = 0; i < 2500; i++) {
emit({
type: "test",
time: new Date(Date.UTC(2011, 6, 18, 0, Math.sqrt(i) - 10)).toISOString(),
data: {i: i}
});
}
return metric.getter(test.db);
},
"unary expression": metricTest({
expression: "sum(test)",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z",
}, {
3e5: [0, 17, 65, 143, 175, 225, 275, 325, 375, 425, 475, 0, 0],
36e5: [82, 2418],
864e5: [82, 2418],
6048e5: [2500],
2592e6: [2500]
}
),
"unary expression with data accessor": metricTest({
expression: "sum(test(i))",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z"
}, {
3e5: [0, 136, 3185, 21879, 54600, 115200, 209550, 345150, 529500, 770100, 1074450, 0, 0],
36e5: [3321, 3120429],
864e5: [3321, 3120429],
6048e5: [3123750],
2592e6: [3123750]
}
),
"unary expression with compound data accessor": metricTest({
expression: "sum(test(i / 100))",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z"
}, {
3e5: [0, 1.36, 31.85, 218.79, 546, 1152, 2095.5, 3451.5, 5295, 7701, 10744.5, 0, 0],
36e5: [33.21, 31204.29],
864e5: [33.21, 31204.29],
6048e5: [31237.5],
2592e6: [31237.5]
}
),
"compound expression": metricTest({
expression: "max(test(i)) - min(test(i))",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z",
}, {
3e5: [NaN, 16, 64, 142, 174, 224, 274, 324, 374, 424, 474, NaN, NaN],
36e5: [81, 2417],
864e5: [81, 2417],
6048e5: [2499],
2592e6: [2499]
}
),
"non-pyramidal expression": metricTest({
expression: "distinct(test(i))",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z",
}, {
3e5: [0, 17, 65, 143, 175, 225, 275, 325, 375, 425, 475, 0, 0],
36e5: [82, 2418],
864e5: [82, 2418],
6048e5: [2500],
2592e6: [2500]
}
),
"compound pyramidal and non-pyramidal expression": metricTest({
expression: "sum(test(i)) - median(test(i))",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z",
}, {
3e5: [NaN, 128, 3136, 21726, 54288, 114688, 208788, 344088, 528088, 768288, 1072188, NaN, NaN],
36e5: [3280.5, 3119138.5],
864e5: [3280.5, 3119138.5],
6048e5: [3122500.5],
2592e6: [3122500.5]
}
),
"compound with constant expression": metricTest({
expression: "-1 + sum(test)",
start: "2011-07-17T23:47:00.000Z",
stop: "2011-07-18T00:50:00.000Z",
}, {
3e5: [-1, 16, 64, 142, 174, 224, 274, 324, 374, 424, 474, -1, -1],
36e5: [81, 2417],
864e5: [81, 2417],
6048e5: [2499],
2592e6: [2499]
}
)
}));
suite.export(module);
function metricTest(request, expected) {
var t = {}, k;
for (k in expected) t["at " + steps[k].description + " intervals"] = testStep(k, expected[k]);
return t;
function testStep(step, expected) {
var t = testStepDepth(0, step, expected);
t["(cached)"] = testStepDepth(1, step, expected);
return t;
}
function testStepDepth(depth, step, expected) {
var start = new Date(request.start),
stop = new Date(request.stop);
var test = {
topic: function() {
var actual = [],
total = expected.length,
timeout = setTimeout(function() { cb("Time's up!"); }, 10000),
cb = this.callback,
req = Object.create(request);
req.step = step;
arguments[depth](req, function(response) {
if (actual.push(response) >= total) {
clearTimeout(timeout);
cb(null, actual.sort(function(a, b) { return a.time - b.time; }));
}
});
}
};
test[request.expression] = function(actual) {
// rounds down the start time (inclusive)
var floor = steps[step](start, 0);
assert.deepEqual(actual[0].time, floor);
// rounds up the stop time (exclusive)
var ceil = steps[step](stop, 0);
if (!(ceil - stop)) ceil = steps[step](stop, -1);
assert.deepEqual(actual[actual.length - 1].time, ceil);
// formats UTC time in ISO 8601
actual.forEach(function(d) {
assert.instanceOf(d.time, Date);
assert.match(JSON.stringify(d.time), /[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:00.000Z/);
});
// returns exactly one value per time
var i = 0, n = actual.length, t = actual[0].time;
while (++i < n) assert.isTrue(t < (t = actual[i].time));
// each metric defines only time and value properties
actual.forEach(function(d) {
assert.deepEqual(Object.keys(d), ["time", "value"]);
});
// returns the expected times
var floor = steps[step],
time = floor(start, 0),
times = [];
while (time < stop) {
times.push(time);
time = floor(time, 1);
}
assert.deepEqual(actual.map(function(d) { return d.time; }), times);
// returns the expected values
expected.forEach(function(value, i) {
if (Math.abs(actual[i].value - value) > 1e-6) {
assert.fail(actual.map(function(d) { return d.value; }), expected, "expected {expected}, got {actual} at " + actual[i].time.toISOString());
}
});
};
return test;
}
}