tor-browser

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

test_HeapSnapshot_takeCensus_08.js (2644B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // HeapSnapshot.prototype.takeCensus: test by: 'count' breakdown
      6 //
      7 // Ported from js/src/jit-test/tests/debug/Memory-takeCensus-08.js
      8 
      9 function run_test() {
     10  const g = newGlobal();
     11  const dbg = new Debugger(g);
     12 
     13  g.eval(`
     14         var stuff = [];
     15         function add(n, c) {
     16           for (let i = 0; i < n; i++)
     17             stuff.push(c());
     18         }
     19 
     20         let count = 0;
     21 
     22         function obj() { return { count: count++ }; }
     23         obj.factor = 1;
     24 
     25         // This creates a closure (a function JSObject) that has captured
     26         // a Call object. So each call creates two items.
     27         function fun() { let v = count; return () => { return v; } }
     28         fun.factor = 2;
     29 
     30         function str() { return 'perambulator' + count++; }
     31         str.factor = 1;
     32 
     33         // Eval a fresh text each time, allocating:
     34         // - a fresh ScriptSourceObject
     35         // - a new JSScripts, not an eval cache hits
     36         // - a fresh prototype object
     37         // - a fresh Call object, since the eval makes 'ev' heavyweight
     38         // - the new function itself
     39         function ev()  {
     40           return eval(\`(function () { return \${ count++ } })\`);
     41         }
     42         ev.factor = 5;
     43 
     44         // A new object (1) with a new shape (2) with a new atom (3)
     45         function shape() { return { [ 'theobroma' + count++ ]: count }; }
     46         shape.factor = 3;
     47         `);
     48 
     49  let baseline = 0;
     50  function countIncreasedByAtLeast(n) {
     51    const oldBaseline = baseline;
     52 
     53    // Since a census counts only reachable objects, one might assume that calling
     54    // GC here would have no effect on the census results. But GC also throws away
     55    // JIT code and any objects it might be holding (template objects, say);
     56    // takeCensus reaches those. Shake everything loose that we can, to make the
     57    // census approximate reachability a bit more closely, and make our results a
     58    // bit more predictable.
     59    gc(g, "shrinking");
     60 
     61    baseline = saveHeapSnapshotAndTakeCensus(dbg, {
     62      breakdown: { by: "count" },
     63    }).count;
     64    return baseline >= oldBaseline + n;
     65  }
     66 
     67  countIncreasedByAtLeast(0);
     68 
     69  g.add(100, g.obj);
     70  ok(countIncreasedByAtLeast(g.obj.factor * 100));
     71 
     72  g.add(100, g.fun);
     73  ok(countIncreasedByAtLeast(g.fun.factor * 100));
     74 
     75  g.add(100, g.str);
     76  ok(countIncreasedByAtLeast(g.str.factor * 100));
     77 
     78  g.add(100, g.ev);
     79  ok(countIncreasedByAtLeast(g.ev.factor * 100));
     80 
     81  g.add(100, g.shape);
     82  ok(countIncreasedByAtLeast(g.shape.factor * 100));
     83 
     84  do_test_finished();
     85 }