blob: c60cfbc7f9d8dc3a93eb671192bd2cababa5bdcf [file] [log] [blame]
Kevin Lubick556350d2018-10-12 15:21:17 -04001const REPORT_URL = 'http://localhost:8081/report_perf_data'
2// Set this to enforce that the perf server must be up.
3// Typically used for debugging.
4const fail_on_no_perf = false;
5
Kevin Lubick556350d2018-10-12 15:21:17 -04006function benchmarkAndReport(benchName, setupFn, testFn, teardownFn) {
Kevin Lubickd2544352019-03-12 09:20:32 -04007 try {
8 let ctx = {};
9 // warmup 3 times (arbitrary choice)
10 setupFn(ctx);
Kevin Lubick556350d2018-10-12 15:21:17 -040011 testFn(ctx);
Kevin Lubick556350d2018-10-12 15:21:17 -040012 testFn(ctx);
Kevin Lubickd2544352019-03-12 09:20:32 -040013 testFn(ctx);
14 teardownFn(ctx);
Kevin Lubick556350d2018-10-12 15:21:17 -040015
Kevin Lubickd2544352019-03-12 09:20:32 -040016 ctx = {};
17 setupFn(ctx);
18 let start = Date.now();
19 let now = start;
20 times = 0;
21 // See how many times we can do it in 100ms (arbitrary choice)
22 while (now - start < 100) {
23 testFn(ctx);
24 now = Date.now();
25 times++;
26 }
27
28 teardownFn(ctx);
29
30 // Try to make it go for 2 seconds (arbitrarily chosen)
31 // Since the pre-try took 100ms, multiply by 20 to get
32 // approximate tries in 2s (unless now - start >> 100 ms)
33 let goalTimes = times * 20;
34 ctx = {};
35 setupFn(ctx);
36 times = 0;
37 start = Date.now();
38 while (times < goalTimes) {
39 testFn(ctx);
40 times++;
41 }
42 const end = Date.now();
43 teardownFn(ctx);
44
45 const us = (end - start) * 1000 / times;
46 console.log(benchName, `${us} microseconds`)
47 return _report(us, benchName);
48 } catch(e) {
49 console.error('caught error', e);
50 return Promise.reject(e);
51 }
Kevin Lubick556350d2018-10-12 15:21:17 -040052}
53
54
55function _report(microseconds, benchName) {
56 return fetch(REPORT_URL, {
57 method: 'POST',
58 mode: 'no-cors',
59 headers: {
60 'Content-Type': 'application/json',
61 },
62 body: JSON.stringify({
63 'bench_name': benchName,
64 'time_us': microseconds,
65 })
66 }).then(() => console.log(`Successfully reported ${benchName} to perf aggregator`));
67}
68
69function reportError(done) {
70 return (e) => {
71 console.log("Error with fetching. Likely could not connect to aggegator server", e.message);
72 if (fail_on_no_perf) {
73 expect(e).toBeUndefined();
74 }
75 done();
76 };
77}