Add startTime to metrics
A metric startTime makes possible to order events by time reported. Change-Id: I1a404192dbf989e1762846cdaa22a5c1adffff8c
This commit is contained in:
		@@ -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);
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user