tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_delazification_strategy.html (7482B)


      1 <!DOCTYPE html>
      2 <html>
      3 <!-- https://bugzilla.mozilla.org/show_bug.cgi?id=1753709 -->
      4 <!-- Script delazification strategy is not supposed to have any observable
      5     side-effect. To make it observable, the ScriptLoader is instrumented to
      6     trigger notifications on the script tag. These notifications are used to
      7     validate that the strategy is used as execpected. This does not garantee
      8     that all functions are delazified properly, but this should be checked in
      9     the JS engine test suite.
     10 -->
     11 <head>
     12  <meta charset="utf-8">
     13  <title>Test for triggering eager delazification.</title>
     14  <script src="/resources/testharness.js"></script>
     15  <script src="/resources/testharnessreport.js"></script>
     16  <script type="application/javascript">
     17    async function WaitForScriptNotification() {
     18      var url = "file_delazification_strategy.html";
     19      var iframe = document.createElement("iframe");
     20 
     21      // Call the resolve function when the notification is one of the expected
     22      // notifications. This is made to be used by a promise and provided to
     23      // notification observer.
     24      function resolve_with_notification(resolve, subject, topic, data) {
     25        const param = {};
     26        for (const line of data.split("\n")) {
     27          const m = line.match(/^([^:]+):(.+)/);
     28          param[m[1]] = m[2];
     29        }
     30 
     31        // If we have multiple script tags in the loaded source, make sure
     32        // we only watch a single one.
     33        if (param.id != "watchme")
     34          return;
     35 
     36        switch (param.event) {
     37          case "delazification:OnDemandOnly":
     38          case "delazification:ConcurrentDepthFirst":
     39          case "delazification:ParseEverythingEagerly":
     40            resolve(param.event.replace("delazification:", ""));
     41            break;
     42          case "compile:main thread":
     43            resolve(param.event);
     44            break;
     45        }
     46      }
     47 
     48      // Create an notification observer, which resolves a promise.
     49      let log_notification;
     50      let scriptLoaderTrace = new Promise((resolve, reject) => {
     51        log_notification = resolve_with_notification.bind(this, resolve);
     52      });
     53 
     54      // Wait until the iframe is fully loaded.
     55      await new Promise(resolve => {
     56        iframe.onload = resolve;
     57        iframe.src = url;
     58        document.body.appendChild(iframe);
     59      });
     60 
     61      // Register all notifications.
     62      SpecialPowers.Services.obs.addObserver(log_notification, "ScriptLoaderTest");
     63 
     64      // Add a script tag, which will trigger one of the previous
     65      // notifications..
     66      let script = document.createElement("script");
     67      script.setAttribute("id", "watchme");
     68      script.setAttribute("src", "file_delazification_strategy.js");
     69      iframe.contentDocument.body.appendChild(script);
     70 
     71      // Wait for the notification emitted by ScriptLoader, while processing the
     72      // previous script.
     73      let result = await scriptLoaderTrace;
     74 
     75      SpecialPowers.Services.obs.removeObserver(log_notification, "ScriptLoaderTest");
     76      document.body.removeChild(iframe);
     77      return result;
     78    }
     79 
     80    // Setting dom.expose_test_interfaces pref causes the
     81    // nsScriptLoadRequest to fire notifications on script tags, with
     82    // information about its internal state. The ScriptLoader source send
     83    // notifications to trace these and resolve a promise with the path taken
     84    // by the script loader.
     85    //
     86    // Setting dom.script_loader.bytecode_cache.enabled to false in order
     87    // to prevent the bytecode cache to perturb this test case.
     88    //
     89    // Setting dom.script_loader.external_scripts.speculate_* are used to
     90    // force off-main-thread compilation, while hoping that we have enough
     91    // processors to run the test case
     92    //
     93    // Setting dom.delazification.* are used to select the delazification
     94    // strategy and to check that it is well selected.
     95    promise_test(async function() {
     96      await SpecialPowers.pushPrefEnv({set: [
     97        ['dom.expose_test_interfaces', true],
     98        ['dom.script_loader.bytecode_cache.enabled', false],
     99        ['dom.script_loader.external_scripts.speculate_non_parser_inserted.enable', true],
    100        ['dom.script_loader.external_scripts.speculate_async.enabled', true],
    101        ['dom.script_loader.external_scripts.speculate_link_preload.enabled', true],
    102        // Parse everything eagerly
    103        ['dom.script_loader.delazification.strategy', 255],
    104        ['dom.script_loader.delazification.max_size', 0],
    105        ['dom.script_loader.delazification.min_mem', 0],
    106      ]});
    107 
    108      assert_equals(await WaitForScriptNotification(), "OnDemandOnly",
    109                    "[1] AttemptAsyncScriptCompile: On demand only");
    110    }, "Check that max_size can disable delazification strategy");
    111 
    112    promise_test(async function() {
    113      await SpecialPowers.pushPrefEnv({set: [
    114        ['dom.expose_test_interfaces', true],
    115        ['dom.script_loader.bytecode_cache.enabled', false],
    116        // Enable OffMainThread compilation for everything, and cross-fingers
    117        // about the number of CPU.
    118        ['dom.script_loader.external_scripts.speculate_non_parser_inserted.enable', true],
    119        ['dom.script_loader.external_scripts.speculate_async.enabled', true],
    120        ['dom.script_loader.external_scripts.speculate_link_preload.enabled', true],
    121        // Parse everything eagerly
    122        ['dom.script_loader.delazification.strategy', 255],
    123        ['dom.script_loader.delazification.max_size', 10485760],
    124        // 4 TB should of RAM be enough.
    125        ['dom.script_loader.delazification.min_mem', 4096],
    126      ]});
    127 
    128      assert_equals(await WaitForScriptNotification(), "OnDemandOnly",
    129                    "[2] AttemptAsyncScriptCompile: On demand only");
    130    }, "Check that min_mem can disable delazification strategy");
    131 
    132    promise_test(async function() {
    133      await SpecialPowers.pushPrefEnv({set: [
    134        ['dom.expose_test_interfaces', true],
    135        ['dom.script_loader.bytecode_cache.enabled', false],
    136        // Enable OffMainThread compilation for everything, and cross-fingers
    137        // about the number of CPU.
    138        ['dom.script_loader.external_scripts.speculate_non_parser_inserted.enable', true],
    139        ['dom.script_loader.external_scripts.speculate_async.enabled', true],
    140        ['dom.script_loader.external_scripts.speculate_link_preload.enabled', true],
    141        ['dom.script_loader.delazification.max_size', 10485760],
    142        ['dom.script_loader.delazification.min_mem', 0],
    143      ]});
    144 
    145      await SpecialPowers.pushPrefEnv({set: [
    146        ['dom.script_loader.delazification.strategy', 0],
    147      ]});
    148      assert_equals(await WaitForScriptNotification(), "OnDemandOnly",
    149                    "[3] AttemptAsyncScriptCompile: On demand only");
    150 
    151      await SpecialPowers.pushPrefEnv({set: [
    152        ['dom.script_loader.delazification.strategy', 2],
    153      ]});
    154      assert_equals(await WaitForScriptNotification(), "ConcurrentDepthFirst",
    155                    "[3] AttemptAsyncScriptCompile: Concurrent Depth First");
    156 
    157      await SpecialPowers.pushPrefEnv({set: [
    158        ['dom.script_loader.delazification.strategy', 255],
    159      ]});
    160      assert_equals(await WaitForScriptNotification(), "ParseEverythingEagerly",
    161                    "[3] AttemptAsyncScriptCompile: Parse Everything Eagerly");
    162    }, "Check enabling delazification strategy works");
    163 
    164    done();
    165  </script>
    166 </head>
    167 <body>
    168  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1753709">Mozilla Bug 1753709</a>
    169 </body>
    170 </html>