summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/model/helpers/android_model_helper_test.html
blob: 114bd5a8a10b46b17e1472a80fc2dda921be0433 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!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/core/test_utils.html">
<link rel="import" href="/tracing/extras/android/android_auditor.html">
<link rel="import" href="/tracing/extras/importer/linux_perf/ftrace_importer.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  var AndroidModelHelper = tr.model.helpers.AndroidModelHelper;
  var newAsyncSliceNamed = tr.c.TestUtils.newAsyncSliceNamed;
  var newSliceEx = tr.c.TestUtils.newSliceEx;
  var newCounterNamed = tr.c.TestUtils.newCounterNamed;
  var newCounterSeries = tr.c.TestUtils.newCounterSeries;

  function createSurfaceFlingerWithVsyncs(model) {
      if (model.getProcess(2))
        throw new Error('process already exists');

      var sfProcess = model.getOrCreateProcess(2);
      var sfThread = sfProcess.getOrCreateThread(2); // main thread, tid = pid
      sfThread.name = '/system/bin/surfaceflinger';

      // ensure slicegroup has data
      sfThread.sliceGroup.pushSlice(newSliceEx({
        title: 'doComposition',
        start: 8,
        duration: 2
      }));

      var counter = sfProcess.getOrCreateCounter('android', 'VSYNC');
      var series = newCounterSeries();
      for (var i = 0; i <= 10; i++) {
        series.addCounterSample(i * 10, i % 2);
      }
      counter.addSeries(series);
  }

  /*
   * List of customizeModelCallbacks which produce different 80ms frames,
   * each starting at 10ms, and with a single important slice
   */
  var SINGLE_FRAME_CUSTOM_MODELS = [
    function(model) {
      // UI thread only
      var uiThread = model.getOrCreateProcess(120).getOrCreateThread(120);
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 10, duration: 80}));

      model.uiThread = uiThread;
    },

    function(model) {
      // RenderThread only
      var renderThread = model.getOrCreateProcess(120).getOrCreateThread(200);
      renderThread.name = 'RenderThread';
      renderThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'doFrame', start: 10, duration: 80}));

      model.renderThread = renderThread;
    },

    function(model) {
      var uiThread = model.getOrCreateProcess(120).getOrCreateThread(120);

      // UI thread time - 19 (from 10 to 29)
      uiThread.asyncSliceGroup.push(
        newAsyncSliceNamed('deliverInputEvent', 10, 9, uiThread, uiThread));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 20, duration: 10}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'draw', start: 20, duration: 8}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'Record View#draw()', start: 20, duration: 8}));

      // RenderThread time - 61 (from 29 to 90)
      var renderThread = model.getOrCreateProcess(120).getOrCreateThread(200);
      renderThread.name = 'RenderThread';
      renderThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'DrawFrame', start: 29, duration: 61}));
      renderThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'syncFrameState', start: 29, duration: 1}));

      model.uiThread = uiThread;
      model.renderThread = renderThread;
    }
  ];

  test('getThreads', function() {
    SINGLE_FRAME_CUSTOM_MODELS.forEach(function(customizeModelCallback) {
      var model = tr.c.TestUtils.newModel(customizeModelCallback);
      var helper = model.getOrCreateHelper(AndroidModelHelper);
      assert.equal(helper.apps[0].uiThread, model.uiThread);
      assert.equal(helper.apps[0].renderThread, model.renderThread);
    });
  });

  test('iterateImportantSlices', function() {
    SINGLE_FRAME_CUSTOM_MODELS.forEach(function(customizeModelCallback) {
      var model = tr.c.TestUtils.newModel(customizeModelCallback);
      var helper = model.getOrCreateHelper(AndroidModelHelper);

      var seen = 0;
      helper.iterateImportantSlices(function(importantSlice) {
        assert.isTrue(importantSlice instanceof tr.model.Slice);
        seen++;
      });
      assert.equal(seen, 1);
    });
  });

  test('getFrames', function() {
    SINGLE_FRAME_CUSTOM_MODELS.forEach(function(customizeModelCallback) {
      var model = tr.c.TestUtils.newModel(customizeModelCallback);
      var helper = model.getOrCreateHelper(AndroidModelHelper);
      assert.equal(helper.apps.length, 1);

      var frames = helper.apps[0].getFrames();
      assert.equal(frames.length, 1);
      assert.closeTo(frames[0].totalDuration, 80, 1e-5);

      assert.closeTo(frames[0].start, 10, 1e-5);
      assert.closeTo(frames[0].end, 90, 1e-5);
    });
  });

  test('surfaceFlingerVsyncs', function() {
    var model = tr.c.TestUtils.newModel(createSurfaceFlingerWithVsyncs);
    var helper = model.getOrCreateHelper(AndroidModelHelper);
    assert.isTrue(helper.surfaceFlinger.hasVsyncs);

    // test querying the vsyncs
    assert.closeTo(helper.surfaceFlinger.getFrameKickoff(5), 0, 1e-5);
    assert.closeTo(helper.surfaceFlinger.getFrameDeadline(95), 100, 1e-5);

    assert.closeTo(helper.surfaceFlinger.getFrameKickoff(10), 10, 1e-5);
    assert.closeTo(helper.surfaceFlinger.getFrameDeadline(90), 100, 1e-5);

    // test undefined behavior outside of vsyncs.
    assert.isUndefined(helper.surfaceFlinger.getFrameKickoff(-5));
    assert.isUndefined(helper.surfaceFlinger.getFrameDeadline(105));
  });

  test('frameVsyncInterop', function() {
    var model = tr.c.TestUtils.newModel(function(model) {
      // app - 3 good, 3 bad frames
      var uiThread = model.getOrCreateProcess(1).getOrCreateThread(1);
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 1, duration: 8}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 10, duration: 8}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 20, duration: 8}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 31, duration: 11}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 45, duration: 6}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 60, duration: 20}));

      // surface flinger - vsync every 10ms
      createSurfaceFlingerWithVsyncs(model);
    });
    var helper = model.getOrCreateHelper(AndroidModelHelper);

    var frames = helper.apps[0].getFrames();
    assert.equal(frames.length, 6);
    for (var i = 0; i < 6; i++) {
      var shouldMissDeadline = i >= 3;
      var missedDeadline = frames[i].args['deadline'] < frames[i].end;
      assert.equal(shouldMissDeadline, missedDeadline);
    }
  });

  test('appInputs', function() {
    var model = tr.c.TestUtils.newModel(function(model) {
      var process = model.getOrCreateProcess(120);
      var uiThread = process.getOrCreateThread(120);
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 20, duration: 4}));
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 40, duration: 4}));

      var counter = process.getOrCreateCounter('android', 'aq:pending:foo');
      var series = newCounterSeries();
      series.addCounterSample(10, 1);
      series.addCounterSample(20, 0);
      series.addCounterSample(30, 1);
      series.addCounterSample(40, 2);
      series.addCounterSample(50, 0);
      counter.addSeries(series);
    });
    var helper = model.getOrCreateHelper(AndroidModelHelper);
    assert.equal(helper.apps.length, 1);

    var inputSamples = helper.apps[0].getInputSamples();
    assert.equal(inputSamples.length, 3);
    assert.equal(inputSamples[0].timestamp, 10);
    assert.equal(inputSamples[1].timestamp, 30);
    assert.equal(inputSamples[2].timestamp, 40);
  });

  test('appAnimations', function() {
    var model = tr.c.TestUtils.newModel(function(model) {
      var process = model.getOrCreateProcess(120);
      var uiThread = process.getOrCreateThread(120);
      uiThread.sliceGroup.pushSlice(newSliceEx(
          {title: 'performTraversals', start: 10, duration: 10}));
      uiThread.asyncSliceGroup.push(newAsyncSliceNamed('animator:foo', 0, 10,
                                                       uiThread, uiThread));
    });
    var helper = model.getOrCreateHelper(AndroidModelHelper);
    assert.equal(helper.apps.length, 1);

    var animations = helper.apps[0].getAnimationAsyncSlices();
    assert.equal(animations.length, 1);
    assert.equal(animations[0].start, 0);
    assert.equal(animations[0].end, 10);
  });
});
</script>