Bug 1428072 - 2/3 - Reorganize mochitest files for the profiler r=mstange draft
authorJulien Wajsberg <felash@gmail.com>
Wed, 31 Jan 2018 10:51:19 +0100
changeset 752023 a3f41959123bd0fb0655c65d7cdc9a56f5f748b0
parent 752022 5a509fbb25f199ad0dfad9afd44e618f40041467
child 752024 b3c38d7cf8164d6ec1224b54342531984a6a424c
push id98134
push userbmo:felash@gmail.com
push dateWed, 07 Feb 2018 12:23:55 +0000
reviewersmstange
bugs1428072
milestone60.0a1
Bug 1428072 - 2/3 - Reorganize mochitest files for the profiler r=mstange We extract some common framework to run profiler tests. MozReview-Commit-ID: 7FPcH1NpJ7S
tools/profiler/tests/chrome/chrome.ini
tools/profiler/tests/chrome/profiler_test_utils.js
tools/profiler/tests/chrome/test_profile_worker_bug_1428076.html
--- a/tools/profiler/tests/chrome/chrome.ini
+++ b/tools/profiler/tests/chrome/chrome.ini
@@ -1,1 +1,4 @@
+[DEFAULT]
+support-files=profiler_test_utils.js
+
 [test_profile_worker_bug_1428076.html]
new file mode 100644
--- /dev/null
+++ b/tools/profiler/tests/chrome/profiler_test_utils.js
@@ -0,0 +1,62 @@
+"use strict";
+
+(function() {
+
+ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+function startProfiler(settings) {
+  Services.profiler.StartProfiler(
+    settings.entries,
+    settings.interval,
+    settings.features,
+    settings.features.length,
+    settings.threads,
+    settings.threads.length
+  );
+
+  info("Profiler has started");
+}
+
+function getProfile() {
+  const profile = Services.profiler.getProfileData();
+  info("We got a profile, run the mochitest with `--keep-open true` to see the logged profile in the Web Console.");
+
+  // Run the mochitest with `--keep-open true` to see the logged profile in the
+  // Web console.
+  console.log(profile);
+
+  return profile;
+}
+
+function stopProfiler() {
+  Services.profiler.StopProfiler();
+  info("Profiler has stopped");
+}
+
+function end(error) {
+  if (error) {
+    ok(false, `We got an error: ${error}`);
+  } else {
+    ok(true, "We ran the whole process");
+  }
+  SimpleTest.finish();
+}
+
+async function runTest(settings, workload) {
+  SimpleTest.waitForExplicitFinish();
+  try {
+    await startProfiler(settings);
+    await workload();
+    await getProfile();
+    await stopProfiler();
+    await end();
+  } catch (e) {
+    // By catching and handling the error, we're being nice to mochitest
+    // runners: instead of waiting for the timeout, we fail right away.
+    await end(e);
+  }
+}
+
+window.runTest = runTest;
+
+})();
--- a/tools/profiler/tests/chrome/test_profile_worker_bug_1428076.html
+++ b/tools/profiler/tests/chrome/test_profile_worker_bug_1428076.html
@@ -8,66 +8,30 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 1428076</title>
   <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1428076">Mozilla Bug 1428076</a>
 
 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+<script type="application/javascript" src="profiler_test_utils.js"></script>
 <script type="application/javascript">
-
 /** Test for Bug 1428076 **/
-"use strict";
-ChromeUtils.import("resource://gre/modules/Services.jsm");
 
-function startProfiler() {
-  const settings = {
-    entries: 1000000, // 9MB
-    interval: 1, // ms
-    features: ["js", "threads", "leaf", "stackwalk"],
-    threads: ["GeckoMain", "Compositor", "Worker"] // most common combination
-  };
-
-  Services.profiler.StartProfiler(
-    settings.entries,
-    settings.interval,
-    settings.features,
-    settings.features.length,
-    settings.threads,
-    settings.threads.length
-  );
+/* globals runTest */
 
-  info("Profiler has started");
-}
-
-function getProfile() {
-  const profile = Services.profiler.getProfileData();
-  info("We got a profile");
-
-  // Run the mochitest with `--keep-open true` to see the logged profile in the
-  // Web console.
-  console.log(profile);
-
-  return profile;
-}
+"use strict";
 
-function stopProfiler() {
-  Services.profiler.StopProfiler();
-  info("Profiler has stopped");
-}
-
-function end(error) {
-  if (error) {
-    ok(false, `We got an error: ${error}`);
-  } else {
-    ok(true, "We ran the whole process");
-  }
-  SimpleTest.finish();
-}
+const settings = {
+  entries: 1000000, // 9MB
+  interval: 1, // ms
+  features: ["js", "threads", "leaf", "stackwalk"],
+  threads: ["GeckoMain", "Compositor", "Worker"] // most common combination
+};
 
 function workload() {
   // We use a Blob for the worker content to avoid an external JS file, and data
   // URLs seem to be blocked in a chrome environment.
   const workerContent = new Blob(
     [ "console.log('hello world!')" ],
     { type: "application/javascript" }
   );
@@ -82,27 +46,13 @@ function workload() {
 
   // We're deferring some little time so that the worker has the time to be
   // properly cleaned up and the profiler actually saves the worker data.
   return new Promise(resolve => {
     setTimeout(resolve, 50);
   });
 }
 
-SimpleTest.waitForExplicitFinish();
-(async function() {
-  try {
-    await startProfiler();
-    await workload();
-    await getProfile();
-    await stopProfiler();
-    await end();
-  } catch (e) {
-    // By catching and handling the error, we're being nice to mochitest
-    // runners: instead of waiting for the timeout, we fail right away.
-    await end(e);
-  }
-})();
-
+runTest(settings, workload);
 
 </script>
 </body>
 </html>