tor-browser

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

test_memory_allocations_05.html (2736B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1068144 - Test getting the timestamps for allocations.
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Memory monitoring actor test</title>
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
     11 </head>
     12 <body>
     13 <pre id="test">
     14 <script src="memory-helpers.js" type="application/javascript"></script>
     15 <script>
     16 "use strict";
     17 
     18 window.onload = function() {
     19  SimpleTest.waitForExplicitFinish();
     20 
     21  (async function() {
     22    const { memory, target } = await startServerAndGetSelectedTabMemory();
     23    await memory.attach();
     24 
     25    const allocs = [];
     26    function allocator() {
     27      allocs.push({});
     28    }
     29 
     30    // Using setTimeout results in wildly varying delays that make it hard to
     31    // test our timestamps and results in intermittent failures. Instead, we
     32    // actually spin an empty loop for a whole millisecond.
     33    function actuallyWaitOneWholeMillisecond() {
     34      const start = window.performance.now();
     35      // eslint-disable-next-line curly
     36      while (window.performance.now() - start < 1.000);
     37    }
     38 
     39    await memory.startRecordingAllocations();
     40 
     41    allocator();
     42    actuallyWaitOneWholeMillisecond();
     43    allocator();
     44    actuallyWaitOneWholeMillisecond();
     45    allocator();
     46 
     47    const response = await memory.getAllocations();
     48    await memory.stopRecordingAllocations();
     49 
     50    ok(response.allocationsTimestamps, "The response should have timestamps.");
     51    is(response.allocationsTimestamps.length, response.allocations.length,
     52       "There should be a timestamp for every allocation.");
     53 
     54    const allocatorIndices = response.allocations
     55      .map(function(a, idx) {
     56        const frame = response.frames[a];
     57        if (frame && frame.functionDisplayName === "allocator") {
     58          return idx;
     59        }
     60        return null;
     61      })
     62      .filter(function(idx) {
     63        return idx !== null;
     64      });
     65 
     66    is(allocatorIndices.length, 3,
     67       "Should have our 3 allocations from the `allocator` timeouts.");
     68 
     69    let lastTimestamp;
     70    for (let i = 0; i < 3; i++) {
     71      const timestamp = response.allocationsTimestamps[allocatorIndices[i]];
     72      info("timestamp", timestamp);
     73      ok(timestamp, "We should have a timestamp for the `allocator` allocation.");
     74 
     75      if (lastTimestamp) {
     76        const delta = timestamp - lastTimestamp;
     77        info("delta since last timestamp", delta);
     78        // ms
     79        ok(delta >= 1,
     80           "The timestamp should be about 1 ms after the last timestamp.");
     81      }
     82 
     83      lastTimestamp = timestamp;
     84    }
     85 
     86    await memory.detach();
     87    destroyServerAndFinish(target);
     88  })();
     89 };
     90 </script>
     91 </pre>
     92 </body>
     93 </html>