speedscope/import/bg-flamegraph.ts
Alan Pierce 404aacfa46 Address review comments
* Switch to `bracketSpacing: false`.
* Add prettier-ignore in one case.
2018-04-14 16:19:26 -07:00

31 lines
876 B
TypeScript

// https://github.com/brendangregg/FlameGraph#2-fold-stacks
import {Profile, FrameInfo} from '../profile'
interface BGSample {
stack: FrameInfo[]
duration: number
}
function parseBGFoldedStacks(contents: string): BGSample[] {
const samples: BGSample[] = []
contents.replace(/^(.*) (\d+)$/gm, (match: string, stack: string, n: string) => {
samples.push({
stack: stack.split(';').map(name => ({key: name, name: name})),
duration: parseInt(n, 10),
})
return match
})
return samples
}
export function importFromBGFlameGraph(contents: string): Profile {
const parsed = parseBGFoldedStacks(contents)
const duration = parsed.reduce((prev: number, cur: BGSample) => prev + cur.duration, 0)
const profile = new Profile(duration)
for (let sample of parsed) {
profile.appendSample(sample.stack, sample.duration)
}
return profile
}