summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/metrics/system_health/responsiveness_metric.html
blob: 5098c535277ddf12329cd935491e86ab4f80a017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/statistics.html">
<link rel="import" href="/tracing/metrics/metric_registry.html">
<link rel="import" href="/tracing/metrics/system_health/utils.html">
<link rel="import" href="/tracing/model/user_model/animation_expectation.html">
<link rel="import" href="/tracing/model/user_model/load_expectation.html">
<link rel="import" href="/tracing/model/user_model/response_expectation.html">
<link rel="import" href="/tracing/value/numeric.html">
<link rel="import" href="/tracing/value/value.html">

<script>
'use strict';

tr.exportTo('tr.metrics.sh', function() {
  // In the case of Response, Load, and DiscreteAnimation IRs, Responsiveness is
  // derived from the time between when the user thinks they begin an interation
  // (expectedStart) and the time when the screen first changes to reflect the
  // interaction (actualEnd).  There may be a delay between expectedStart and
  // when chrome first starts processing the interaction (actualStart) if the
  // main thread is busy.  The user doesn't know when actualStart is, they only
  // know when expectedStart is. User responsiveness, by definition, considers
  // only what the user experiences, so "duration" is defined as actualEnd -
  // expectedStart.

  function computeAnimationThroughput(animationExpectation) {
    if (animationExpectation.frameEvents === undefined ||
        animationExpectation.frameEvents.length === 0)
      throw new Error('Animation missing frameEvents ' +
                      animationExpectation.stableId);

    var durationSeconds = animationExpectation.duration / 1000;
    return animationExpectation.frameEvents.length / durationSeconds;
  }

  function computeAnimationframeTimeDiscrepancy(animationExpectation) {
    if (animationExpectation.frameEvents === undefined ||
        animationExpectation.frameEvents.length === 0)
      throw new Error('Animation missing frameEvents ' +
                      animationExpectation.stableId);

    var frameTimestamps = animationExpectation.frameEvents;
    frameTimestamps = frameTimestamps.toArray().map(function(event) {
      return event.start;
    });

    var absolute = false;
    return tr.b.Statistics.timestampsDiscrepancy(frameTimestamps, absolute);
  }

  var RESPONSE_NUMERIC_BUILDER = tr.v.NumericBuilder.createLinear(
      tr.v.Unit.byName.timeDurationInMs_smallerIsBetter,
      tr.b.Range.fromExplicitRange(100, 1000), 90);

  var THROUGHPUT_NUMERIC_BUILDER = tr.v.NumericBuilder.createLinear(
      tr.v.Unit.byName.unitlessNumber_biggerIsBetter,
      tr.b.Range.fromExplicitRange(10, 60), 10);

  var DISCREPANCY_NUMERIC_BUILDER = tr.v.NumericBuilder.createLinear(
      tr.v.Unit.byName.unitlessNumber_smallerIsBetter,
      tr.b.Range.fromExplicitRange(0, 1), 50);

  var LATENCY_NUMERIC_BUILDER = tr.v.NumericBuilder.createLinear(
      tr.v.Unit.byName.timeDurationInMs_smallerIsBetter,
      tr.b.Range.fromExplicitRange(0, 300), 60);

  /**
   * @param {!tr.v.ValueSet} values
   * @param {!tr.model.Model} model
   * @param {!Object=} opt_options
   */
  function responsivenessMetric(values, model, opt_options) {
    // TODO(benjhayden): Add categories to benchmark to support:
    // tr.metrics.sh.firstPaintMetric(values, model);

    var responseNumeric = RESPONSE_NUMERIC_BUILDER.build();
    var throughputNumeric = THROUGHPUT_NUMERIC_BUILDER.build();
    var frameTimeDiscrepancyNumeric = DISCREPANCY_NUMERIC_BUILDER.build();
    var latencyNumeric = LATENCY_NUMERIC_BUILDER.build();

    model.userModel.expectations.forEach(function(ue) {
      if (opt_options && opt_options.rangeOfInterest &&
          !opt_options.rangeOfInterest.intersectsExplicitRangeExclusive(
            ue.start, ue.end))
        return;

      var sourceInfo = {userExpectationId: ue.stableId};

      // Responsiveness is not defined for Idle.
      if (ue instanceof tr.model.um.IdleExpectation) {
        return;
      } else if (ue instanceof tr.model.um.LoadExpectation) {
        // This is already covered by firstPaintMetric.
      } else if (ue instanceof tr.model.um.ResponseExpectation) {
        responseNumeric.add(ue.duration, sourceInfo);
      } else if (ue instanceof tr.model.um.AnimationExpectation) {
        var throughput = computeAnimationThroughput(ue);
        if (throughput === undefined)
          throw new Error('Missing throughput for ' +
                          ue.stableId);

        throughputNumeric.add(throughput, sourceInfo);

        var frameTimeDiscrepancy = computeAnimationframeTimeDiscrepancy(ue);
        if (frameTimeDiscrepancy === undefined)
          throw new Error('Missing frameTimeDiscrepancy for ' +
                          ue.stableId);

        frameTimeDiscrepancyNumeric.add(frameTimeDiscrepancy, sourceInfo);

        ue.associatedEvents.forEach(function(event) {
          if (!(event instanceof tr.e.cc.InputLatencyAsyncSlice))
            return;

          latencyNumeric.add(event.duration, sourceInfo);
        });
      } else {
        throw new Error('Unrecognized stage for ' + ue.stableId);
      }
    });

    [
      responseNumeric, throughputNumeric, frameTimeDiscrepancyNumeric,
      latencyNumeric
    ].forEach(function(numeric) {
      numeric.customizeSummaryOptions({
        avg: true,
        max: true,
        min: true,
        std: true
      });
    });

    values.addValue(new tr.v.NumericValue(
        'response latency', responseNumeric));
    values.addValue(new tr.v.NumericValue(
        'animation throughput', throughputNumeric));
    values.addValue(new tr.v.NumericValue(
        'animation frameTimeDiscrepancy',
        frameTimeDiscrepancyNumeric));
    values.addValue(new tr.v.NumericValue(
        'animation latency', latencyNumeric));
  }

  tr.metrics.MetricRegistry.register(responsivenessMetric, {
    supportsRangeOfInterest: true
  });

  return {
    responsivenessMetric: responsivenessMetric,
  };
});
</script>