tor-browser

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

pc-count-profiler.js (1300B)


      1 // Smoke test for the PC count profiler. 
      2 
      3 function f(x) {
      4    if (x < 10)
      5        return 0;
      6    if (x < 50)
      7        return 1;
      8    return 2;
      9 }
     10 
     11 L: try {
     12    // Profile the test function "f".
     13    pccount.start();
     14    for (var i = 0; i < 100; ++i) f(i);
     15    pccount.stop();
     16 
     17    // Count the profiled scripts, stop if no scripts were profiled.
     18    var n = pccount.count();
     19    if (n === 0)
     20        break L;
     21 
     22    // Find the script index for "f".
     23    var scriptIndex = -1;
     24    for (var i = 0; i < n; ++i) {
     25        var summary = JSON.parse(pccount.summary(i));
     26        if (summary.name === "f")
     27            scriptIndex = i;
     28    }
     29 
     30    // Stop if the script index for "f" wasn't found.
     31    if (scriptIndex < 0)
     32        break L;
     33 
     34    // Retrieve the profiler data for "f".
     35    var contents = pccount.contents(scriptIndex);
     36    assertEq(typeof contents, "string");
     37 
     38    // The profiler data should be parsable as JSON text.
     39    var contents = JSON.parse(contents, (name, value) => {
     40        // Split the Ion code segments into multiple entries.
     41        if (name === "code")
     42            return value.split("\n");
     43 
     44        return value;
     45    });
     46 
     47    // Pretty print the profiler data.
     48    var pretty = JSON.stringify(contents, null, 1);
     49    print(pretty);
     50 } finally {
     51    // Clean-up.
     52    pccount.purge();
     53 }