perfReporter.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. function benchmarkAndReport(benchName, setupFn, testFn, teardownFn) {
  6. try {
  7. let ctx = {};
  8. // warmup 3 times (arbitrary choice)
  9. setupFn(ctx);
  10. testFn(ctx);
  11. testFn(ctx);
  12. testFn(ctx);
  13. teardownFn(ctx);
  14. ctx = {};
  15. setupFn(ctx);
  16. let start = Date.now();
  17. let now = start;
  18. times = 0;
  19. // See how many times we can do it in 100ms (arbitrary choice)
  20. while (now - start < 100) {
  21. testFn(ctx);
  22. now = Date.now();
  23. times++;
  24. }
  25. teardownFn(ctx);
  26. // Try to make it go for 2 seconds (arbitrarily chosen)
  27. // Since the pre-try took 100ms, multiply by 20 to get
  28. // approximate tries in 2s (unless now - start >> 100 ms)
  29. let goalTimes = times * 20;
  30. ctx = {};
  31. setupFn(ctx);
  32. times = 0;
  33. start = Date.now();
  34. while (times < goalTimes) {
  35. testFn(ctx);
  36. times++;
  37. }
  38. const end = Date.now();
  39. teardownFn(ctx);
  40. const us = (end - start) * 1000 / times;
  41. console.log(benchName, `${us} microseconds`)
  42. return _report(us, benchName);
  43. } catch(e) {
  44. console.error('caught error', e);
  45. return Promise.reject(e);
  46. }
  47. }
  48. function _report(microseconds, benchName) {
  49. return fetch(REPORT_URL, {
  50. method: 'POST',
  51. mode: 'no-cors',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. },
  55. body: JSON.stringify({
  56. 'bench_name': benchName,
  57. 'time_us': microseconds,
  58. })
  59. }).then(() => console.log(`Successfully reported ${benchName} to perf aggregator`));
  60. }
  61. function reportError(done) {
  62. return (e) => {
  63. console.log("Error with fetching. Likely could not connect to aggegator server", e.message);
  64. if (fail_on_no_perf) {
  65. expect(e).toBeUndefined();
  66. }
  67. done();
  68. };
  69. }