0af10877e00ecdb9529353d7622df2723ae7566b
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / measurement-set-tests.js
1 'use strict';
2
3 var assert = require('assert');
4
5 require('./resources/mock-remote-api.js');
6 require('./resources/v3-models.js');
7
8 describe('MeasurementSet', function () {
9     beforeEach(function () {
10         MeasurementSet._set = null;
11     });
12
13     describe('findSet', function () {
14         it('should create a new MeasurementSet for a new pair of platform and matric', function () {
15             assert.notEqual(MeasurementSet.findSet(1, 1, 3000), MeasurementSet.findSet(1, 2, 3000));
16             assert.notEqual(MeasurementSet.findSet(1, 1, 3000), MeasurementSet.findSet(2, 1, 3000));
17         });
18
19         it('should not create a new MeasurementSet when the same pair of platform and matric are requested', function () {
20             assert.equal(MeasurementSet.findSet(1, 1), MeasurementSet.findSet(1, 1));
21         });
22     });
23
24     describe('fetchBetween', function () {
25         it('should always request the cached primary cluster first', function () {
26             var set = MeasurementSet.findSet(1, 1, 3000);
27             set.fetchBetween(1000, 2000, function () { assert.notReached(); });
28             assert.equal(requests.length, 1);
29             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
30         });
31
32         it('should not request the cached primary cluster twice', function () {
33             var set = MeasurementSet.findSet(1, 1, 3000);
34             set.fetchBetween(1000, 2000, function () { assert.notReached(); });
35             assert.equal(requests.length, 1);
36             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
37             set.fetchBetween(2000, 3000, function () { assert.notReached(); });
38             assert.equal(requests.length, 1);
39         });
40
41         it('should invoke the callback when the up-to-date cached primary cluster is fetched and it matches the requested range', function (done) {
42             var set = MeasurementSet.findSet(1, 1, 3000);
43             var callCount = 0;
44             set.fetchBetween(2000, 3000, function () {
45                 callCount++;
46             });
47             assert.equal(requests.length, 1);
48             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
49
50             requests[0].resolve({
51                 'clusterStart': 1000,
52                 'clusterSize': 1000,
53                 'formatMap': [],
54                 'configurations': {current: []},
55                 'startTime': 2000,
56                 'endTime': 3000,
57                 'lastModified': 3000,
58                 'clusterCount': 2,
59                 'status': 'OK'});
60
61             requests[0].promise.then(function () {
62                 assert.equal(callCount, 1);
63                 assert.equal(requests.length, 1);
64                 done();
65             }).catch(function (error) {
66                 done(error);
67             });
68         });
69
70         it('should invoke the callback and fetch a secondary cluster'
71             + 'when the cached primary cluster is up-to-date and within in the requested range', function (done) {
72             var set = MeasurementSet.findSet(1, 1, 3000);
73             var callCount = 0;
74             set.fetchBetween(1000, 3000, function () {
75                 callCount++;
76             });
77             assert.equal(requests.length, 1);
78             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
79
80             requests[0].resolve({
81                 'clusterStart': 1000,
82                 'clusterSize': 1000,
83                 'formatMap': [],
84                 'configurations': {current: []},
85                 'startTime': 2000,
86                 'endTime': 3000,
87                 'lastModified': 3000,
88                 'clusterCount': 2,
89                 'status': 'OK'});
90
91             Promise.resolve().then(function () {
92                 assert.equal(callCount, 1);
93                 assert.equal(requests.length, 2);
94                 assert.equal(requests[1].url, '../data/measurement-set-1-1-2000.json');
95                 done();
96             }).catch(function (error) {
97                 done(error);
98             });
99         });
100
101         it('should request additional secondary clusters as requested', function (done) {
102             var set = MeasurementSet.findSet(1, 1, 5000);
103             set.fetchBetween(2000, 3000, function () {
104                 assert.notReached();
105             });
106             assert.equal(requests.length, 1);
107             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
108
109             requests[0].resolve({
110                 'clusterStart': 1000,
111                 'clusterSize': 1000,
112                 'formatMap': [],
113                 'configurations': {current: []},
114                 'startTime': 4000,
115                 'endTime': 5000,
116                 'lastModified': 5000,
117                 'clusterCount': 3,
118                 'status': 'OK'});
119
120             Promise.resolve().then(function () {
121                 assert.equal(requests.length, 2);
122                 assert.equal(requests[1].url, '../data/measurement-set-1-1-3000.json');
123
124                 var callCount = 0;
125                 set.fetchBetween(0, 7000, function () { callCount++; });
126
127                 assert.equal(callCount, 1);
128                 assert.equal(requests.length, 4);
129                 assert.equal(requests[2].url, '../data/measurement-set-1-1-2000.json');
130                 assert.equal(requests[3].url, '../data/measurement-set-1-1-4000.json');
131
132                 done();
133             }).catch(function (error) {
134                 done(error);
135             });
136         });
137
138         it('should request secondary clusters which forms a superset of the requested range', function (done) {
139             var set = MeasurementSet.findSet(1, 1, 5000);
140             set.fetchBetween(2707, 4207, function () {
141                 assert.notReached();
142             });
143             assert.equal(requests.length, 1);
144             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
145
146             requests[0].resolve({
147                 'clusterStart': 1000,
148                 'clusterSize': 1000,
149                 'formatMap': [],
150                 'configurations': {current: []},
151                 'startTime': 4000,
152                 'endTime': 5000,
153                 'lastModified': 5000,
154                 'clusterCount': 3,
155                 'status': 'OK'});
156
157             Promise.resolve().then(function () {
158                 assert.equal(requests.length, 3);
159                 assert.equal(requests[1].url, '../data/measurement-set-1-1-3000.json');
160                 assert.equal(requests[2].url, '../data/measurement-set-1-1-4000.json');
161                 done();
162             }).catch(function (error) {
163                 done(error);
164             });
165         });
166
167         it('should not request secondary clusters that are not requested', function (done) {
168             var set = MeasurementSet.findSet(1, 1, 5000);
169             set.fetchBetween(3200, 3700, function () {
170                 assert.notReached();
171             });
172             assert.equal(requests.length, 1);
173             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
174
175             requests[0].resolve({
176                 'clusterStart': 1000,
177                 'clusterSize': 1000,
178                 'formatMap': [],
179                 'configurations': {current: []},
180                 'startTime': 4000,
181                 'endTime': 5000,
182                 'lastModified': 5000,
183                 'clusterCount': 3,
184                 'status': 'OK'});
185
186             Promise.resolve().then(function () {
187                 assert.equal(requests.length, 2);
188                 assert.equal(requests[1].url, '../data/measurement-set-1-1-4000.json');
189
190                 var callCount = 0;
191                 set.fetchBetween(1207, 1293, function () { callCount++; });
192
193                 assert.equal(callCount, 0);
194                 assert.equal(requests.length, 3);
195                 assert.equal(requests[2].url, '../data/measurement-set-1-1-2000.json');
196
197                 set.fetchBetween(1964, 3401, function () { callCount++; });
198
199                 assert.equal(callCount, 0);
200                 assert.equal(requests.length, 4);
201                 assert.equal(requests[3].url, '../data/measurement-set-1-1-3000.json');
202
203                 done();
204             }).catch(function (error) {
205                 done(error);
206             });
207         });
208
209         it('should invoke the callback when the fetching of the primray cluster fails', function (done) {
210             var set = MeasurementSet.findSet(1, 1, 3000);
211             var callCount = 0;
212             set.fetchBetween(1000, 3000, function () {
213                 callCount++;
214             });
215             assert.equal(requests.length, 1);
216             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
217
218             requests[0].reject(500);
219
220             Promise.resolve().then(function () {
221                 assert.equal(callCount, 1);
222                 assert.equal(requests.length, 1);
223                 done();
224             }).catch(function (error) {
225                 done(error);
226             });
227         });
228
229         it('should request the uncached primary cluster when the cached cluster is outdated', function (done) {
230             var set = MeasurementSet.findSet(1, 1, 3005);
231             var callCount = 0;
232             set.fetchBetween(1000, 2000, function () {
233                 callCount++;
234             });
235             assert.equal(requests.length, 1);
236             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
237
238             requests[0].resolve({
239                 'clusterStart': 1000,
240                 'clusterSize': 1000,
241                 'formatMap': [],
242                 'configurations': {current: []},
243                 'startTime': 2000,
244                 'endTime': 3000,
245                 'lastModified': 3000,
246                 'clusterCount': 2,
247                 'status': 'OK'});
248
249             Promise.resolve().then(function () {
250                 assert.equal(callCount, 0);
251                 assert.equal(requests.length, 2);
252                 assert.equal(requests[1].url, '../api/measurement-set?platform=1&metric=1');
253                 done();
254             }).catch(function (error) {
255                 done(error);
256             });
257         });
258
259         it('should request the uncached primary cluster when the cached cluster is 404', function (done) {
260             var set = MeasurementSet.findSet(1, 1, 3005);
261             var callCount = 0;
262             set.fetchBetween(1000, 2000, function () {
263                 callCount++;
264             });
265             assert.equal(requests.length, 1);
266             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
267
268             requests[0].reject(404);
269
270             Promise.resolve().then(function () {
271                 assert.equal(callCount, 0);
272                 assert.equal(requests.length, 2);
273                 assert.equal(requests[1].url, '../api/measurement-set?platform=1&metric=1');
274                 done();
275             }).catch(function (error) {
276                 done(error);
277             });
278         });
279
280         it('should not request the primary cluster twice when multiple clients request it but should invoke all callbacks', function (done) {
281             var set = MeasurementSet.findSet(1, 1, 3000);
282             var callCount = 0;
283             set.fetchBetween(2000, 3000, function () {
284                 callCount++;
285             });
286             assert.equal(requests.length, 1);
287             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
288
289             var alternativeCallCount = 0;
290             set.fetchBetween(2000, 3000, function () {
291                 alternativeCallCount++;
292             });
293
294             requests[0].resolve({
295                 'clusterStart': 1000,
296                 'clusterSize': 1000,
297                 'formatMap': [],
298                 'configurations': {current: []},
299                 'startTime': 2000,
300                 'endTime': 3000,
301                 'lastModified': 3000,
302                 'clusterCount': 2,
303                 'status': 'OK'});
304
305             Promise.resolve().then(function () {
306                 assert.equal(callCount, 1);
307                 assert.equal(alternativeCallCount, 1);
308                 assert.equal(requests.length, 1);
309                 done();
310             }).catch(function (error) {
311                 done(error);
312             });
313         });
314
315         it('should invoke callback for each secondary clusters that are fetched or rejected', function (done) {
316             var set = MeasurementSet.findSet(1, 1, 5000);
317             var callCountFor4000 = 0;
318             set.fetchBetween(3200, 3700, function () { callCountFor4000++; });
319             assert.equal(requests.length, 1);
320             assert.equal(requests[0].url, '../data/measurement-set-1-1.json');
321
322             requests[0].resolve({
323                 'clusterStart': 1000,
324                 'clusterSize': 1000,
325                 'formatMap': [],
326                 'configurations': {current: []},
327                 'startTime': 4000,
328                 'endTime': 5000,
329                 'lastModified': 5000,
330                 'clusterCount': 3,
331                 'status': 'OK'});
332
333             var callCountFor4000To5000 = 0;
334             var callCountFor2000 = 0;
335             var callCountFor2000To4000 = 0;
336             Promise.resolve().then(function () {
337                 assert.equal(callCountFor4000, 0);
338                 assert.equal(requests.length, 2);
339                 assert.equal(requests[1].url, '../data/measurement-set-1-1-4000.json');
340
341                 set.fetchBetween(3708, 4800, function () { callCountFor4000To5000++; });
342                 assert.equal(callCountFor4000To5000, 1);
343                 assert.equal(requests.length, 2);
344
345                 set.fetchBetween(1207, 1293, function () { callCountFor2000++; });
346                 assert.equal(callCountFor2000, 0);
347                 assert.equal(requests.length, 3);
348                 assert.equal(requests[2].url, '../data/measurement-set-1-1-2000.json');
349
350                 requests[2].resolve({
351                     'formatMap': [],
352                     'configurations': {current: []},
353                     'startTime': 1000,
354                     'endTime': 2000,
355                     'lastModified': 5000,
356                     'status': 'OK'});
357             }).then(function () {
358                 assert.equal(requests.length, 3);
359                 assert.equal(callCountFor4000, 0);
360                 assert.equal(callCountFor4000To5000, 1);
361                 assert.equal(callCountFor2000, 1);
362
363                 set.fetchBetween(1964, 3401, function () { callCountFor2000To4000++; });
364
365                 assert.equal(callCountFor2000To4000, 1);
366                 assert.equal(requests.length, 4);
367                 assert.equal(requests[3].url, '../data/measurement-set-1-1-3000.json');
368
369                 requests[3].resolve({
370                     'formatMap': [],
371                     'configurations': {current: []},
372                     'startTime': 2000,
373                     'endTime': 3000,
374                     'lastModified': 5000,
375                     'status': 'OK'});
376             }).then(function () {
377                 assert.equal(callCountFor4000, 0);
378                 assert.equal(callCountFor4000To5000, 1);
379                 assert.equal(callCountFor2000, 1);
380                 assert.equal(callCountFor2000To4000, 2);
381                 assert.equal(requests.length, 4);
382
383                 requests[1].resolve({
384                     'formatMap': [],
385                     'configurations': {current: []},
386                     'startTime': 3000,
387                     'endTime': 4000,
388                     'lastModified': 5000,
389                     'status': 'OK'});
390             }).then(function () {
391                 assert.equal(callCountFor4000, 1);
392                 assert.equal(callCountFor4000To5000, 2);
393                 assert.equal(callCountFor2000, 1);
394                 assert.equal(callCountFor2000To4000, 3);
395                 assert.equal(requests.length, 4);
396
397                 done();
398             }).catch(function (error) {
399                 done(error);
400             })
401         });
402
403     });
404
405 });