Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 1 | const 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. |
| 4 | const fail_on_no_perf = false; |
| 5 | |
Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 6 | function benchmarkAndReport(benchName, setupFn, testFn, teardownFn) { |
Kevin Lubick | d254435 | 2019-03-12 09:20:32 -0400 | [diff] [blame] | 7 | try { |
| 8 | let ctx = {}; |
| 9 | // warmup 3 times (arbitrary choice) |
| 10 | setupFn(ctx); |
Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 11 | testFn(ctx); |
Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 12 | testFn(ctx); |
Kevin Lubick | d254435 | 2019-03-12 09:20:32 -0400 | [diff] [blame] | 13 | testFn(ctx); |
| 14 | teardownFn(ctx); |
Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 15 | |
Kevin Lubick | d254435 | 2019-03-12 09:20:32 -0400 | [diff] [blame] | 16 | 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 Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | function _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 | |
| 69 | function 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 | } |