Skip to content

Commit d9711c3

Browse files
yoavweissChrome-bot
authored and
Chrome-bot
committed
Align resource timing buffer full processing to spec PR 163
This change implements the processing model from PR 163[1], when it comes to setResourceTimingBufferSize(), clearResourceTimings() and the firing of the resourcetimingbufferfull event. [1] w3c/resource-timing#163 Change-Id: I3fd901fa411dd128a723e77cd120f50974a775a8
1 parent 412054b commit d9711c3

6 files changed

+148
-44
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head onload>
4+
<meta charset="utf-8" />
5+
<title>This test validates increasing the buffer size in onresourcetimingbufferfull callback of resource timing.</title>
6+
<link rel="author" title="Intel" href="http://www.intel.com/" />
7+
<link rel="help" href="http://www.w3.org/TR/resource-timing/#performanceresourcetiming"/>
8+
<script src="/resources/testharness.js"></script>
9+
<script src="/resources/testharnessreport.js"></script>
10+
<script src="resources/append.js"></script>
11+
</head>
12+
<body onload=onload_test()>
13+
<script>
14+
const resource_timing_buffer_size = 1;
15+
performance.clearResourceTimings();
16+
var increase = function() {
17+
performance.setResourceTimingBufferSize(resource_timing_buffer_size * 2);
18+
}
19+
performance.addEventListener('resourcetimingbufferfull', increase);
20+
performance.setResourceTimingBufferSize(resource_timing_buffer_size);
21+
// Scripts appended in JS to ensure setResourceTimingBufferSize is called before.
22+
// This resource gets buffered in the resource timing entry buffer, then moved to the global buffer
23+
appendScript('resources/empty.js');
24+
// This resource overflows the entry buffer, goes into the secondary buffer and eventually copied to the entry buffer, after its size was increased.
25+
appendScript('resources/empty_script.js');
26+
setup({ explicit_done: true });
27+
function onload_test() {
28+
test(function() {
29+
assert_equals(performance.getEntriesByType('resource').length, 2,
30+
'Both entries should be stored in resource timing buffer since its increases size once it overflows.');
31+
assert_true(performance.getEntriesByType('resource')[0].name.includes('empty.js'), "empty.js is in the entries buffer");
32+
assert_true(performance.getEntriesByType('resource')[1].name.includes('empty_script.js'), "empty_script.js is in the entries buffer");
33+
done();
34+
});
35+
}
36+
</script>
37+
</body>
38+
</html>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="help" href="https://w3c.github.io/resource-timing/#dom-performance-setresourcetimingbuffersize">
6+
<title>This test validates that setResourceTimingBufferFull behaves appropriately when set to the current buffer level.</title>
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
</head>
10+
<body>
11+
<script>
12+
let eventFired = false;
13+
function loadRandomResource() {
14+
return fetch(window.location.href + "?" + Math.random());
15+
}
16+
17+
setup(function() {
18+
// Get the browser into a consistent state.
19+
performance.clearResourceTimings();
20+
performance.setResourceTimingBufferSize(100);
21+
22+
window.result = "";
23+
24+
});
25+
26+
let gatherEntries = new Promise(function(resolve, reject) {
27+
// Gather up 3 Resource Entries to kick off the rest of test behavior.
28+
let observer = new PerformanceObserver(function(list) {
29+
let allResourceEntries = performance.getEntriesByType("resource");
30+
if (allResourceEntries.length !== 3)
31+
return;
32+
observer.disconnect();
33+
resolve();
34+
});
35+
observer.observe({entryTypes: ["resource"]});
36+
for (let i = 0; i < 3; ++i)
37+
loadRandomResource();
38+
});
39+
40+
let setBufferSize = new Promise(function(resolve, reject) {
41+
performance.onresourcetimingbufferfull = function() {
42+
eventFired = true;
43+
window.result += "Event Fired with " + performance.getEntriesByType("resource").length + " entries. ";
44+
};
45+
window.result += "before setLimit(3). ";
46+
performance.setResourceTimingBufferSize(3);
47+
window.result += "after setLimit(3). ";
48+
resolve();
49+
});
50+
51+
promise_test(function() {
52+
return gatherEntries;
53+
}, "Reset the entries number");
54+
promise_test(function() {
55+
return setBufferSize;
56+
}, "Set the buffer size");
57+
promise_test(function() {
58+
return new Promise(function(resolve, reject) {
59+
loadRandomResource().then(function() {
60+
window.result += "after loading 4th resource. ";
61+
resolve();
62+
})});
63+
}, "Overflow the buffer");
64+
promise_test(function() {
65+
return new Promise(function(resolve, reject) {
66+
let waitForIt = function() {
67+
if (eventFired) {
68+
resolve();
69+
}
70+
}
71+
step_timeout(waitForIt, 0);
72+
});
73+
}, "Wait for event");
74+
promise_test(function() {
75+
return new Promise(function(resolve, reject) {
76+
if(window.result != "before setLimit(3). after setLimit(3). after loading 4th resource. Event Fired with 3 entries. ") {
77+
reject("Non matching value: " + window.result);
78+
}
79+
resolve();
80+
});
81+
}, "Check result");
82+
</script>

