[pathkit] Clean up perf/test init

This will hopefully help with flakiness.

Bug: skia:8810
Change-Id: Id2fa9abcc0e95f0cf8b08557215766b4f9c57478
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/200047
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
diff --git a/modules/pathkit/perf/perfReporter.js b/modules/pathkit/perf/perfReporter.js
index d74110c..c60cfbc 100644
--- a/modules/pathkit/perf/perfReporter.js
+++ b/modules/pathkit/perf/perfReporter.js
@@ -3,47 +3,52 @@
 // Typically used for debugging.
 const fail_on_no_perf = false;
 
-
 function benchmarkAndReport(benchName, setupFn, testFn, teardownFn) {
-    let ctx = {};
-    // warmup 3 times (arbitrary choice)
-    setupFn(ctx);
-    testFn(ctx);
-    testFn(ctx);
-    testFn(ctx);
-    teardownFn(ctx);
-
-    ctx = {};
-    setupFn(ctx);
-    let start = Date.now();
-    let now = start;
-    times = 0;
-    // See how many times we can do it in 100ms (arbitrary choice)
-    while (now - start < 100) {
+    try {
+        let ctx = {};
+        // warmup 3 times (arbitrary choice)
+        setupFn(ctx);
         testFn(ctx);
-        now = Date.now();
-        times++;
-    }
-
-    teardownFn(ctx);
-
-    // Try to make it go for 2 seconds (arbitrarily chosen)
-    // Since the pre-try took 100ms, multiply by 20 to get
-    // approximate tries in 2s
-    let goalTimes = times * 20;
-    setupFn(ctx);
-    start = Date.now();
-    times = 0;
-    while (times < goalTimes) {
         testFn(ctx);
-        times++;
-    }
-    let end = Date.now();
-    teardownFn(ctx);
+        testFn(ctx);
+        teardownFn(ctx);
 
-    let us = (end - start) * 1000 / times;
-    console.log(benchName, `${us} microseconds`)
-    return _report(us, benchName);
+        ctx = {};
+        setupFn(ctx);
+        let start = Date.now();
+        let now = start;
+        times = 0;
+        // See how many times we can do it in 100ms (arbitrary choice)
+        while (now - start < 100) {
+            testFn(ctx);
+            now = Date.now();
+            times++;
+        }
+
+        teardownFn(ctx);
+
+        // Try to make it go for 2 seconds (arbitrarily chosen)
+        // Since the pre-try took 100ms, multiply by 20 to get
+        // approximate tries in 2s (unless now - start >> 100 ms)
+        let goalTimes = times * 20;
+        ctx = {};
+        setupFn(ctx);
+        times = 0;
+        start = Date.now();
+        while (times < goalTimes) {
+            testFn(ctx);
+            times++;
+        }
+        const end = Date.now();
+        teardownFn(ctx);
+
+        const us = (end - start) * 1000 / times;
+        console.log(benchName, `${us} microseconds`)
+        return _report(us, benchName);
+    } catch(e) {
+        console.error('caught error', e);
+        return Promise.reject(e);
+    }
 }