1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5'use strict';
6
7base.require('tracing.test_utils');
8base.require('tracing.analysis.analysis_view');
9base.require('tracing.analysis.stub_analysis_results');
10base.require('tracing.selection');
11base.require('tracing.trace_model');
12
13base.unittest.testSuite('tracing.analysis.analyze_slices', function() {
14  var Model = tracing.TraceModel;
15  var Thread = tracing.trace_model.Thread;
16  var Selection = tracing.Selection;
17  var AnalysisView = tracing.analysis.AnalysisView;
18  var StubAnalysisResults = tracing.analysis.StubAnalysisResults;
19  var newSliceNamed = tracing.test_utils.newSliceNamed;
20  var newSliceCategory = tracing.test_utils.newSliceCategory;
21
22  var createSelectionWithSingleSlice = function(withCategory) {
23    var model = new Model();
24    var t53 = model.getOrCreateProcess(52).getOrCreateThread(53);
25    if (withCategory)
26      t53.sliceGroup.pushSlice(newSliceCategory('foo', 'b', 0, 0.002));
27    else
28      t53.sliceGroup.pushSlice(newSliceNamed('b', 0, 0.002));
29
30    var t53track = {};
31    t53track.thread = t53;
32
33    var selection = new Selection();
34    selection.addSlice(t53track, t53.sliceGroup.slices[0]);
35    assertEquals(1, selection.length);
36
37    return selection;
38  };
39
40  var createSelectionWithTwoSlices = function() {
41    var model = new Model();
42    var t53 = model.getOrCreateProcess(52).getOrCreateThread(53);
43    t53.sliceGroup.pushSlice(newSliceNamed('a', 0.0, 0.04));
44    t53.sliceGroup.pushSlice(newSliceNamed('aa', 0.120, 0.06));
45
46    var t53track = {};
47    t53track.thread = t53;
48
49    var selection = new Selection();
50    selection.addSlice(t53track, t53.sliceGroup.slices[0]);
51    selection.addSlice(t53track, t53.sliceGroup.slices[1]);
52
53    return selection;
54  };
55
56  var createSelectionWithTwoSlicesSameTitle = function() {
57    var model = new Model();
58    var t53 = model.getOrCreateProcess(52).getOrCreateThread(53);
59    t53.sliceGroup.pushSlice(newSliceNamed('c', 0.0, 0.04));
60    t53.sliceGroup.pushSlice(newSliceNamed('c', 0.12, 0.06));
61
62    var t53track = {};
63    t53track.thread = t53;
64
65    var selection = new Selection();
66    selection.addSlice(t53track, t53.sliceGroup.slices[0]);
67    selection.addSlice(t53track, t53.sliceGroup.slices[1]);
68
69    return selection;
70  };
71
72  test('instantiate_withSingleSlice', function() {
73    var selection = createSelectionWithSingleSlice();
74
75    var analysisEl = new AnalysisView();
76    analysisEl.selection = selection;
77    this.addHTMLOutput(analysisEl);
78  });
79
80  test('instantiate_withSingleSliceCategory', function() {
81    var selection = createSelectionWithSingleSlice(true);
82
83    var analysisEl = new AnalysisView();
84    analysisEl.selection = selection;
85    this.addHTMLOutput(analysisEl);
86  });
87
88  test('instantiate_withMultipleSlices', function() {
89    var selection = createSelectionWithTwoSlices();
90
91    var analysisEl = new AnalysisView();
92    analysisEl.selection = selection;
93    this.addHTMLOutput(analysisEl);
94  });
95
96  test('instantiate_withMultipleSlicesSameTitle', function() {
97    var selection = createSelectionWithTwoSlicesSameTitle();
98
99    var analysisEl = new AnalysisView();
100    analysisEl.selection = selection;
101    this.addHTMLOutput(analysisEl);
102  });
103
104  test('analyzeSelectionWithSingleSlice', function() {
105    var selection = createSelectionWithSingleSlice();
106
107    var results = new StubAnalysisResults();
108    tracing.analysis.analyzeSelection(results, selection);
109    assertEquals(1, results.tables.length);
110    var table = results.tables[0];
111    assertEquals('Selected slice:', table.tableHeader);
112    assertEquals(3, table.rows.length);
113
114    assertEquals('b', table.rows[0].text);
115    assertEquals(0, table.rows[1].time);
116    assertAlmostEquals(0.002, table.rows[2].time);
117  });
118
119  test('analyzeSelectionWithSingleSliceCategory', function() {
120    var selection = createSelectionWithSingleSlice(true);
121
122    var results = new StubAnalysisResults();
123    tracing.analysis.analyzeSelection(results, selection);
124    assertEquals(1, results.tables.length);
125    var table = results.tables[0];
126    assertEquals('Selected slice:', table.tableHeader);
127    assertEquals(4, table.rows.length);
128
129    assertEquals('b', table.rows[0].text);
130    assertEquals('foo', table.rows[1].text);
131    assertEquals(0, table.rows[2].time);
132    assertAlmostEquals(0.002, table.rows[3].time);
133  });
134
135  test('analyzeSelectionWithTwoSlices', function() {
136    var selection = createSelectionWithTwoSlices();
137
138    var results = new StubAnalysisResults();
139    tracing.analysis.analyzeSelection(results, selection);
140    assertEquals(1, results.tables.length);
141    var table = results.tables[0];
142    assertEquals('Slices:', table.tableHeader);
143    assertEquals(6, table.rows.length);
144
145    assertEquals('a', table.rows[0].label);
146    assertEquals(1, table.rows[0].occurences);
147    assertAlmostEquals(0.04, table.rows[0].duration);
148    assertEquals('aa', table.rows[1].label);
149    assertEquals(1, table.rows[1].occurences);
150    assertAlmostEquals(0.06, table.rows[1].duration);
151    assertEquals('*Totals', table.rows[2].label);
152    assertEquals(2, table.rows[2].occurences);
153    assertAlmostEquals(0.1, table.rows[2].duration);
154
155    assertEquals('Selection start', table.rows[4].label);
156    assertAlmostEquals(0, table.rows[4].time);
157
158    assertEquals('Selection extent', table.rows[5].label);
159    assertAlmostEquals(0.18, table.rows[5].time);
160  });
161
162  test('analyzeSelectionWithTwoSlicesSameTitle', function() {
163    var selection = createSelectionWithTwoSlicesSameTitle();
164
165    var results = new StubAnalysisResults();
166    tracing.analysis.analyzeSelection(results, selection);
167    assertEquals(3, results.tables.length);
168
169    var t;
170    // Table 1.
171    t = results.tables[0];
172    assertEquals('Slices:', t.tableHeader);
173    assertObjectEquals(
174        {label: 'c',
175          duration: 0.1,
176          occurences: 2,
177          details: {min: 0.04, max: 0.06, avg: 0.05,
178            avg_stddev: 0.014142135623730947}
179        },
180        t.rows[0]);
181    assertObjectEquals({label: 'Selection start', time: 0}, t.rows[1]);
182    assertObjectEquals({label: 'Selection extent', time: 0.18}, t.rows[2]);
183
184    // Table 2.
185    var t = results.tables[1];
186    assertObjectEquals({label: 'Title', text: 'c'}, t.rows[0]);
187    assertObjectEquals({label: 'Start', time: 0}, t.rows[1]);
188    assertObjectEquals({label: 'Duration', time: 0.04}, t.rows[2]);
189
190    // Table 3.
191    var t = results.tables[2];
192    assertObjectEquals({label: 'Title', text: 'c'}, t.rows[0]);
193    assertObjectEquals({label: 'Start', time: 0.12}, t.rows[1]);
194    assertObjectEquals({label: 'Duration', time: 0.06}, t.rows[2]);
195  });
196
197  test('instantiate_withSingleSliceContainingIDRef', function() {
198    var model = new Model();
199    var p1 = model.getOrCreateProcess(1);
200    var myObjectSlice = p1.objects.addSnapshot(
201        '0x1000', 'cat', 'my_object', 0);
202
203    var t1 = p1.getOrCreateThread(1);
204    t1.sliceGroup.pushSlice(newSliceCategory('cat', 'b', 0, 2));
205    t1.sliceGroup.slices[0].args.my_object = myObjectSlice;
206
207    var t1track = {};
208    t1track.thread = t1;
209
210    var selection = new Selection();
211    selection.addSlice(t1track, t1.sliceGroup.slices[0]);
212    assertEquals(1, selection.length);
213
214    var analysisEl = new AnalysisView();
215    analysisEl.selection = selection;
216    this.addHTMLOutput(analysisEl);
217  });
218});
219