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