resource-timing/resource_timing_buffer_full_when_populate_entries.html

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script src="/resources/testharnessreport.js"></script>
1010
<script src="resources/webperftestharness.js"></script>
1111
<script src="resources/webperftestharnessextension.js"></script>
12+
<script src="resources/append.js"></script>
1213
</head>
1314
<body onload=onload_test()>
1415
<script>
@@ -21,12 +22,6 @@
2122
}
2223
context.registerResourceTimingBufferFullCallback(buffer_full_callback);
2324
// Scripts appended in JS to ensure setResourceTimingBufferSize is called before.
24-
function appendScript(src) {
25-
const script = document.createElement('script');
26-
script.type = 'text/javascript';
27-
script.src = src;
28-
document.body.appendChild(script);
29-
}
3025
appendScript('resources/empty.js');
3126
appendScript('resources/empty_script.js');
3227
appendScript('resources/resource_timing_test0.js');

resource-timing/resource_timing_store_and_clear_during_callback.html

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,37 @@
77
<link rel="help" href="http://www.w3.org/TR/resource-timing/#performanceresourcetiming"/>
88
<script src="/resources/testharness.js"></script>
99
<script src="/resources/testharnessreport.js"></script>
10-
<script src="resources/webperftestharness.js"></script>
11-
<script src="resources/webperftestharnessextension.js"></script>
10+
<script src="resources/append.js"></script>
1211
</head>
1312
<body onload=onload_test()>
1413
<script>
15-
const context = new PerformanceContext(performance);
1614
const resource_timing_buffer_size = 1;
15+
performance.clearResourceTimings();
1716
let global_buffer = [];
18-
function store_and_clear() {
19-
const entryList = context.getEntriesByType('resource');
17+
let store_and_clear = function() {
18+
const entryList = performance.getEntriesByType('resource');
2019
entryList.forEach(function (entry) {
2120
global_buffer.push(entry);
2221
});
23-
context.clearResourceTimings();
22+
performance.clearResourceTimings();
2423
}
25-
context.registerResourceTimingBufferFullCallback(store_and_clear);
26-
context.setResourceTimingBufferSize(resource_timing_buffer_size);
24+
performance.addEventListener('resourcetimingbufferfull', store_and_clear);
25+
performance.setResourceTimingBufferSize(resource_timing_buffer_size);
2726
// Scripts appended in JS to ensure setResourceTimingBufferSize is called before.
28-
function appendScript(src) {
29-
const script = document.createElement('script');
30-
script.type = 'text/javascript';
31-
script.src = src;
32-
document.body.appendChild(script);
33-
}
27+
// This resource gets buffered in the resource timing entry buffer, then moved to the global buffer
3428
appendScript('resources/empty.js');
29+
// This resource overflows the entry buffer, goes into the secondary buffer and eventually copied to the entry buffer, after it was cleared.
3530
appendScript('resources/empty_script.js');
36-
appendScript('resources/resource_timing_test0.js');
3731
setup({ explicit_done: true });
3832
function onload_test() {
39-
test_equals(context.getEntriesByType('resource').length, 0, 'No entry should be stored in resource timing buffer since its cleared once an item arrived.');
40-
// The entry for empty.js must not be in the global buffer, but all others should be.
41-
test_equals(global_buffer.length, 6, '6 resource timing entries should be moved to global buffer.');
42-
const index = window.location.pathname.lastIndexOf('resource-timing');
43-
const pathname = window.location.pathname.substring(0, index);
44-
let expected_entries = {};
45-
expected_entries[pathname + 'resources/testharness.js'] = 'script';
46-
expected_entries[pathname + 'resources/testharnessreport.js'] = 'script';
47-
expected_entries[pathname + 'resource-timing/resources/webperftestharness.js'] = 'script';
48-
expected_entries[pathname + 'resource-timing/resources/webperftestharnessextension.js'] = 'script';
49-
expected_entries[pathname + 'resource-timing/resources/empty_script.js'] = 'script';
50-
expected_entries[pathname + 'resource-timing/resources/resource_timing_test0.js'] = 'script';
51-
test_resource_entries(global_buffer, expected_entries);
52-
done();
33+
test(function() {
34+
assert_equals(performance.getEntriesByType('resource').length, 1,
35+
"Only the last entry should be stored in resource timing buffer since it's cleared once it overflows.");
36+
assert_equals(global_buffer.length, 1, '1 resource timing entry should be moved to global buffer.');
37+
assert_true(global_buffer[0].name.includes('empty.js'), "empty.js is in the global buffer");
38+
assert_true(performance.getEntriesByType('resource')[0].name.includes('empty_script.js'), "empty_script.js is in the entries buffer");
39+
done();
40+
});
5341
}
5442
</script>
5543
</body>

resource-timing/resources/append.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function appendScript(src) {
2+
const script = document.createElement('script');
3+
script.type = 'text/javascript';
4+
script.src = src;
5+
document.body.appendChild(script);
6+
}
7+

service-workers/service-worker/resources/performance-timeline-worker.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,13 @@ promise_test(function(test) {
4444
assert_greater_than(entry.startTime, 0);
4545
assert_greater_than(entry.responseEnd, entry.startTime);
4646
}
47-
return Promise.race([
48-
new Promise(function(resolve) {
47+
return new Promise(function(resolve) {
4948
performance.onresourcetimingbufferfull = _ => {
5049
resolve('bufferfull');
5150
}
5251
performance.setResourceTimingBufferSize(expectedResources.length);
53-
}),
54-
55-
// Race the bufferfull event against another fetch. We should get the
56-
// event before this completes. This allows us to detect a failure
57-
// to dispatch the event without timing out the entire test.
58-
fetch('dummy.txt').then(resp => resp.text())
59-
]);
52+
fetch('dummy.txt');
53+
});
6054
})
6155
.then(function(result) {
6256
assert_equals(result, 'bufferfull');

0 commit comments

Comments
 (0)