Bug 1631822 - Return a random TRR from getFastestTRR if requested. r=johannh

Differential Revision: https://phabricator.services.mozilla.com/D73359
This commit is contained in:
Nihanth Subramanya 2020-05-01 20:05:47 +00:00
Родитель de3f5af147
Коммит eae10ebb4f
3 изменённых файлов: 34 добавлений и 6 удалений

Просмотреть файл

@ -295,22 +295,27 @@ class TRRRacer {
}
}
getFastestTRR() {
getFastestTRR(returnRandomDefault = false) {
if (!this._complete) {
throw new Error("getFastestTRR: Measurement still running.");
}
return this._getFastestTRRFromResults(this._aggregator.results);
return this._getFastestTRRFromResults(
this._aggregator.results,
returnRandomDefault
);
}
/*
* Given an array of { trr, time }, returns the trr with smallest mean time.
* Separate from _getFastestTRR for easy unit-testing.
*
* @returns The TRR with the fastest average time, or undefined if no result
* had a valid measured time (!= -1)
* @returns The TRR with the fastest average time.
* If returnRandomDefault is false-y, returns undefined if no valid
* times were present in the results. Otherwise, returns one of the
* present TRRs at random.
*/
_getFastestTRRFromResults(results) {
_getFastestTRRFromResults(results, returnRandomDefault = false) {
// First, organize the results into a map of TRR -> array of times
let TRRTimingMap = new Map();
for (let { trr, time } of results) {
@ -346,6 +351,10 @@ class TRRRacer {
}
}
if (returnRandomDefault && !fastestTRR) {
fastestTRR = trrs[Math.floor(Math.random() * trrs.length)];
}
return fastestTRR;
}

Просмотреть файл

@ -175,4 +175,23 @@ add_task(async function test_TRRRacer_getFastestTRRFromResults() {
let racer = new TRRRacer();
let fastest = racer._getFastestTRRFromResults(results);
Assert.equal(fastest, "trr2");
// When no valid entries are available, undefined is the default output.
results = [
{ trr: "trr1", time: -1 },
{ trr: "trr2", time: -1 },
];
fastest = racer._getFastestTRRFromResults(results);
Assert.equal(fastest, undefined);
// When passing `returnRandomDefault = true`, verify that both TRRs are
// possible outputs. The probability that the randomization is working
// correctly and we consistently get the same output after 50 iterations is
// 0.5^50 ~= 8.9*10^-16.
let firstResult = racer._getFastestTRRFromResults(results, true);
while (racer._getFastestTRRFromResults(results, true) == firstResult) {
continue;
}
Assert.ok(true, "Both TRRs were possible outputs when all results invalid.");
});

Просмотреть файл

@ -44,7 +44,7 @@ this.trrselect = class trrselect extends ExtensionAPI {
await new Promise(resolve => {
let racer = new TRRRacer(() => {
setDryRunResultAndRecordTelemetry(racer.getFastestTRR());
setDryRunResultAndRecordTelemetry(racer.getFastestTRR(true));
resolve();
});
racer.run();