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.trace_model'); 9base.require('tracing.importer'); 10 11base.unittest.testSuite('tracing.trace_model', function() { 12 var ThreadSlice = tracing.trace_model.ThreadSlice; 13 var TraceModel = tracing.TraceModel; 14 var TitleFilter = tracing.TitleFilter; 15 16 var createTraceModelWithOneOfEverything = function() { 17 var m = new TraceModel(); 18 var cpu = m.kernel.getOrCreateCpu(1); 19 cpu.slices.push(tracing.test_utils.newSlice(1, 3)); 20 21 var p = m.getOrCreateProcess(1); 22 var t = p.getOrCreateThread(1); 23 t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 4)); 24 t.asyncSliceGroup.push(tracing.test_utils.newAsyncSlice(0, 1, t, t)); 25 26 var c = p.getOrCreateCounter('', 'ProcessCounter'); 27 var aSeries = new tracing.trace_model.CounterSeries('a', 0); 28 var bSeries = new tracing.trace_model.CounterSeries('b', 0); 29 c.addSeries(aSeries); 30 c.addSeries(bSeries); 31 32 aSeries.addSample(0, 5); 33 aSeries.addSample(1, 6); 34 aSeries.addSample(2, 5); 35 aSeries.addSample(3, 7); 36 37 bSeries.addSample(0, 10); 38 bSeries.addSample(1, 15); 39 bSeries.addSample(2, 12); 40 bSeries.addSample(3, 16); 41 42 var c1 = cpu.getOrCreateCounter('', 'CpuCounter'); 43 var aSeries = new tracing.trace_model.CounterSeries('a', 0); 44 var bSeries = new tracing.trace_model.CounterSeries('b', 0); 45 c1.addSeries(aSeries); 46 c1.addSeries(bSeries); 47 48 aSeries.addSample(0, 5); 49 aSeries.addSample(1, 6); 50 aSeries.addSample(2, 5); 51 aSeries.addSample(3, 7); 52 53 bSeries.addSample(0, 10); 54 bSeries.addSample(1, 15); 55 bSeries.addSample(2, 12); 56 bSeries.addSample(3, 16); 57 58 m.updateBounds(); 59 60 return m; 61 }; 62 63 test('traceModelBounds_EmptyTraceModel', function() { 64 var m = new TraceModel(); 65 m.updateBounds(); 66 assertEquals(undefined, m.bounds.min); 67 assertEquals(undefined, m.bounds.max); 68 }); 69 70 test('traceModelBounds_OneEmptyThread', function() { 71 var m = new TraceModel(); 72 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 73 m.updateBounds(); 74 assertEquals(undefined, m.bounds.min); 75 assertEquals(undefined, m.bounds.max); 76 }); 77 78 test('traceModelBounds_OneThread', function() { 79 var m = new TraceModel(); 80 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 81 t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 82 m.updateBounds(); 83 assertEquals(1, m.bounds.min); 84 assertEquals(4, m.bounds.max); 85 }); 86 87 test('traceModelBounds_OneThreadAndOneEmptyThread', function() { 88 var m = new TraceModel(); 89 var t1 = m.getOrCreateProcess(1).getOrCreateThread(1); 90 t1.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 91 var t2 = m.getOrCreateProcess(1).getOrCreateThread(1); 92 m.updateBounds(); 93 assertEquals(1, m.bounds.min); 94 assertEquals(4, m.bounds.max); 95 }); 96 97 test('traceModelBounds_OneCpu', function() { 98 var m = new TraceModel(); 99 var cpu = m.kernel.getOrCreateCpu(1); 100 cpu.slices.push(tracing.test_utils.newSlice(1, 3)); 101 m.updateBounds(); 102 assertEquals(1, m.bounds.min); 103 assertEquals(4, m.bounds.max); 104 }); 105 106 test('traceModelBounds_OneCpuOneThread', function() { 107 var m = new TraceModel(); 108 var cpu = m.kernel.getOrCreateCpu(1); 109 cpu.slices.push(tracing.test_utils.newSlice(1, 3)); 110 111 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 112 t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 4)); 113 114 m.updateBounds(); 115 assertEquals(1, m.bounds.min); 116 assertEquals(5, m.bounds.max); 117 }); 118 119 test('traceModelCanImportEmpty', function() { 120 var m; 121 m = new TraceModel([]); 122 m = new TraceModel(''); 123 }); 124 125 test('traceModelCanImportSubtraces', function() { 126 var systraceLines = [ 127 'SurfaceFlinger-2 [001] ...1 1000.0: 0: B|1|taskA', 128 'SurfaceFlinger-2 [001] ...1 2000.0: 0: E' 129 ]; 130 var traceEvents = [ 131 {ts: 1000, pid: 1, tid: 3, ph: 'B', cat: 'c', name: 'taskB', args: { 132 my_object: {id_ref: '0x1000'} 133 }}, 134 {ts: 2000, pid: 1, tid: 3, ph: 'E', cat: 'c', name: 'taskB', args: {}} 135 ]; 136 137 var combined = JSON.stringify({ 138 traceEvents: traceEvents, 139 systemTraceEvents: systraceLines.join('\n') 140 }); 141 142 var m = new TraceModel(); 143 m.importTraces([combined]); 144 assertEquals(1, base.dictionaryValues(m.processes).length); 145 146 var p1 = m.processes[1]; 147 assertNotUndefined(p1); 148 149 var t2 = p1.threads[2]; 150 var t3 = p1.threads[3]; 151 assertNotUndefined(t2); 152 assertNotUndefined(t3); 153 154 assertEquals(1, t2.sliceGroup.length, 1); 155 assertEquals('taskA', t2.sliceGroup.slices[0].title); 156 157 assertEquals(1, t3.sliceGroup.length); 158 assertEquals('taskB', t3.sliceGroup.slices[0].title); 159 }); 160 161 test('traceModelWithImportFailure', function() { 162 var malformed = '{traceEvents: [{garbage'; 163 var m = new TraceModel(); 164 assertThrows(function() { 165 m.importTraces([malformed]); 166 }); 167 }); 168 169 test('titleFilter', function() { 170 var s0 = tracing.test_utils.newSlice(1, 3); 171 assertFalse(new TitleFilter('').matchSlice(s0)); 172 173 assertTrue(new TitleFilter('a').matchSlice(s0)); 174 assertFalse(new TitleFilter('x').matchSlice(s0)); 175 176 var s1 = tracing.test_utils.newSliceNamed('ba', 1, 3); 177 assertTrue(new TitleFilter('a').matchSlice(s1)); 178 assertTrue(new TitleFilter('ba').matchSlice(s1)); 179 assertFalse(new TitleFilter('x').matchSlice(s1)); 180 }); 181 182 test('traceModel_toJSON', function() { 183 var m = createTraceModelWithOneOfEverything(); 184 assertNotNull(JSON.stringify(m)); 185 }); 186 187 test('traceModel_findAllThreadsNamed', function() { 188 var m = new TraceModel(); 189 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 190 t.name = 'CrBrowserMain'; 191 192 m.updateBounds(); 193 var f = m.findAllThreadsNamed('CrBrowserMain'); 194 assertArrayEquals([t], f); 195 f = m.findAllThreadsNamed('NoSuchThread'); 196 assertEquals(0, f.length); 197 }); 198 199 test('traceModel_updateCategories', function() { 200 var m = new TraceModel(); 201 var t = m.getOrCreateProcess(1).getOrCreateThread(1); 202 t.sliceGroup.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 203 t.sliceGroup.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 204 t.sliceGroup.pushSlice(new ThreadSlice('categoryB', 'a', 0, 1, {}, 3)); 205 t.sliceGroup.pushSlice(new ThreadSlice('categoryA', 'a', 0, 1, {}, 3)); 206 t.sliceGroup.pushSlice(new ThreadSlice('', 'a', 0, 1, {}, 3)); 207 m.updateCategories_(); 208 assertArrayEquals(['categoryA', 'categoryB'], m.categories); 209 }); 210}); 211