thread_test.html revision edfe2194ee8a857cc1e78b4e4020f9b5e7210029
1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/range.html">
9<link rel="import" href="/tracing/core/test_utils.html">
10<link rel="import" href="/tracing/model/model.html">
11
12<script>
13'use strict';
14
15tr.b.unittest.testSuite(function() {
16  var ThreadSlice = tr.model.ThreadSlice;
17  var Process = tr.model.Process;
18  var Thread = tr.model.Thread;
19  var newSliceEx = tr.c.test_utils.newSliceEx;
20  var newAsyncSlice = tr.c.test_utils.newAsyncSlice;
21  var newThreadSlice = tr.c.test_utils.newThreadSlice;
22  var SCHEDULING_STATE = tr.model.SCHEDULING_STATE;
23
24  test('threadBounds_Empty', function() {
25    var model = new tr.Model();
26    var t = new Thread(new Process(model, 7), 1);
27    t.updateBounds();
28    assert.isUndefined(t.bounds.min);
29    assert.isUndefined(t.bounds.max);
30  });
31
32  test('threadBounds_SubRow', function() {
33    var model = new tr.Model();
34    var t = new Thread(new Process(model, 7), 1);
35    t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3));
36    t.updateBounds();
37    assert.equal(t.bounds.min, 1);
38    assert.equal(t.bounds.max, 4);
39  });
40
41  test('threadBounds_AsyncSliceGroup', function() {
42    var model = new tr.Model();
43    var t = new Thread(new Process(model, 7), 1);
44    t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3));
45    t.asyncSliceGroup.push(newAsyncSlice(0.1, 5, t, t));
46    t.updateBounds();
47    assert.equal(t.bounds.min, 0.1);
48    assert.equal(t.bounds.max, 5.1);
49  });
50
51  test('threadBounds_Cpu', function() {
52    var model = new tr.Model();
53    var t = new Thread(new Process(model, 7), 1);
54    t.timeSlices = [newSliceEx({title: 'x', start: 0, duration: 1})];
55    t.updateBounds();
56    assert.equal(t.bounds.min, 0);
57    assert.equal(t.bounds.max, 1);
58  });
59
60  test('shiftTimestampsForwardWithCpu', function() {
61    var model = new tr.Model();
62    var t = new Thread(new Process(model, 7), 1);
63    t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 0, {}, 3));
64    t.asyncSliceGroup.push(newAsyncSlice(0, 5, t, t));
65    t.timeSlices = [newSliceEx({title: 'x', start: 0, duration: 1})];
66
67    var shiftCount = 0;
68    t.asyncSliceGroup.shiftTimestampsForward = function(ts) {
69      if (ts == 0.32)
70        shiftCount++;
71    };
72
73    t.shiftTimestampsForward(0.32);
74
75    assert.equal(shiftCount, 1);
76    assert.equal(t.sliceGroup.slices[0].start, 0.32);
77    assert.equal(t.timeSlices[0].start, 0.32);
78  });
79
80  test('shiftTimestampsForwardWithoutCpu', function() {
81    var model = new tr.Model();
82    var t = new Thread(new Process(model, 7), 1);
83    t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 0, {}, 3));
84    t.asyncSliceGroup.push(newAsyncSlice(0, 5, t, t));
85
86    var shiftCount = 0;
87    t.asyncSliceGroup.shiftTimestampsForward = function(ts) {
88      if (ts == 0.32)
89        shiftCount++;
90    };
91
92    t.shiftTimestampsForward(0.32);
93
94    assert.equal(shiftCount, 1);
95    assert.equal(t.sliceGroup.slices[0].start, 0.32);
96  });
97
98  test('getSchedulingStatsForRange', function() {
99    var scheduledThread = undefined;
100    var unscheduledThread = undefined;
101    var model = tr.c.test_utils.newModel(function(model) {
102      unscheduledThread = model.getOrCreateProcess(1).getOrCreateThread(1);
103      unscheduledThread.sliceGroup.pushSlice(newSliceEx(
104          {title: 'work', start: 0, duration: 20}));
105
106      scheduledThread = model.getOrCreateProcess(2).getOrCreateThread(2);
107      scheduledThread.sliceGroup.pushSlice(newSliceEx(
108          {title: 'work', start: 0, duration: 20}));
109      scheduledThread.timeSlices = [
110          newThreadSlice(scheduledThread, SCHEDULING_STATE.RUNNING, 0, 3),
111          newThreadSlice(scheduledThread, SCHEDULING_STATE.RUNNABLE, 3, 5),
112          newThreadSlice(scheduledThread, SCHEDULING_STATE.RUNNING, 8, 2),
113          newThreadSlice(scheduledThread, SCHEDULING_STATE.SLEEPING, 10, 10)
114      ];
115    });
116
117    // thread without scheduling states
118    var stats = unscheduledThread.getSchedulingStatsForRange(0, 20);
119    assert.deepEqual(stats, {});
120
121    // no scheduling info
122    var stats = scheduledThread.getSchedulingStatsForRange(50, 100);
123    assert.deepEqual(stats, {});
124
125    // simple query
126    var stats = scheduledThread.getSchedulingStatsForRange(0, 3);
127    var expected = {};
128    expected[SCHEDULING_STATE.RUNNING] = 3;
129    assert.deepEqual(stats, expected);
130
131    // aggregation
132    var stats = scheduledThread.getSchedulingStatsForRange(0, 20);
133    var expected = {};
134    expected[SCHEDULING_STATE.RUNNING] = 5;
135    expected[SCHEDULING_STATE.RUNNABLE] = 5;
136    expected[SCHEDULING_STATE.SLEEPING] = 10;
137    assert.deepEqual(stats, expected);
138  });
139
140  test('getCpuStatsForRange', function() {
141    var model = tr.c.test_utils.newModel(function(model) {
142      var cpu0 = model.kernel.getOrCreateCpu(0);
143      var cpu1 = model.kernel.getOrCreateCpu(1);
144      var thread = model.getOrCreateProcess(1).getOrCreateThread(1);
145      thread.timeSlices = [
146          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 0, 3, cpu0),
147          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 8, 2, cpu1),
148          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 20, 5, cpu1)
149      ];
150      var range = tr.b.Range.fromExplicitRange(1, 22);
151      var stats = thread.getCpuStatsForRange(range);
152      assert.deepEqual(stats, {
153        0: 2,
154        1: 4,
155        total: 6
156      });
157    });
158  });
159
160  test('getCpuStatsForRange_excludesNotRunningThreads', function() {
161    var model = tr.c.test_utils.newModel(function(model) {
162      var cpu0 = model.kernel.getOrCreateCpu(0);
163      var cpu1 = model.kernel.getOrCreateCpu(1);
164      var thread = model.getOrCreateProcess(1).getOrCreateThread(1);
165      thread.timeSlices = [
166          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 0, 8, cpu0),
167          newThreadSlice(thread, SCHEDULING_STATE.RUNNABLE, 8, 3),
168          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 11, 4, cpu1),
169          newThreadSlice(thread, SCHEDULING_STATE.SLEEPING, 15, 10),
170          newThreadSlice(thread, SCHEDULING_STATE.RUNNING, 25, 10, cpu1)
171      ];
172      var range = tr.b.Range.fromExplicitRange(1, 26);
173      var stats = thread.getCpuStatsForRange(range);
174      assert.deepEqual(stats, {
175        0: 7,
176        1: 5,
177        total: 12
178      });
179    });
180  });
181});
182</script>
183
184