Files
SpeedScope/test-utils.ts
Jamie Wong 2207ff54ce Add tests for importers (#56)
This also fixes deep copy import for Instruments when copying from Instruments 7

Related to #14
2018-05-29 23:53:03 -07:00

42 lines
850 B
TypeScript

import {Profile, CallTreeNode, Frame} from './profile'
interface DumpedProfile {
stacks: string[]
frames: Frame[]
}
export function dumpProfile(profile: Profile): any {
const dump: DumpedProfile = {
stacks: [],
frames: [],
}
profile.forEachFrame(f => dump.frames.push(f))
let lastValue = 0
const curStack: (number | string)[] = []
function maybeEmit(value: number) {
if (lastValue != value) {
dump.stacks.push(
curStack.map(k => `${k}`).join(';') + ` ${profile.formatValue(value - lastValue)}`,
)
lastValue = value
}
}
function openFrame(node: CallTreeNode, value: number) {
maybeEmit(value)
curStack.push(node.frame.name)
}
function closeFrame(value: number) {
maybeEmit(value)
curStack.pop()
}
profile.forEachCall(openFrame, closeFrame)
return dump
}