blob: cf8fb74d62ae97595afb3c494070bc22c2ca01df [file] [log] [blame]
<!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="/model/counter.html">
<link rel="import" href="/model/model.html">
<link rel="import" href="/model/slice.html">
<link rel="import" href="/model/slice_group.html">
<link rel="import" href="/model/stack_frame.html">
<link rel="import" href="/model/thread_time_slice.html">
<script>
'use strict';
/**
* @fileoverview Helper functions for use in tracing tests.
*/
tr.exportTo('tr.c.test_utils', function() {
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 getStartAndDurationFromDict(options) {
return _getStartAndCpuDurationFromDict(
options, true, 'start', 'duration', 'end');
}
function maybeGetCpuStartAndCpuDurationFromDict(options) {
return _getStartAndCpuDurationFromDict(
options, false, 'cpuStart', 'cpuDuration', 'cpuEnd');
}
function newAsyncSlice(start, duration, startThread, endThread) {
return newAsyncSliceNamed('a', start, duration, startThread, endThread);
}
function newAsyncSliceNamed(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;
}
function newAsyncSliceEx(options) {
var sd = 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() *
tr.ui.b.paletteProperties.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;
}
function newCounter(parent) {
return newCounterNamed(parent, 'a');
}
function newCounterNamed(parent, name) {
var s = new tr.model.Counter(parent, name, null, name);
return s;
}
function newCounterCategory(parent, category, name) {
var s = new tr.model.Counter(parent, name, category, name);
return s;
}
function newCounterSeries() {
var s = new tr.model.CounterSeries('a', 0);
return s;
}
function newFlowEventEx(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 = 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;
}
function newSlice(start, duration) {
return newSliceNamed('a', start, duration);
}
function newSliceNamed(name, start, duration) {
var s = new tr.model.Slice('', name, 0, start, {}, duration);
return s;
}
function newThreadSlice(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;
}
function newSampleNamed(thread, sampleName, category, frameNames, start) {
var model;
if (thread.parent)
model = thread.parent.model;
else
model = undefined;
var sf = newStackTrace(model, category, frameNames);
var s = new tr.model.Sample(undefined, thread,
sampleName, start,
sf,
1);
return s;
}
function newSliceCategory(category, name, start, duration) {
var s = new tr.model.Slice(
category, name, 0, start, {}, duration);
return s;
}
function newSliceEx(options) {
var sd = 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;
}
function newStackTrace(model, category, titles) {
var frame = undefined;
for (var i = 0; i < titles.length; i++) {
frame = new tr.model.StackFrame(frame, tr.b.GUID.allocate(),
category, titles[i], 7);
if (model)
model.addStackFrame(frame);
}
return frame;
}
function findSliceNamed(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;
}
function newModel(customizeModelCallback) {
var io = new tr.ImportOptions();
io.customizeModelCallback = customizeModelCallback;
io.shiftWorldToZero = false;
io.pruneEmptyContainers = false;
io.auditorConstructors = [];
return new tr.Model([], io);
}
function newModelWithAuditor(customizeModelCallback, auditor) {
var io = new tr.ImportOptions();
io.customizeModelCallback = customizeModelCallback;
io.shiftWorldToZero = false;
io.pruneEmptyContainers = false;
io.auditorConstructors = [auditor];
return new tr.Model([], io);
}
function newFakeThread() {
var process = {model: {}};
return new tr.model.Thread(process);
}
return {
getStartAndDurationFromDict: getStartAndDurationFromDict,
newAsyncSlice: newAsyncSlice,
newAsyncSliceNamed: newAsyncSliceNamed,
newAsyncSliceEx: newAsyncSliceEx,
newCounter: newCounter,
newCounterNamed: newCounterNamed,
newCounterCategory: newCounterCategory,
newCounterSeries: newCounterSeries,
newFlowEventEx: newFlowEventEx,
newSlice: newSlice,
newFakeThread: newFakeThread,
newThreadSlice: newThreadSlice,
newSliceNamed: newSliceNamed,
newSliceEx: newSliceEx,
newSampleNamed: newSampleNamed,
newSliceCategory: newSliceCategory,
newStackTrace: newStackTrace,
newModel: newModel,
newModelWithAuditor: newModelWithAuditor,
findSliceNamed: findSliceNamed
};
});
</script>