blob: 8ebde780c2d0f6cc1cc885eb2031e1352560c1d1 [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="/core/trace_model/counter.html">
<link rel="import" href="/core/trace_model/slice.html">
<link rel="import" href="/core/trace_model/slice_group.html">
<link rel="import" href="/core/trace_model/stack_frame.html">
<link rel="import" href="/core/trace_model/thread_time_slice.html">
<link rel="import" href="/core/trace_model/trace_model.html">
<script>
'use strict';
/**
* @fileoverview Helper functions for use in tracing tests.
*/
tv.exportTo('tv.c.test_utils', function() {
function newAsyncSlice(start, duration, startThread, endThread) {
return newAsyncSliceNamed('a', start, duration, startThread, endThread);
}
function newAsyncSliceNamed(name, start, duration, startThread, endThread) {
var asyncSliceConstructor =
tv.c.trace_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) {
if (options.start === undefined)
throw new Error('Too little info');
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() *
tv.b.ui.paletteProperties.numGeneralPurposeColorIds);
} else {
colorId = options.colorId;
}
} else {
colorId = 0;
}
var duration;
if (options.duration !== undefined) {
if (options.end !== undefined) throw new Error('TMI');
duration = options.duration;
} else if (options.end !== undefined) {
if (options.duration !== undefined) throw new Error('TMI');
duration = options.end - options.start;
} else {
throw new Error('Too little info');
}
var isTopLevel;
if (options.isTopLevel !== undefined)
isTopLevel = options.isTopLevel;
else
isTopLevel = false;
var asyncSliceConstructor =
tv.c.trace_model.AsyncSlice.getConstructor(cat, title);
var slice = new asyncSliceConstructor(
cat,
title,
colorId,
options.start,
options.args ? options.args : {},
duration, isTopLevel);
if (options.id)
slice.id = options.id;
else
slice.id = tv.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 tv.c.trace_model.Counter(parent, name, null, name);
return s;
}
function newCounterCategory(parent, category, name) {
var s = new tv.c.trace_model.Counter(parent, name, category, name);
return s;
}
function newCounterSeries() {
var s = new tv.c.trace_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 duration;
if (options.duration !== undefined) {
if (options.end !== undefined) throw new Error('TMI');
duration = options.duration;
} else if (options.end !== undefined) {
if (options.duration !== undefined) throw new Error('TMI');
duration = options.end - options.start;
} else {
throw new Error('Too little info');
}
var id;
if (options.id !== undefined)
id = options.id;
else
id = tv.b.GUID.allocate();
var event = new tv.c.trace_model.FlowEvent(
options.cat ? options.cat : 'cat',
id,
title,
colorId,
options.start,
options.args ? options.args : {},
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 tv.c.trace_model.Slice('', name, 0, start, {}, duration);
return s;
}
function newThreadSlice(thread, state, start, duration, opt_cpu) {
var s = new tv.c.trace_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 tv.c.trace_model.Sample(undefined, thread,
sampleName, start,
sf,
1);
return s;
}
function newSliceCategory(category, name, start, duration) {
var s = new tv.c.trace_model.Slice(
category, name, 0, start, {}, duration);
return s;
}
function newSliceEx(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 duration;
if (options.duration !== undefined) {
if (options.end !== undefined) throw new Error('TMI');
duration = options.duration;
} else if (options.end !== undefined) {
if (options.duration !== undefined) throw new Error('TMI');
duration = options.end - options.start;
} else {
throw new Error('Too little info');
}
var cpuStart = options.cpuStart;
var cpuDuration;
if (options.cpuDuration !== undefined) {
if (cpuStart !== undefined) throw new Error('Too little info');
if (options.cpuEnd !== undefined) throw new Error('TMI');
cpuDuration = options.cpuDuration;
} else if (options.cpuEnd !== undefined) {
if (cpuStart === undefined) throw new Error('Too little info');
if (options.cpuDuration !== undefined) throw new Error('TMI');
cpuDuration = options.cpuEnd - cpuStart;
}
var type;
if (options.type)
type = options.type;
else
type = tv.c.trace_model.Slice;
var slice = new type(
options.cat ? options.cat : 'cat',
title,
colorId,
options.start,
options.args ? options.args : {},
duration,
cpuStart, cpuDuration);
return slice;
}
function newStackTrace(model, category, titles) {
var frame = undefined;
for (var i = 0; i < titles.length; i++) {
frame = new tv.c.trace_model.StackFrame(frame, tv.b.GUID.allocate(),
category, titles[i], 7);
if (model)
model.addStackFrame(frame);
}
return frame;
}
function findSliceNamed(slices, name) {
if (slices instanceof tv.c.trace_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 tv.c.ImportOptions();
io.customizeModelCallback = customizeModelCallback;
io.shiftWorldToZero = false;
io.pruneEmptyContainers = false;
return new tv.c.TraceModel([], io);
}
function newModelWithAuditor(customizeModelCallback, auditor) {
var io = new tv.c.ImportOptions();
io.customizeModelCallback = customizeModelCallback;
io.shiftWorldToZero = false;
io.pruneEmptyContainers = false;
io.auditorConstructors = [auditor];
return new tv.c.TraceModel([], io);
}
return {
newAsyncSlice: newAsyncSlice,
newAsyncSliceNamed: newAsyncSliceNamed,
newAsyncSliceEx: newAsyncSliceEx,
newCounter: newCounter,
newCounterNamed: newCounterNamed,
newCounterCategory: newCounterCategory,
newCounterSeries: newCounterSeries,
newFlowEventEx: newFlowEventEx,
newSlice: newSlice,
newThreadSlice: newThreadSlice,
newSliceNamed: newSliceNamed,
newSliceEx: newSliceEx,
newSampleNamed: newSampleNamed,
newSliceCategory: newSliceCategory,
newStackTrace: newStackTrace,
newModel: newModel,
newModelWithAuditor: newModelWithAuditor,
findSliceNamed: findSliceNamed
};
});
</script>