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
--- 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>