| <!DOCTYPE html> |
| <!-- |
| Copyright (c) 2012 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/importer/import.html"> |
| <link rel="import" href="/tracing/model/counter.html"> |
| <link rel="import" href="/tracing/model/interaction_record.html"> |
| <link rel="import" href="/tracing/model/model.html"> |
| <link rel="import" href="/tracing/model/slice.html"> |
| <link rel="import" href="/tracing/model/slice_group.html"> |
| <link rel="import" href="/tracing/model/stack_frame.html"> |
| <link rel="import" href="/tracing/model/thread_time_slice.html"> |
| |
| <script> |
| 'use strict'; |
| |
| /** |
| * @fileoverview Helper functions for use in tracing tests. |
| */ |
| tr.exportTo('tr.c', function() { |
| var ColorScheme = tr.b.ColorScheme; |
| |
| function _getStartAndCpuDurationFromDict( |
| options, required, startFieldName, durationFieldName, endFieldName) { |
| |
| if (options[startFieldName] === undefined) { |
| if (required) |
| throw new Error('Too little information.'); |
| else |
| return {start: undefined, duration: undefined}; |
| } |
| if (options[durationFieldName] !== undefined && |
| options[endFieldName] !== undefined) { |
| throw new Error('Too much information.'); |
| } |
| if (options[durationFieldName] === undefined && |
| options[endFieldName] === undefined) { |
| if (required) |
| throw new Error('Too little information.'); |
| else |
| return {start: undefined, duration: undefined}; |
| } |
| |
| var duration; |
| if (options[durationFieldName] !== undefined) { |
| duration = options[durationFieldName]; |
| } else { |
| duration = options[endFieldName] - options[startFieldName]; |
| } |
| |
| return { |
| start: options[startFieldName], |
| duration: duration |
| }; |
| } |
| |
| function _maybeGetCpuStartAndCpuDurationFromDict(options) { |
| return _getStartAndCpuDurationFromDict( |
| options, false, 'cpuStart', 'cpuDuration', 'cpuEnd'); |
| } |
| |
| function TestUtils() { |
| } |
| |
| TestUtils.getStartAndDurationFromDict = function(options) { |
| return _getStartAndCpuDurationFromDict( |
| options, true, 'start', 'duration', 'end'); |
| } |
| |
| TestUtils.newAsyncSlice = function(start, duration, startThread, endThread) { |
| return TestUtils.newAsyncSliceNamed( |
| 'a', start, duration, startThread, endThread); |
| } |
| |
| TestUtils.newAsyncSliceNamed = function( |
| name, start, duration, startThread, endThread) { |
| var asyncSliceConstructor = |
| tr.model.AsyncSlice.getConstructor('', name); |
| |
| var s = new asyncSliceConstructor('', name, 0, start); |
| s.duration = duration; |
| s.startThread = startThread; |
| s.endThread = endThread; |
| return s; |
| } |
| |
| TestUtils.newAsyncSliceEx = function(options) { |
| var sd = TestUtils.getStartAndDurationFromDict(options); |
| |
| var cat = options.cat ? options.cat : 'cat'; |
| var title = options.title ? options.title : 'a'; |
| |
| var colorId; |
| if (options.colorId) { |
| if (options.colorId === 'random') { |
| colorId = Math.floor( |
| Math.random() * |
| ColorScheme.proprties.numGeneralPurposeColorIds); |
| } else { |
| colorId = options.colorId; |
| } |
| } else { |
| colorId = 0; |
| } |
| |
| |
| var isTopLevel; |
| if (options.isTopLevel !== undefined) |
| isTopLevel = options.isTopLevel; |
| else |
| isTopLevel = false; |
| |
| var asyncSliceConstructor = |
| tr.model.AsyncSlice.getConstructor(cat, title); |
| |
| var slice = new asyncSliceConstructor( |
| cat, |
| title, |
| colorId, |
| sd.start, |
| options.args ? options.args : {}, |
| sd.duration, isTopLevel); |
| |
| if (options.id) |
| slice.id = options.id; |
| else |
| slice.id = tr.b.GUID.allocate(); |
| |
| if (options.startStackFrame) |
| slice.startStackFrame = options.startStackFrame; |
| if (options.endStackFrame) |
| slice.endStackFrame = options.endStackFrame; |
| if (options.important) |
| slice.important = options.important; |
| if (options.startThread) |
| slice.startThread = options.startThread; |
| if (options.endThread) |
| slice.endThread = options.endThread; |
| return slice; |
| } |
| |
| TestUtils.newCounter = function(parent) { |
| return TestUtils.newCounterNamed(parent, 'a'); |
| } |
| |
| TestUtils.newCounterNamed = function(parent, name) { |
| var s = new tr.model.Counter(parent, name, null, name); |
| return s; |
| } |
| |
| TestUtils.newCounterCategory = function(parent, category, name) { |
| var s = new tr.model.Counter(parent, name, category, name); |
| return s; |
| } |
| |
| TestUtils.newCounterSeries = function() { |
| var s = new tr.model.CounterSeries('a', 0); |
| return s; |
| } |
| |
| TestUtils.newFlowEventEx = function(options) { |
| if (options.start === undefined) |
| throw new Error('Too little info'); |
| |
| var title = options.title ? options.title : 'a'; |
| |
| var colorId = options.colorId ? options.colorId : 0; |
| |
| var sd = TestUtils.getStartAndDurationFromDict(options); |
| |
| var id; |
| if (options.id !== undefined) |
| id = options.id; |
| else |
| id = tr.b.GUID.allocate(); |
| |
| var event = new tr.model.FlowEvent( |
| options.cat ? options.cat : 'cat', |
| id, |
| title, |
| colorId, |
| sd.start, |
| options.args ? options.args : {}, |
| sd.duration); |
| |
| if (options.startStackFrame) |
| event.startStackFrame = options.startStackFrame; |
| if (options.endStackFrame) |
| event.endStackFrame = options.endStackFrame; |
| if (options.important) |
| event.important = options.important; |
| if (options.startSlice) { |
| event.startSlice = options.startSlice; |
| event.startSlice.outFlowEvents.push(event); |
| } |
| if (options.endSlice) { |
| event.endSlice = options.endSlice; |
| event.endSlice.inFlowEvents.push(event); |
| } |
| return event; |
| } |
| |
| TestUtils.newSlice = function(start, duration) { |
| return TestUtils.newSliceNamed('a', start, duration); |
| } |
| |
| TestUtils.newSliceNamed = function(name, start, duration) { |
| var s = new tr.model.Slice('', name, 0, start, {}, duration); |
| return s; |
| } |
| |
| TestUtils.newThreadSlice = function(thread, state, start, duration, opt_cpu) { |
| var s = new tr.model.ThreadTimeSlice( |
| thread, state, 'cat', start, {}, duration); |
| if (opt_cpu) |
| s.cpuOnWhichThreadWasRunning = opt_cpu; |
| return s; |
| } |
| |
| TestUtils.newSampleNamed = function( |
| thread, sampleName, category, frameNames, start) { |
| var model; |
| if (thread.parent) |
| model = thread.parent.model; |
| else |
| model = undefined; |
| var sf = TestUtils.newStackTrace(model, frameNames); |
| var s = new tr.model.Sample(undefined, thread, |
| sampleName, start, |
| sf, |
| 1); |
| return s; |
| } |
| |
| TestUtils.newSliceCategory = function(category, name, start, duration) { |
| var s = new tr.model.Slice( |
| category, name, 0, start, {}, duration); |
| return s; |
| } |
| |
| TestUtils.newSliceEx = function(options) { |
| var sd = TestUtils.getStartAndDurationFromDict(options); |
| |
| var title = options.title ? options.title : 'a'; |
| |
| var colorId = options.colorId ? options.colorId : 0; |
| |
| var cpuSD = _maybeGetCpuStartAndCpuDurationFromDict(options); |
| |
| var type; |
| if (options.type) |
| type = options.type; |
| else |
| type = tr.model.Slice; |
| |
| var slice = new type( |
| options.cat ? options.cat : 'cat', |
| title, |
| colorId, |
| sd.start, |
| options.args ? options.args : {}, |
| sd.duration, |
| cpuSD.start, cpuSD.duration); |
| |
| |
| return slice; |
| } |
| |
| TestUtils.newStackTrace = function(model, titles) { |
| var frame = undefined; |
| titles.forEach(function(title) { |
| frame = new tr.model.StackFrame(frame, tr.b.GUID.allocate(), title, 7); |
| if (model) |
| model.addStackFrame(frame); |
| }); |
| return frame; |
| } |
| |
| TestUtils.findSliceNamed = function(slices, name) { |
| if (slices instanceof tr.model.SliceGroup) |
| slices = slices.slices; |
| for (var i = 0; i < slices.length; i++) |
| if (slices[i].title == name) |
| return slices[i]; |
| return undefined; |
| } |
| |
| TestUtils.newInteractionRecord = function(parentModel, start, duration) { |
| return new tr.model.InteractionRecord(parentModel, 'a', 0, start, duration); |
| } |
| |
| TestUtils.newModel = function(customizeModelCallback) { |
| return TestUtils.newModelWithEvents([], { |
| shiftWorldToZero: false, |
| pruneEmptyContainers: false, |
| customizeModelCallback: customizeModelCallback |
| }); |
| } |
| |
| TestUtils.newModelWithEvents = function(events, opts) { |
| if (!(events instanceof Array)) |
| events = [events]; |
| |
| opts = opts || {}; |
| |
| var io = new tr.importer.ImportOptions(); |
| io.showImportWarnings = false; |
| io.customizeModelCallback = opts.customizeModelCallback; |
| io.shiftWorldToZero = opts.shiftWorldToZero === undefined ? |
| true : opts.shiftWorldToZero; |
| io.pruneEmptyContainers = opts.pruneEmptyContainers === undefined ? |
| true : opts.pruneEmptyContainers; |
| io.auditorConstructors = opts.auditorConstructors === undefined ? |
| [] : opts.auditorConstructors; |
| |
| var m = new tr.Model(); |
| var i = new tr.importer.Import(m, io); |
| i.importTraces(events); |
| return m; |
| } |
| |
| TestUtils.newModelWithAuditor = function(customizeModelCallback, auditor) { |
| return TestUtils.newModelWithEvents([], { |
| shiftWorldToZero: false, |
| pruneEmptyContainers: false, |
| customizeModelCallback: customizeModelCallback, |
| auditorConstructors: [auditor] |
| }); |
| } |
| |
| TestUtils.newFakeThread = function() { |
| var process = {model: {}}; |
| return new tr.model.Thread(process); |
| } |
| |
| return { |
| TestUtils: TestUtils |
| }; |
| }); |
| </script> |