1const 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 6 7function benchmarkAndReport(benchName, setupFn, testFn, teardownFn) { 8 let ctx = {}; 9 // warmup 3 times (arbitrary choice) 10 setupFn(ctx); 11 testFn(ctx); 12 testFn(ctx); 13 testFn(ctx); 14 teardownFn(ctx); 15 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 33 let goalTimes = times * 20; 34 setupFn(ctx); 35 start = Date.now(); 36 times = 0; 37 while (times < goalTimes) { 38 testFn(ctx); 39 times++; 40 } 41 let end = Date.now(); 42 teardownFn(ctx); 43 44 let us = (end - start) * 1000 / times; 45 console.log(benchName, `${us} microseconds`) 46 return _report(us, benchName); 47} 48 49 50function _report(microseconds, benchName) { 51 return fetch(REPORT_URL, { 52 method: 'POST', 53 mode: 'no-cors', 54 headers: { 55 'Content-Type': 'application/json', 56 }, 57 body: JSON.stringify({ 58 'bench_name': benchName, 59 'time_us': microseconds, 60 }) 61 }).then(() => console.log(`Successfully reported ${benchName} to perf aggregator`)); 62} 63 64function reportError(done) { 65 return (e) => { 66 console.log("Error with fetching. Likely could not connect to aggegator server", e.message); 67 if (fail_on_no_perf) { 68 expect(e).toBeUndefined(); 69 } 70 done(); 71 }; 72}