Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for JS Self-Profiling data #388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {decodeBase64} from '../lib/utils'
import {importFromChromeHeapProfile} from './v8heapalloc'
import {isTraceEventFormatted, importTraceEvents} from './trace-event'
import {importFromCallgrind} from './callgrind'
import {importFromJSSelfProfiling} from './js-self-profiling'

export async function importProfileGroupFromText(
fileName: string,
Expand Down Expand Up @@ -180,6 +181,14 @@ async function _importProfileGroup(dataSource: ProfileDataSource): Promise<Profi
} else if ('recording' in parsed && 'sampleStackTraces' in parsed.recording) {
console.log('Importing as Safari profile')
return toGroup(importFromSafari(JSON.parse(contents)))
} else if (
'frames' in parsed &&
'resources' in parsed &&
'stacks' in parsed &&
'samples' in parsed
) {
console.log('Importing as JS Self-Profiling')
return importFromJSSelfProfiling(parsed)
}
} else {
// Format is not JSON
Expand Down
71 changes: 71 additions & 0 deletions src/import/js-self-profiling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {importSpeedscopeProfiles} from '../lib/file-format'
import {FileFormat} from '../lib/file-format-spec'
import {ProfileGroup} from '../lib/profile'

type Sample = {
timestamp: number
stackId?: number
}
type Frame = {
name: string
column?: number
line?: number
resourceId?: number
}
type StackNode = {
frameId: number
parentId?: number
}
type JSSelfProfilingData = {
samples: Sample[]
resources: string[]
frames: Frame[]
stacks: StackNode[]
}

function jsspToSpeedscope(json: JSSelfProfilingData): FileFormat.File {
function getFrames(stackId: number | undefined): number[] {
if (stackId == null) return []
const {frameId, parentId} = json.stacks[stackId]
return [...getFrames(parentId), frameId]
}

const profile = {
type: FileFormat.ProfileType.SAMPLED,
name: 'profile',
unit: 'milliseconds',
startValue: Math.min.apply(
null,
json.samples.map(s => s.timestamp),
),
endValue: Math.max.apply(
null,
json.samples.map(s => s.timestamp),
),
samples: json.samples.map(({stackId}) => getFrames(stackId)),
weights: json.samples.map(({timestamp}, i) =>
i === 0 ? 0 : timestamp - json.samples[i - 1].timestamp,
),
} as FileFormat.Profile

const frames = json.frames.map(f => {
return {
name: f.name,
line: f.line,
col: f.column,
file: f.resourceId != null ? json.resources[f.resourceId] : '(unknown)',
}
})

return {
$schema: 'https://www.speedscope.app/file-format-schema.json',
shared: {
frames,
},
profiles: [profile],
}
}

export function importFromJSSelfProfiling(serialized: JSSelfProfilingData): ProfileGroup {
return importSpeedscopeProfiles(jsspToSpeedscope(serialized))
}