Skip to content

Commit

Permalink
AutoXHR: Do not extend the event timeout with each uninteresting muta…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
querymetrics authored and nicjansma committed Apr 4, 2018
1 parent 58b0879 commit 1fdfcda
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/auto-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,10 @@
});
}

if (!evt.interesting) {
// if we didn't have any interesting nodes for this MO callback or
// any prior callbacks, timeout the event
if (!this.timer && !evt.interesting) {
// timeout the event if we haven't already created a timer and
// we didn't have any interesting nodes for this MO callback or
// any prior callbacks
this.setTimeout(SPA_TIMEOUT, index);
}

Expand Down Expand Up @@ -1477,7 +1478,8 @@
* - We turn on DOM observer, and wait up to 50 milliseconds for something
* - If nothing happens after the timeout, we stop watching and clear the resource without firing the event
* - If a history event happened recently/will happen shortly, use the URL as the resource.url
* - Else if something uninteresting happens, we extend the timeout for 1 second
* - Else if something uninteresting happens, we set the timeout for 1 second if it wasn't already started
* - We don't want to continuously extend the timeout with each uninteresting event
* - Else if an interesting node is added, we add load and error listeners and turn off the timeout but keep watching
* - If we do not have a resource.url, and if this is a script, then we use the script's URL
* - Once all listeners have fired, we stop watching, fire the event and clear the resource
Expand All @@ -1490,7 +1492,8 @@
* - If a history event happened recently/will happen shortly, use the URL as the resource.url
* - We watch for all changes in state (for async requests) and for load (for all requests)
* - On load, we turn on DOM observer, and wait up to 50 milliseconds for something
* - If something uninteresting happens, we extend the timeout for 1 second
* - If something uninteresting happens, we set the timeout for 1 second if it wasn't already started
* - We don't want to continuously extend the timeout with each uninteresting event
* - Else if an interesting node is added, we add load and error listeners and turn off the timeout
* - Once all listeners have fired, we stop watching, fire the event and clear the resource
* - If nothing happens after the timeout, we stop watching fire the event and clear the resource
Expand Down
77 changes: 77 additions & 0 deletions tests/page-templates/07-autoxhr/38-xhr-uninteresting-mo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<%= header %>
<!--
Verify that creating uninteresting mutation observer events doesn't hold an xhr beacon from being sent
-->
<script src="38-xhr-uninteresting-mo.js" type="text/javascript"></script>
<%= boomerangSnippet %>
<script>
// create non interesting MO events a few times per second
function addDiv() {
var div = window.document.createElement("div");
window.document.body.appendChild(div);
setTimeout(addDiv, 200);
};
setTimeout(addDiv, 200);

BOOMR_test.init({
"instrument_xhr": true,
afterFirstBeacon:
function() {
if (!BOOMR.plugins.AutoXHR) {
return;
}

var xhrNum = 0;
function sendBeacon() {
setTimeout(function() {
var xhr;
if (xhrNum === 0) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script200.js"); //200, async
xhr.send(null);
}
else if (xhrNum === 1) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script200.js", false); //200, sync
xhr.send(null);
}
else if (xhrNum === 2) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script404.js"); //404, async
xhr.send(null);
}
else if (xhrNum === 3) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script404.js", false); //404, sync
xhr.send(null);
}
else if (xhrNum === 4) {
xhr = new XMLHttpRequest();
xhr.open("GET", "http://soasta.com"); //error
xhr.send(null);
}
else if (xhrNum === 5) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script200.js"); //200, async, abort
xhr.send(null);
xhr.abort();
}
else if (xhrNum === 6) {
xhr = new XMLHttpRequest();
xhr.open("GET", "support/script200.js"); //200, async, timeout
xhr.timeout = 1;
xhr.send(null);
}
xhrNum++;
}, 100);
}

// send a new XHR on each beacon
BOOMR.subscribe("onbeacon", sendBeacon);

// start the first beacon
sendBeacon();
}
});
</script>
<%= footer %>
115 changes: 115 additions & 0 deletions tests/page-templates/07-autoxhr/38-xhr-uninteresting-mo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*eslint-env mocha*/
/*global assert*/

describe("e2e/07-autoxhr/38-xhr-uninteresting-mo", function() {
var t = BOOMR_test;
var tf = BOOMR.plugins.TestFramework;

it("Should get 8 beacons: 1 onload, 7 xhr (XMLHttpRequest !== null)", function(done) {
this.timeout(10000);
t.ifAutoXHR(
done,
function() {
t.ensureBeaconCount(done, 8);
});
});

it("Should get 8 beacons: 1st onload beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[0].u, "38-xhr-uninteresting-mo.html");
done();
});
});

it("Should get 8 beacons: 2nd XHR 200 async beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[1].u, "script200.js");
done();
});
});

it("Should get 8 beacons: 3rd XHR 200 sync beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[2].u, "script200.js");
done();
});
});

it("Should get 8 beacons: 4th XHR 404 async beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[3].u, "script404.js");
done();
});
});

it("Should get 8 beacons: 5th XHR 404 sync beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[4].u, "script404.js");
done();
});
});

it("Should get 8 beacons: 6th X-O beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[5].u, "soasta.com");
done();
});
});

it("Should get 8 beacons: 7th abort beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[6].u, "script200.js");
done();
});
});

it("Should get 8 beacons: 8th timeout beacon (XMLHttpRequest !== null)", function(done) {
t.ifAutoXHR(
done,
function() {
assert.include(tf.beacons[7].u, "script200.js");
done();
});
});

it("Should get 1 beacons: 1 onload, 0 xhr (XMLHttpRequest === null)", function(done) {
t.ifAutoXHR(
done,
undefined,
function() {
t.ensureBeaconCount(done, 1);
});
});

it("Should have all beacons set rt.nstart = navigationTiming (if NavigationTiming is supported)", function(done) {
t.ifAutoXHR(
done,
undefined,
function() {
if (typeof BOOMR.plugins.RT.navigationStart() !== "undefined") {
for (var i = 0; i <= 7; i++) {
assert.equal(tf.beacons[i]["rt.nstart"], BOOMR.plugins.RT.navigationStart());
}
}
else {
done();
}
}
);
});

});

0 comments on commit 1fdfcda

Please sign in to comment.