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.analysis.analyze_counters');
11base.require('tracing.selection');
12base.require('tracing.trace_model.counter');
13base.require('tracing.trace_model.counter_series');
14
15base.unittest.testSuite('tracing.analysis.analyze_counters', function() {
16  var Counter = tracing.trace_model.Counter;
17  var CounterSeries = tracing.trace_model.CounterSeries;
18
19  var Selection = tracing.Selection;
20  var AnalysisView = tracing.analysis.AnalysisView;
21  var StubAnalysisResults = tracing.analysis.StubAnalysisResults;
22
23  var createSelectionWithCounters = function(numSamples) {
24    if (numSamples > 2 || numSamples < 1)
25      throw new Error('This function only supports 1 or 2 samples');
26
27    var ctr = new Counter(null, 0, '', 'ctr');
28    var series = new CounterSeries('value', 0);
29    ctr.addSeries(series);
30
31    series.addSample(0, 0);
32    series.addSample(10, 10);
33
34    var selection = new Selection();
35    var t1track = {};
36    selection.addCounterSample(t1track, ctr, 1);
37
38    if (numSamples === 1)
39      return selection;
40
41    selection.addCounterSample(t1track, ctr, 0);
42    return selection;
43  };
44
45  function createSeries(ctr) {
46    var allocatedSeries = new CounterSeries('bytesallocated', 0);
47    var freeSeries = new CounterSeries('bytesfree', 1);
48
49    ctr.addSeries(allocatedSeries);
50    ctr.addSeries(freeSeries);
51
52    allocatedSeries.addSample(0, 0);
53    allocatedSeries.addSample(10, 25);
54    allocatedSeries.addSample(20, 10);
55
56    freeSeries.addSample(0, 15);
57    freeSeries.addSample(10, 20);
58    freeSeries.addSample(20, 5);
59  }
60
61  var createSelectionWithTwoSeriesSingleCounter = function() {
62    var ctr = new Counter(null, 0, 'foo', 'ctr[0]');
63    createSeries(ctr);
64
65    var selection = new Selection();
66    var t1track = {};
67
68    selection.addCounterSample(t1track, ctr, 1);
69    return selection;
70  };
71
72  var createSelectionWithTwoSeriesTwoCounters = function() {
73    var ctr1 = new Counter(null, 0, '', 'ctr1');
74    createSeries(ctr1);
75
76    var ctr2 = new Counter(null, 0, '', 'ctr2');
77    createSeries(ctr2);
78
79    var selection = new Selection();
80    var t1track = {};
81    selection.addCounterSample(t1track, ctr1, 1);
82    selection.addCounterSample(t1track, ctr2, 2);
83    return selection;
84  };
85
86  var createSelectionWithTwoCountersDiffSeriesDiffHits = function() {
87    var ctr1 = new Counter(null, 0, '', 'a');
88    var allocatedSeries = new CounterSeries('bytesallocated', 0);
89    ctr1.addSeries(allocatedSeries);
90
91    allocatedSeries.addSample(0, 0);
92    allocatedSeries.addSample(10, 25);
93    allocatedSeries.addSample(20, 15);
94
95    assertEquals('a', ctr1.name);
96    assertEquals(3, ctr1.numSamples);
97    assertEquals(1, ctr1.numSeries);
98
99    var ctr2 = new Counter(null, 0, '', 'b');
100    var allocatedSeries = new CounterSeries('bytesallocated', 0);
101    var freeSeries = new CounterSeries('bytesfree', 1);
102
103    ctr2.addSeries(allocatedSeries);
104    ctr2.addSeries(freeSeries);
105
106    allocatedSeries.addSample(0, 0);
107    allocatedSeries.addSample(10, 25);
108    allocatedSeries.addSample(20, 10);
109    allocatedSeries.addSample(30, 15);
110
111    freeSeries.addSample(0, 20);
112    freeSeries.addSample(10, 5);
113    freeSeries.addSample(20, 25);
114    freeSeries.addSample(30, 0);
115
116    assertEquals('b', ctr2.name);
117    assertEquals(4, ctr2.numSamples);
118    assertEquals(2, ctr2.numSeries);
119
120    var selection = new Selection();
121    var t1track = {};
122    selection.addCounterSample(t1track, ctr1, 1);
123    selection.addCounterSample(t1track, ctr2, 2);
124
125    return selection;
126  };
127
128  test('instantiate_singleCounterWithTwoSeries', function() {
129    var selection = createSelectionWithTwoSeriesSingleCounter();
130
131    var analysisEl = new AnalysisView();
132    analysisEl.selection = selection;
133    this.addHTMLOutput(analysisEl);
134  });
135
136  test('instantiate_twoCountersWithTwoSeries', function() {
137    var selection = createSelectionWithTwoSeriesTwoCounters();
138
139    var analysisEl = new AnalysisView();
140    analysisEl.selection = selection;
141    this.addHTMLOutput(analysisEl);
142  });
143
144  test('analyzeSelectionWithSingleCounter', function() {
145    var selection = createSelectionWithCounters(1);
146
147    var results = new StubAnalysisResults();
148    tracing.analysis.analyzeSelection(results, selection);
149
150    assertEquals(1, results.tables.length);
151    var table = results.tables[0];
152    assertEquals('Selected counter:', table.tableHeader);
153    assertEquals(3, table.rows.length);
154
155    assertEquals('Title', table.rows[0].label);
156    assertEquals('Timestamp', table.rows[1].label);
157    assertEquals('value', table.rows[2].label);
158    assertEquals(10, table.rows[2].text);
159  });
160
161  test('analyzeSelectionWithBasicTwoSeriesTwoCounters', function() {
162    var selection = createSelectionWithTwoSeriesTwoCounters();
163
164    var results = new StubAnalysisResults();
165    tracing.analysis.analyzeSelection(results, selection);
166    assertEquals(1, results.tables.length);
167    var table = results.tables[0];
168    assertEquals('Counters:', table.tableHeader);
169    assertEquals(4, table.rows.length);
170
171    assertEquals('ctr1: series(bytesallocated)', table.rows[0].label);
172    assertEquals('ctr1: series(bytesfree)', table.rows[1].label);
173    assertEquals('ctr2: series(bytesallocated)', table.rows[2].label);
174    assertEquals('ctr2: series(bytesfree)', table.rows[3].label);
175  });
176
177  test('analyzeSelectionWithComplexSeriesTwoCounters', function() {
178    var selection = createSelectionWithTwoCountersDiffSeriesDiffHits();
179
180    var results = new StubAnalysisResults();
181    tracing.analysis.analyzeSelection(results, selection);
182    assertEquals(1, results.tables.length);
183    var table = results.tables[0];
184    assertEquals('Counters:', table.tableHeader);
185    assertEquals(3, table.rows.length);
186
187    assertEquals('a: series(bytesallocated)', table.rows[0].label);
188    assertEquals('b: series(bytesallocated)', table.rows[1].label);
189    assertEquals('b: series(bytesfree)', table.rows[2].label);
190  });
191});
192