bug 1440673 - Test Event Summarization in xpcshell r=Dexter

To test the TelemetryEvents portion of the code we need TelemetryEvents
tests. (The gtests only cover portions deeper than the TelemetryScalar API).

MozReview-Commit-ID: ExaiW0OiwFI

--HG--
extra : rebase_source : 1e352e1a8c8172d92eec3270d332818e6de5ba24
This commit is contained in:
Chris H-C 2018-04-03 14:38:51 -04:00
parent 088b18b6de
commit befcb6a760

View File

@ -36,6 +36,26 @@ function checkEventFormat(events) {
}
}
/**
* @param summaries is of the form
* [{process, [event category, event object, event method], count}]
* @param clearScalars - true if you want to clear the scalars
*/
function checkEventSummary(summaries, clearScalars) {
let scalars = Telemetry.snapshotKeyedScalars(OPTOUT, clearScalars);
dump(JSON.stringify(summaries));
for (let [process, [category, eObject, method], count] of summaries) {
let uniqueEventName = `${category}#${eObject}#${method}`;
let summaryCount;
if (process === "dynamic") {
summaryCount = scalars.dynamic["telemetry.dynamic_event_counts"][uniqueEventName];
} else {
summaryCount = scalars[process]["telemetry.event_counts"][uniqueEventName];
}
Assert.equal(summaryCount, count, `${uniqueEventName} had wrong summary count`);
}
}
add_task(async function test_recording_state() {
const events = [
["telemetry.test", "test1", "object1"],
@ -46,6 +66,7 @@ add_task(async function test_recording_state() {
events.forEach(e => Telemetry.recordEvent(...e));
let snapshot = Telemetry.snapshotEvents(OPTIN, true);
Assert.equal(Object.keys(snapshot).length, 0, "Should not have recorded any events.");
checkEventSummary(events.map(e => (["parent", e, 1])), true);
// Enable one test category and see that we record correctly.
Telemetry.setEventRecordingEnabled("telemetry.test", true);
@ -54,6 +75,7 @@ add_task(async function test_recording_state() {
Assert.ok(("parent" in snapshot), "Should have entry for main process.");
Assert.equal(snapshot.parent.length, 1, "Should have recorded one event.");
Assert.equal(snapshot.parent[0][1], "telemetry.test", "Should have recorded one event in telemetry.test");
checkEventSummary(events.map(e => (["parent", e, 1])), true);
// Also enable the other test category and see that we record correctly.
Telemetry.setEventRecordingEnabled("telemetry.test.second", true);
@ -63,6 +85,7 @@ add_task(async function test_recording_state() {
Assert.equal(snapshot.parent.length, 2, "Should have recorded two events.");
Assert.equal(snapshot.parent[0][1], "telemetry.test", "Should have recorded one event in telemetry.test");
Assert.equal(snapshot.parent[1][1], "telemetry.test.second", "Should have recorded one event in telemetry.test.second");
checkEventSummary(events.map(e => (["parent", e, 1])), true);
// Now turn of one category again and check that this works as expected.
Telemetry.setEventRecordingEnabled("telemetry.test", false);
@ -71,6 +94,7 @@ add_task(async function test_recording_state() {
Assert.ok(("parent" in snapshot), "Should have entry for main process.");
Assert.equal(snapshot.parent.length, 1, "Should have recorded one event.");
Assert.equal(snapshot.parent[0][1], "telemetry.test.second", "Should have recorded one event in telemetry.test.second");
checkEventSummary(events.map(e => (["parent", e, 1])), true);
});
add_task(async function recording_setup() {
@ -81,6 +105,7 @@ add_task(async function recording_setup() {
});
add_task(async function test_recording() {
Telemetry.clearScalars();
Telemetry.clearEvents();
// Record some events.
@ -116,6 +141,19 @@ add_task(async function test_recording() {
}
}
// Check that the events were summarized properly.
let summaries = {};
expected.forEach(({optout, event}) => {
let [category, eObject, method] = event;
let uniqueEventName = `${category}#${eObject}#${method}`;
if (!(uniqueEventName in summaries)) {
summaries[uniqueEventName] = ["parent", event, 1];
} else {
summaries[uniqueEventName][2]++;
}
});
checkEventSummary(Object.values(summaries), true);
// The following should not result in any recorded events.
Assert.throws(() => Telemetry.recordEvent("unknown.category", "test1", "object1"),
/Error: Unknown event: \["unknown.category", "test1", "object1"\]/,
@ -299,6 +337,7 @@ add_task(async function test_unicodeValues() {
add_task(async function test_dynamicEvents() {
Telemetry.clearEvents();
Telemetry.clearScalars();
Telemetry.canRecordExtended = true;
// Register some test events.
@ -360,6 +399,9 @@ add_task(async function test_dynamicEvents() {
"Should have recorded the expected event data.");
}
// Check that we've summarized the recorded events
checkEventSummary(expected.map(ev => ["dynamic", ev, 1]), true);
// Check that the opt-out snapshot contains only the one expected event.
snapshot = Telemetry.snapshotEvents(OPTOUT, false);
Assert.ok(("dynamic" in snapshot), "Should have dynamic events in the snapshot.");