tor-browser

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

worker-performance.worker.js (7066B)


      1 "use strict";
      2 
      3 // Tests that most of the functionality of the window.performance object is available in web workers.
      4 
      5 importScripts("/resources/testharness.js");
      6 
      7 function verifyEntry (entry, name, type, duration, assertName) {
      8    assert_equals(entry.name, name, assertName + " has the right name");
      9    assert_equals(entry.entryType, type, assertName + " has the right type");
     10    assert_equals(entry.duration, duration, assertName + "has the right duration");
     11 }
     12 
     13 var start;
     14 test(function testPerformanceNow () {
     15    start = performance.now();
     16 }, "Can use performance.now in workers");
     17 
     18 test(function testPerformanceMark () {
     19    while (performance.now() == start) { }
     20    performance.mark("mark1");
     21     // Stall the minimum amount of time to ensure the marks are separate
     22    var now = performance.now();
     23    while (performance.now() == now) { }
     24    performance.mark("mark2");
     25 }, "Can use performance.mark in workers");
     26 
     27 test(function testPerformanceMeasure () {
     28    performance.measure("measure1", "mark1", "mark2");
     29 }, "Can use performance.measure in workers");
     30 
     31 test(function testPerformanceGetEntriesByName () {
     32    var mark1s = performance.getEntriesByName("mark1");
     33    assert_equals(mark1s.length, 1, "getEntriesByName gave correct number of entries");
     34    verifyEntry(mark1s[0], "mark1", "mark", 0, "Entry got by name");
     35 }, "Can use performance.getEntriesByName in workers");
     36 
     37 var marks;
     38 var measures;
     39 test(function testPerformanceGetEntriesByType () {
     40    marks = performance.getEntriesByType("mark");
     41    assert_equals(marks.length, 2, "getEntriesByType gave correct number of entries");
     42    verifyEntry(marks[0], "mark1", "mark", 0, "First mark entry");
     43    verifyEntry(marks[1], "mark2", "mark", 0, "Second mark entry");
     44    measures = performance.getEntriesByType("measure");
     45    assert_equals(measures.length, 1, "getEntriesByType(\"measure\") gave correct number of entries");
     46    verifyEntry(measures[0], "measure1", "measure", marks[1].startTime - marks[0].startTime, "Measure entry");
     47 }, "Can use performance.getEntriesByType in workers");
     48 
     49 test(function testPerformanceEntryOrder () {
     50    assert_greater_than(marks[0].startTime, start, "First mark startTime is after a time before it");
     51    assert_greater_than(marks[1].startTime, marks[0].startTime, "Second mark startTime is after first mark startTime");
     52    assert_equals(measures[0].startTime, marks[0].startTime, "measure's startTime is the first mark's startTime");
     53 }, "Performance marks and measures seem to be working correctly in workers");
     54 
     55 test(function testPerformanceClearing () {
     56    performance.clearMarks();
     57    assert_equals(performance.getEntriesByType("mark").length, 0, "clearMarks cleared the marks");
     58    performance.clearMeasures();
     59    assert_equals(performance.getEntriesByType("measure").length, 0, "clearMeasures cleared the measures");
     60 }, "Can use clearMarks and clearMeasures in workers");
     61 
     62 test(function testPerformanceResourceTiming () {  // Resource timing
     63    var start = performance.now();
     64    var xhr = new XMLHttpRequest();
     65     // Do a synchronous request and add a little artificial delay
     66    xhr.open("GET", "/resources/testharness.js?pipe=trickle(d0.25)", false);
     67    xhr.send();
     68     // The browser might or might not have added a resource performance entry for the importScripts() above; we're only interested in xmlhttprequest entries
     69    var entries = performance.getEntriesByType("resource").filter(entry => entry.initiatorType == "xmlhttprequest");
     70    assert_equals(entries.length, 1, "getEntriesByType(\"resource\") returns one entry with initiatorType of xmlhttprequest");
     71    assert_true(!!entries[0].name.match(/\/resources\/testharness.js/), "Resource entry has loaded url as its name");
     72    assert_equals(entries[0].entryType, "resource", "Resource entry has correct entryType");
     73    assert_equals(entries[0].initiatorType, "xmlhttprequest", "Resource entry has correct initiatorType");
     74    var currentTimestamp = start;
     75    var currentTimestampName = "a time before it";
     76    [
     77        "startTime", "fetchStart", "requestStart", "responseStart", "responseEnd"
     78    ].forEach((name) => {
     79        var timestamp = entries[0][name];
     80        // We want to skip over values that are 0 because of TAO securty rescritions
     81        // Or else this test will fail. This can happen for "requestStart", "responseStart".
     82        if (timestamp != 0) {
     83            assert_greater_than_equal(timestamp, currentTimestamp, "Resource entry " + name + " is after " + currentTimestampName);
     84            currentTimestamp = timestamp;
     85            currentTimestampName = name;
     86        }
     87    });
     88    assert_greater_than(entries[0].responseEnd, entries[0].startTime, "The resource request should have taken at least some time");
     89     // We requested a delay of 250ms, but it could be a little bit less, and could be significantly more.
     90    assert_greater_than(entries[0].responseEnd - entries[0].responseStart, 230, "Resource timing numbers reflect reality somewhat");
     91 }, "Resource timing seems to work in workers");
     92 
     93 test(function testPerformanceClearResourceTimings () {
     94    performance.clearResourceTimings();
     95    assert_equals(performance.getEntriesByType("resource").length, 0, "clearResourceTimings cleared the resource timings");
     96 }, "performance.clearResourceTimings in workers");
     97 
     98 test(function testPerformanceSetResourceTimingBufferSize () {
     99    performance.setResourceTimingBufferSize(0);
    100    var xhr = new XMLHttpRequest();
    101    xhr.open("GET", "/resources/testharness.js", false);  // synchronous request
    102    xhr.send();
    103    assert_equals(performance.getEntriesByType("resource").length, 0, "setResourceTimingBufferSize(0) prevents resource entries from being added");
    104 }, "performance.setResourceTimingBufferSize in workers");
    105 
    106 test(function testPerformanceHasNoTiming () {
    107    assert_equals(typeof(performance.timing), "undefined", "performance.timing is undefined");
    108 }, "performance.timing is not available in workers");
    109 
    110 test(function testPerformanceHasNoNavigation () {
    111    assert_equals(typeof(performance.navigation), "undefined", "performance.navigation is undefined");
    112 }, "performance.navigation is not available in workers");
    113 
    114 test(function testPerformanceHasToJSON () {
    115    assert_equals(typeof(performance.toJSON), "function", "performance.toJSON is a function");
    116 }, "performance.toJSON is available in workers");
    117 
    118 test(function testPerformanceNoNavigationEntries () {
    119    assert_equals(performance.getEntriesByType("navigation").length, 0, "getEntriesByType(\"navigation\") returns nothing");
    120    assert_equals(performance.getEntriesByName("document", "navigation").length, 0, "getEntriesByName(\"document\", \"navigation\") returns nothing");
    121    assert_equals(performance.getEntriesByName("document").length, 0, "getEntriesByName(\"document\") returns nothing");
    122    var hasNavigation = performance.getEntries().some((e,i,a) => {
    123        return e.entryType == "navigation";
    124    });
    125    assert_false(hasNavigation, "getEntries should return no navigation entries.");
    126 
    127 }, "There are no navigation type performance entries in workers");
    128 
    129 done();