From e854046d86abdc68e1206407ffc1432fbd5d50f1 Mon Sep 17 00:00:00 2001 From: Milutin Kristofic Date: Thu, 12 Dec 2019 14:23:12 +0100 Subject: [PATCH] Add startTime to metrics A metric startTime makes possible to order events by time reported. Change-Id: I1a404192dbf989e1762846cdaa22a5c1adffff8c --- .../core/gr-reporting/gr-reporting.js | 11 ++++++---- .../core/gr-reporting/gr-reporting_test.html | 21 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js index 3f2f4bdc5c..e2090347d6 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js @@ -184,7 +184,7 @@ }, now() { - return window.performance.now(); + return Math.round(window.performance.now()); }, _arePluginsLoaded() { @@ -205,6 +205,7 @@ detectedExtensions, repoName: reportRepoName, isInBackgroundTab: document.visibilityState === 'hidden', + startTimeMs: this.now(), }; args.splice(4, 0, contextInfo); report.apply(this, args); @@ -238,6 +239,9 @@ if (contextInfo && contextInfo.isInBackgroundTab !== undefined) { detail.inBackgroundTab = contextInfo.isInBackgroundTab; } + if (contextInfo && contextInfo.startTimeMs) { + detail.startTime = contextInfo.startTimeMs; + } document.dispatchEvent(new CustomEvent(type, {detail})); if (opt_noLog) { return; } if (type === ERROR.TYPE && category === ERROR.CATEGORY) { @@ -449,7 +453,7 @@ // Guard against division by zero. if (!denominator) { return; } - const time = Math.round(this.now() - baseTime); + const time = this.now() - baseTime; this._reportTiming(averageName, time / denominator); }, @@ -459,8 +463,7 @@ * @param {number} time The time to report as an integer of milliseconds. */ _reportTiming(name, time) { - this.reporter(TIMING.TYPE, TIMING.CATEGORY_UI_LATENCY, name, - Math.round(time)); + this.reporter(TIMING.TYPE, TIMING.CATEGORY_UI_LATENCY, name, time); }, /** diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html index 430b41e487..7728450b58 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html @@ -160,14 +160,14 @@ limitations under the License. test('time and timeEnd', () => { const nowStub = sandbox.stub(element, 'now').returns(0); element.time('foo'); - nowStub.returns(1.1); + nowStub.returns(1); element.time('bar'); nowStub.returns(2); element.timeEnd('bar'); - nowStub.returns(3.511); + nowStub.returns(3); element.timeEnd('foo'); assert.isTrue(element.reporter.calledWithExactly( - 'timing-report', 'UI Latency', 'foo', 4 + 'timing-report', 'UI Latency', 'foo', 3 )); assert.isTrue(element.reporter.calledWithExactly( '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', () => { setup(() => { element.reporter.restore();