Add startTime to metrics

A metric startTime makes possible to order events by time reported.

Change-Id: I1a404192dbf989e1762846cdaa22a5c1adffff8c
This commit is contained in:
Milutin Kristofic
2019-12-12 14:23:12 +01:00
parent 4c67be5d24
commit e854046d86
2 changed files with 25 additions and 7 deletions

View File

@@ -184,7 +184,7 @@
}, },
now() { now() {
return window.performance.now(); return Math.round(window.performance.now());
}, },
_arePluginsLoaded() { _arePluginsLoaded() {
@@ -205,6 +205,7 @@
detectedExtensions, detectedExtensions,
repoName: reportRepoName, repoName: reportRepoName,
isInBackgroundTab: document.visibilityState === 'hidden', isInBackgroundTab: document.visibilityState === 'hidden',
startTimeMs: this.now(),
}; };
args.splice(4, 0, contextInfo); args.splice(4, 0, contextInfo);
report.apply(this, args); report.apply(this, args);
@@ -238,6 +239,9 @@
if (contextInfo && contextInfo.isInBackgroundTab !== undefined) { if (contextInfo && contextInfo.isInBackgroundTab !== undefined) {
detail.inBackgroundTab = contextInfo.isInBackgroundTab; detail.inBackgroundTab = contextInfo.isInBackgroundTab;
} }
if (contextInfo && contextInfo.startTimeMs) {
detail.startTime = contextInfo.startTimeMs;
}
document.dispatchEvent(new CustomEvent(type, {detail})); document.dispatchEvent(new CustomEvent(type, {detail}));
if (opt_noLog) { return; } if (opt_noLog) { return; }
if (type === ERROR.TYPE && category === ERROR.CATEGORY) { if (type === ERROR.TYPE && category === ERROR.CATEGORY) {
@@ -449,7 +453,7 @@
// Guard against division by zero. // Guard against division by zero.
if (!denominator) { return; } if (!denominator) { return; }
const time = Math.round(this.now() - baseTime); const time = this.now() - baseTime;
this._reportTiming(averageName, time / denominator); this._reportTiming(averageName, time / denominator);
}, },
@@ -459,8 +463,7 @@
* @param {number} time The time to report as an integer of milliseconds. * @param {number} time The time to report as an integer of milliseconds.
*/ */
_reportTiming(name, time) { _reportTiming(name, time) {
this.reporter(TIMING.TYPE, TIMING.CATEGORY_UI_LATENCY, name, this.reporter(TIMING.TYPE, TIMING.CATEGORY_UI_LATENCY, name, time);
Math.round(time));
}, },
/** /**

View File

@@ -160,14 +160,14 @@ limitations under the License.
test('time and timeEnd', () => { test('time and timeEnd', () => {
const nowStub = sandbox.stub(element, 'now').returns(0); const nowStub = sandbox.stub(element, 'now').returns(0);
element.time('foo'); element.time('foo');
nowStub.returns(1.1); nowStub.returns(1);
element.time('bar'); element.time('bar');
nowStub.returns(2); nowStub.returns(2);
element.timeEnd('bar'); element.timeEnd('bar');
nowStub.returns(3.511); nowStub.returns(3);
element.timeEnd('foo'); element.timeEnd('foo');
assert.isTrue(element.reporter.calledWithExactly( assert.isTrue(element.reporter.calledWithExactly(
'timing-report', 'UI Latency', 'foo', 4 'timing-report', 'UI Latency', 'foo', 3
)); ));
assert.isTrue(element.reporter.calledWithExactly( assert.isTrue(element.reporter.calledWithExactly(
'timing-report', 'UI Latency', 'bar', 1 'timing-report', 'UI Latency', 'bar', 1
@@ -249,6 +249,21 @@ limitations under the License.
)); ));
}); });
test('report start time', () => {
element.reporter.restore();
sandbox.stub(element, 'now').returns(42);
sandbox.spy(element, 'defaultReporter');
const dispatchStub = sandbox.spy(document, 'dispatchEvent');
element.pluginsLoaded();
element.time('timeAction');
element.timeEnd('timeAction');
assert.isTrue(element.defaultReporter.getCall(2).calledWithMatch(
'timing-report', 'UI Latency', 'timeAction', 0,
{startTimeMs: 42}
));
assert.equal(dispatchStub.getCall(2).args[0].detail.startTime, 42);
});
suite('plugins', () => { suite('plugins', () => {
setup(() => { setup(() => {
element.reporter.restore(); element.reporter.restore();