summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/workers/worker-performance.worker.js
blob: c913b2e7375067c1a21bdc655f394b5c27bc5aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";

// Tests that most of the functionality of the window.performance object is available in web workers.

importScripts("/resources/testharness.js");

function verifyEntry (entry, name, type, duration, assertName) {
    assert_equals(entry.name, name, assertName + " has the right name");
    assert_equals(entry.entryType, type, assertName + " has the right type");
    assert_equals(entry.duration, duration, assertName + "has the right duration");
}

var start;
test(function testPerformanceNow () {
    start = performance.now();
}, "Can use performance.now in workers");

test(function testPerformanceMark () {
    while (performance.now() == start) { }
    performance.mark("mark1");
     // Stall the minimum amount of time to ensure the marks are separate
    var now = performance.now();
    while (performance.now() == now) { }
    performance.mark("mark2");
}, "Can use performance.mark in workers");

test(function testPerformanceMeasure () {
    performance.measure("measure1", "mark1", "mark2");
}, "Can use performance.measure in workers");

test(function testPerformanceGetEntriesByName () {
    var mark1s = performance.getEntriesByName("mark1");
    assert_equals(mark1s.length, 1, "getEntriesByName gave correct number of entries");
    verifyEntry(mark1s[0], "mark1", "mark", 0, "Entry got by name");
}, "Can use performance.getEntriesByName in workers");

var marks;
var measures;
test(function testPerformanceGetEntriesByType () {
    marks = performance.getEntriesByType("mark");
    assert_equals(marks.length, 2, "getEntriesByType gave correct number of entries");
    verifyEntry(marks[0], "mark1", "mark", 0, "First mark entry");
    verifyEntry(marks[1], "mark2", "mark", 0, "Second mark entry");
    measures = performance.getEntriesByType("measure");
    assert_equals(measures.length, 1, "getEntriesByType(\"measure\") gave correct number of entries");
    verifyEntry(measures[0], "measure1", "measure", marks[1].startTime - marks[0].startTime, "Measure entry");
}, "Can use performance.getEntriesByType in workers");

test(function testPerformanceEntryOrder () {
    assert_greater_than(marks[0].startTime, start, "First mark startTime is after a time before it");
    assert_greater_than(marks[1].startTime, marks[0].startTime, "Second mark startTime is after first mark startTime");
    assert_equals(measures[0].startTime, marks[0].startTime, "measure's startTime is the first mark's startTime");
}, "Performance marks and measures seem to be working correctly in workers");

test(function testPerformanceClearing () {
    performance.clearMarks();
    assert_equals(performance.getEntriesByType("mark").length, 0, "clearMarks cleared the marks");
    performance.clearMeasures();
    assert_equals(performance.getEntriesByType("measure").length, 0, "clearMeasures cleared the measures");
}, "Can use clearMarks and clearMeasures in workers");

test(function testPerformanceResourceTiming () {  // Resource timing
    var start = performance.now();
    var xhr = new XMLHttpRequest();
     // Do a synchronous request and add a little artificial delay
    xhr.open("GET", "/resources/testharness.js?pipe=trickle(d0.25)", false);
    xhr.send();
     // The browser might or might not have added a resource performance entry for the importScripts() above; we're only interested in xmlhttprequest entries
    var entries = performance.getEntriesByType("resource").filter(entry => entry.initiatorType == "xmlhttprequest");
    assert_equals(entries.length, 1, "getEntriesByType(\"resource\") returns one entry with initiatorType of xmlhttprequest");
    assert_true(!!entries[0].name.match(/\/resources\/testharness.js/), "Resource entry has loaded url as its name");
    assert_equals(entries[0].entryType, "resource", "Resource entry has correct entryType");
    assert_equals(entries[0].initiatorType, "xmlhttprequest", "Resource entry has correct initiatorType");
    var currentTimestamp = start;
    var currentTimestampName = "a time before it";
    [
        "startTime", "fetchStart", "requestStart", "responseStart", "responseEnd"
    ].forEach((name) => {
        var timestamp = entries[0][name];
        // We want to skip over values that are 0 because of TAO securty rescritions
        // Or else this test will fail. This can happen for "requestStart", "responseStart".
        if (timestamp != 0) {
            assert_greater_than_equal(timestamp, currentTimestamp, "Resource entry " + name + " is after " + currentTimestampName);
            currentTimestamp = timestamp;
            currentTimestampName = name;
        }
    });
    assert_greater_than(entries[0].responseEnd, entries[0].startTime, "The resource request should have taken at least some time");
     // We requested a delay of 250ms, but it could be a little bit less, and could be significantly more.
    assert_greater_than(entries[0].responseEnd - entries[0].responseStart, 230, "Resource timing numbers reflect reality somewhat");
}, "Resource timing seems to work in workers");

test(function testPerformanceClearResourceTimings () {
    performance.clearResourceTimings();
    assert_equals(performance.getEntriesByType("resource").length, 0, "clearResourceTimings cleared the resource timings");
}, "performance.clearResourceTimings in workers");

test(function testPerformanceSetResourceTimingBufferSize () {
    performance.setResourceTimingBufferSize(0);
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/resources/testharness.js", false);  // synchronous request
    xhr.send();
    assert_equals(performance.getEntriesByType("resource").length, 0, "setResourceTimingBufferSize(0) prevents resource entries from being added");
}, "performance.setResourceTimingBufferSize in workers");

test(function testPerformanceHasNoTiming () {
    assert_equals(typeof(performance.timing), "undefined", "performance.timing is undefined");
}, "performance.timing is not available in workers");

test(function testPerformanceHasNoNavigation () {
    assert_equals(typeof(performance.navigation), "undefined", "performance.navigation is undefined");
}, "performance.navigation is not available in workers");

test(function testPerformanceHasToJSON () {
    assert_equals(typeof(performance.toJSON), "function", "performance.toJSON is a function");
}, "performance.toJSON is available in workers");

test(function testPerformanceNoNavigationEntries () {
    assert_equals(performance.getEntriesByType("navigation").length, 0, "getEntriesByType(\"navigation\") returns nothing");
    assert_equals(performance.getEntriesByName("document", "navigation").length, 0, "getEntriesByName(\"document\", \"navigation\") returns nothing");
    assert_equals(performance.getEntriesByName("document").length, 0, "getEntriesByName(\"document\") returns nothing");
    var hasNavigation = performance.getEntries().some((e,i,a) => {
        return e.entryType == "navigation";
    });
    assert_false(hasNavigation, "getEntries should return no navigation entries.");

}, "There are no navigation type performance entries in workers");

done();