peachsim 0.1.2479 → 0.1.2480
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/commands/perf-native.ts +886 -0
- package/cli/commands/perf.ts +6 -1
- package/dist-lib/agent-daemon-client.cjs +1 -1
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +1 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +2 -2
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +5 -5
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +3 -3
- package/dist-lib/bundle-channel.cjs +3 -3
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +3 -3
- package/dist-lib/cloud-contract.cjs +1 -1
- package/dist-lib/cloud-contract.mjs +3 -3
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +2 -2
- package/dist-lib/cloud.cjs +1 -1
- package/dist-lib/cloud.mjs +7 -7
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +5 -5
- package/dist-lib/detox/index.cjs +2 -2
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/esm-chunks/{acorn-6T74WJEH.mjs → acorn-RHJLDFQ6.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-AYJWQFTP.mjs → chunk-43UZGHLJ.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-AEZPI62A.mjs → chunk-5VNECNVL.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-D7WY4WGU.mjs → chunk-AN4RNKME.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-PFCYY52I.mjs → chunk-HHXID6CS.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-FNLQ2DEH.mjs → chunk-IAQ6RFFI.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-3ZA6FTBR.mjs → chunk-IGQJDRVL.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-BZT6SR75.mjs → chunk-IMJ6FVRR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-RQYBISJH.mjs → chunk-JW6P2F6T.mjs} +4 -4
- package/dist-lib/esm-chunks/{chunk-2LPGM53Y.mjs → chunk-K633NA6X.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-GPU6YJ5I.mjs → chunk-KPDAPPKX.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-MQX3VV72.mjs → chunk-LIULVSRG.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-UWPIIUE4.mjs → chunk-LNLVUOIL.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-V4TJGQ5E.mjs → chunk-NZYUWM5K.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-OER7EEIH.mjs → chunk-OW2NZLWH.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-R2TIEVD6.mjs → chunk-PB53B3G3.mjs} +2 -2
- package/dist-lib/esm-chunks/{chunk-QHX5XHZQ.mjs → chunk-PPI4PXUE.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-TVVDHHO3.mjs → chunk-QNFDJM4V.mjs} +12 -12
- package/dist-lib/esm-chunks/{chunk-G4WYTA67.mjs → chunk-QWPRWZCR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-C4R3TCHX.mjs → chunk-U5UJRFVB.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-XWTX6G5Q.mjs → chunk-VYP6I44Y.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-IHHNYCWO.mjs → chunk-X7K3JLJ3.mjs} +1 -1
- package/dist-lib/esm-chunks/{metro-layout-parser-LMCYOPJD.mjs → metro-layout-parser-K64IERC5.mjs} +3 -3
- package/dist-lib/esm-chunks/{swift-package-prebuilt-NSH43QDM.mjs → swift-package-prebuilt-4B2KGEQL.mjs} +10 -10
- package/dist-lib/esm-chunks/{swift-sdk-app-module-RZ5BXP7S.mjs → swift-sdk-app-module-SUCFPPKO.mjs} +13 -13
- package/dist-lib/esm-chunks/{swift-sdk-image-3TJP3PYO.mjs → swift-sdk-image-YX5FMNXN.mjs} +13 -13
- package/dist-lib/esm-chunks/{swift-sdk-image-producer-SZLGEC7P.mjs → swift-sdk-image-producer-WFJBSXZG.mjs} +4 -4
- package/dist-lib/expo-swift-bundle.cjs +2 -2
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +7 -7
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +3 -3
- package/dist-lib/host/fetch-proxy-handler.cjs +3 -3
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +1 -1
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +3 -3
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +3 -3
- package/dist-lib/metro-production-bundle.cjs +1 -1
- package/dist-lib/metro-production-bundle.mjs +4 -4
- package/dist-lib/native-app-identity-project.cjs +1 -1
- package/dist-lib/native-app-identity-project.mjs +15 -15
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +6 -6
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +3 -3
- package/dist-lib/pdfium-raster.cjs +1 -1
- package/dist-lib/pdfium-raster.mjs +2 -2
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +3 -3
- package/dist-lib/profiles.cjs +1 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +79 -79
- package/dist-lib/public-origins-lib.json +69 -69
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +2 -2
- package/dist-lib/reap-browser.cjs +1 -1
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +1 -1
- package/dist-lib/sdk.mjs +7 -7
- package/dist-lib/skills.cjs +3 -3
- package/dist-lib/swift-compile.cjs +1 -1
- package/dist-lib/swift-compile.mjs +10 -10
- package/dist-lib/swift-package.cjs +1 -1
- package/dist-lib/swift-package.mjs +3 -3
- package/dist-lib/swift-submit.cjs +1 -1
- package/dist-lib/swift-submit.mjs +5 -5
- package/dist-lib/swift-svg-raster.cjs +1 -1
- package/dist-lib/swift-svg-raster.mjs +2 -2
- package/dist-lib/swift-toolchain.cjs +1 -1
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
- package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
- package/dist-lib/vendor/acorn.cjs +1 -1
- package/dist-lib/vendor/bplist-parser.cjs +1 -1
- package/dist-lib/vendor/ignore.cjs +1 -1
- package/dist-lib/vendor/magic-string.cjs +1 -1
- package/dist-lib/vendor/plist.cjs +1 -1
- package/dist-lib/vendor/smol-toml.cjs +1 -1
- package/dist-lib/vendor/stream-json.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
- package/dist-lib/vendor/sucrase.cjs +1 -1
- package/dist-lib/vendor/terser.cjs +1 -1
- package/dist-lib/vendor/valibot.cjs +1 -1
- package/dist-lib/vendor/ws.cjs +1 -1
- package/dist-lib/vendor/zod.cjs +1 -1
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +3 -3
- package/dist-types/packages/peach-engine/src/engine/image-host.d.ts +1 -1
- package/dist-types/packages/peach-engine/src/engine/remote-display-protocol.d.ts +3 -2
- package/dist-types/packages/peach-engine/src/observability/diagnostics-sample.d.ts +2 -1
- package/dist-types/packages/peach-engine/src/observability/types.d.ts +6 -0
- package/dist-types/packages/peach-engine/src/remote-stream/agent-action.d.ts +4 -0
- package/dist-types/packages/peach-engine/src/remote-stream/client-config.d.ts +1 -0
- package/dist-types/packages/peach-engine/src/render-worker/app-load.d.ts +0 -1
- package/package.json +1 -1
- package/runtime-companion.json +1 -1
- package/src/canvas-agent-note-delivery.ts +3 -3
|
@@ -0,0 +1,886 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import {
|
|
4
|
+
copyFileSync,
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
statSync,
|
|
11
|
+
writeFileSync,
|
|
12
|
+
} from 'node:fs'
|
|
13
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
14
|
+
import { parseArgs } from 'node:util'
|
|
15
|
+
|
|
16
|
+
type NativeProfileFunction = {
|
|
17
|
+
name: string
|
|
18
|
+
url: string
|
|
19
|
+
line: number
|
|
20
|
+
column: number
|
|
21
|
+
selfMs: number
|
|
22
|
+
totalMs: number
|
|
23
|
+
selfSamples: number
|
|
24
|
+
totalSamples: number
|
|
25
|
+
selfPct: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type NativeProfileComponent = {
|
|
29
|
+
name: string
|
|
30
|
+
commits: number
|
|
31
|
+
renders: number
|
|
32
|
+
totalMs: number
|
|
33
|
+
maxMs: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type NativeProfileReport = {
|
|
37
|
+
schema: number
|
|
38
|
+
bundleId: string
|
|
39
|
+
build: string
|
|
40
|
+
executableUUID: string
|
|
41
|
+
route: string
|
|
42
|
+
startWallMs: number
|
|
43
|
+
durationMs: number
|
|
44
|
+
frames: number
|
|
45
|
+
droppedFrames: number
|
|
46
|
+
fps: number
|
|
47
|
+
longestFrameMs: number
|
|
48
|
+
mainThreadBusyMs: number
|
|
49
|
+
mainThreadBusyPct: number
|
|
50
|
+
jsMs: number
|
|
51
|
+
nativeMs: number
|
|
52
|
+
phases: Record<string, number>
|
|
53
|
+
jsSamples: number
|
|
54
|
+
topJsFunctions: NativeProfileFunction[]
|
|
55
|
+
topJsCallers?: NativeProfileFunction[]
|
|
56
|
+
reactCommits: number
|
|
57
|
+
topReactComponents: NativeProfileComponent[]
|
|
58
|
+
files: {
|
|
59
|
+
cpuprofile: string
|
|
60
|
+
trace: string
|
|
61
|
+
summary: string
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type ParsedSourceMap = {
|
|
66
|
+
sources: string[]
|
|
67
|
+
names: string[]
|
|
68
|
+
lines: Array<
|
|
69
|
+
Array<{
|
|
70
|
+
genCol: number
|
|
71
|
+
srcIdx: number
|
|
72
|
+
origLine: number
|
|
73
|
+
origCol: number
|
|
74
|
+
nameIdx: number
|
|
75
|
+
}>
|
|
76
|
+
>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type SourceMapBundle = {
|
|
80
|
+
rawMaps: Record<string, string>
|
|
81
|
+
decoded: Map<string, ParsedSourceMap | null>
|
|
82
|
+
declByFileAndName: Map<string, number>
|
|
83
|
+
declByName: Map<string, { file: string; line: number }>
|
|
84
|
+
demangledCache: Map<string, { name?: string; source?: string; line?: number } | null>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const VLQ_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
88
|
+
const VLQ_MAP = new Int8Array(128).fill(-1)
|
|
89
|
+
for (let i = 0; i < VLQ_CHARS.length; i++) VLQ_MAP[VLQ_CHARS.charCodeAt(i)] = i
|
|
90
|
+
|
|
91
|
+
function decodeSourceMap(raw: string): ParsedSourceMap | null {
|
|
92
|
+
try {
|
|
93
|
+
const parsed: unknown = JSON.parse(raw)
|
|
94
|
+
if (!parsed || typeof parsed !== 'object') return null
|
|
95
|
+
const sourcesVal = Reflect.get(parsed, 'sources')
|
|
96
|
+
const namesVal = Reflect.get(parsed, 'names')
|
|
97
|
+
const mappingsVal = Reflect.get(parsed, 'mappings')
|
|
98
|
+
if (!Array.isArray(sourcesVal) || typeof mappingsVal !== 'string') return null
|
|
99
|
+
const sources = sourcesVal.map((s) => (typeof s === 'string' ? s : ''))
|
|
100
|
+
const names = Array.isArray(namesVal)
|
|
101
|
+
? namesVal.map((n) => (typeof n === 'string' ? n : ''))
|
|
102
|
+
: []
|
|
103
|
+
const lines: ParsedSourceMap['lines'] = []
|
|
104
|
+
let srcIdx = 0
|
|
105
|
+
let origLine = 0
|
|
106
|
+
let origCol = 0
|
|
107
|
+
let nameIdx = 0
|
|
108
|
+
let pos = 0
|
|
109
|
+
const len = mappingsVal.length
|
|
110
|
+
const readVlq = (): number => {
|
|
111
|
+
let result = 0
|
|
112
|
+
let shift = 0
|
|
113
|
+
while (pos < len) {
|
|
114
|
+
const code = mappingsVal.charCodeAt(pos++)
|
|
115
|
+
const digit = code < 128 ? VLQ_MAP[code]! : -1
|
|
116
|
+
if (digit < 0) break
|
|
117
|
+
const cont = (digit & 32) !== 0
|
|
118
|
+
result += (digit & 31) << shift
|
|
119
|
+
shift += 5
|
|
120
|
+
if (!cont) break
|
|
121
|
+
}
|
|
122
|
+
return (result & 1) === 1 ? -(result >> 1) : result >> 1
|
|
123
|
+
}
|
|
124
|
+
let currentLine: ParsedSourceMap['lines'][number] = []
|
|
125
|
+
while (pos < len) {
|
|
126
|
+
const ch = mappingsVal.charCodeAt(pos)
|
|
127
|
+
if (ch === 59) {
|
|
128
|
+
lines.push(currentLine)
|
|
129
|
+
currentLine = []
|
|
130
|
+
pos++
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
if (ch === 44) {
|
|
134
|
+
pos++
|
|
135
|
+
continue
|
|
136
|
+
}
|
|
137
|
+
let genCol = currentLine.length ? currentLine[currentLine.length - 1]!.genCol : 0
|
|
138
|
+
genCol += readVlq()
|
|
139
|
+
if (
|
|
140
|
+
pos < len &&
|
|
141
|
+
mappingsVal.charCodeAt(pos) !== 44 &&
|
|
142
|
+
mappingsVal.charCodeAt(pos) !== 59
|
|
143
|
+
) {
|
|
144
|
+
srcIdx += readVlq()
|
|
145
|
+
origLine += readVlq()
|
|
146
|
+
origCol += readVlq()
|
|
147
|
+
let segName = -1
|
|
148
|
+
if (
|
|
149
|
+
pos < len &&
|
|
150
|
+
mappingsVal.charCodeAt(pos) !== 44 &&
|
|
151
|
+
mappingsVal.charCodeAt(pos) !== 59
|
|
152
|
+
) {
|
|
153
|
+
nameIdx += readVlq()
|
|
154
|
+
segName = nameIdx
|
|
155
|
+
}
|
|
156
|
+
currentLine.push({ genCol, srcIdx, origLine, origCol, nameIdx: segName })
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
lines.push(currentLine)
|
|
160
|
+
return { sources, names, lines }
|
|
161
|
+
} catch {
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function lookupPosition(
|
|
167
|
+
map: ParsedSourceMap,
|
|
168
|
+
line1: number,
|
|
169
|
+
col1: number,
|
|
170
|
+
): { source: string; line: number; column: number; name?: string } | null {
|
|
171
|
+
const row = map.lines[line1 - 1]
|
|
172
|
+
if (!row || row.length === 0) return null
|
|
173
|
+
const targetCol = Math.max(0, col1 - 1)
|
|
174
|
+
let lo = 0
|
|
175
|
+
let hi = row.length - 1
|
|
176
|
+
let best = row[0]!
|
|
177
|
+
while (lo <= hi) {
|
|
178
|
+
const mid = (lo + hi) >> 1
|
|
179
|
+
const seg = row[mid]!
|
|
180
|
+
if (seg.genCol <= targetCol) {
|
|
181
|
+
best = seg
|
|
182
|
+
lo = mid + 1
|
|
183
|
+
} else {
|
|
184
|
+
hi = mid - 1
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const source = map.sources[best.srcIdx]
|
|
188
|
+
if (!source) return null
|
|
189
|
+
return {
|
|
190
|
+
source,
|
|
191
|
+
line: best.origLine + 1,
|
|
192
|
+
column: best.origCol + 1,
|
|
193
|
+
name: best.nameIdx >= 0 ? map.names[best.nameIdx] : undefined,
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function loadSourceMapsBundle(
|
|
198
|
+
sourceMapsPath: string | undefined,
|
|
199
|
+
executableUUID: string,
|
|
200
|
+
): SourceMapBundle {
|
|
201
|
+
const empty: SourceMapBundle = {
|
|
202
|
+
rawMaps: {},
|
|
203
|
+
decoded: new Map(),
|
|
204
|
+
declByFileAndName: new Map(),
|
|
205
|
+
declByName: new Map(),
|
|
206
|
+
demangledCache: new Map(),
|
|
207
|
+
}
|
|
208
|
+
if (!sourceMapsPath) return empty
|
|
209
|
+
let targetFile = resolve(sourceMapsPath)
|
|
210
|
+
if (existsSync(targetFile) && statSync(targetFile).isDirectory()) {
|
|
211
|
+
const expected = `OneJavaScriptSourceMaps-${executableUUID}.json`
|
|
212
|
+
const direct = join(targetFile, expected)
|
|
213
|
+
if (existsSync(direct)) {
|
|
214
|
+
targetFile = direct
|
|
215
|
+
} else {
|
|
216
|
+
const candidate = readdirSync(targetFile).find(
|
|
217
|
+
(name) =>
|
|
218
|
+
name.startsWith('OneJavaScriptSourceMaps-') &&
|
|
219
|
+
name.endsWith('.json') &&
|
|
220
|
+
(!executableUUID || name.toLowerCase().includes(executableUUID.toLowerCase())),
|
|
221
|
+
)
|
|
222
|
+
if (!candidate) return empty
|
|
223
|
+
targetFile = join(targetFile, candidate)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (!existsSync(targetFile)) return empty
|
|
227
|
+
const parsed: unknown = JSON.parse(readFileSync(targetFile, 'utf8'))
|
|
228
|
+
if (!parsed || typeof parsed !== 'object') return empty
|
|
229
|
+
const mapsObj = Reflect.get(parsed, 'maps')
|
|
230
|
+
if (mapsObj && typeof mapsObj === 'object') {
|
|
231
|
+
for (const [key, value] of Object.entries(mapsObj)) {
|
|
232
|
+
if (typeof value === 'string') empty.rawMaps[key] = value
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const compiledFrames = Reflect.get(parsed, 'compiledFrames')
|
|
236
|
+
const entries =
|
|
237
|
+
compiledFrames && typeof compiledFrames === 'object'
|
|
238
|
+
? Reflect.get(compiledFrames, 'entries')
|
|
239
|
+
: null
|
|
240
|
+
if (Array.isArray(entries)) {
|
|
241
|
+
const frameRe = /^(\S+)\s+.*one-native-aot\/([a-f0-9]{64}\.[a-z.]+):(\d+)$/
|
|
242
|
+
for (const entry of entries) {
|
|
243
|
+
if (!entry || typeof entry !== 'object') continue
|
|
244
|
+
const name = Reflect.get(entry, 'name')
|
|
245
|
+
if (typeof name !== 'string') continue
|
|
246
|
+
const match = frameRe.exec(name)
|
|
247
|
+
if (!match) continue
|
|
248
|
+
const fn = match[1]!
|
|
249
|
+
const file = match[2]!
|
|
250
|
+
const line = Number(match[3])
|
|
251
|
+
if (fn.startsWith('(')) continue
|
|
252
|
+
const fk = `${file}|${fn}`
|
|
253
|
+
if (!empty.declByFileAndName.has(fk)) empty.declByFileAndName.set(fk, line)
|
|
254
|
+
if (!empty.declByName.has(fn)) empty.declByName.set(fn, { file, line })
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return empty
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function getDecodedMap(bundle: SourceMapBundle, fileKey: string): ParsedSourceMap | null {
|
|
261
|
+
if (bundle.decoded.has(fileKey)) return bundle.decoded.get(fileKey) ?? null
|
|
262
|
+
const raw = bundle.rawMaps[fileKey]
|
|
263
|
+
const parsed = raw ? decodeSourceMap(raw) : null
|
|
264
|
+
bundle.decoded.set(fileKey, parsed)
|
|
265
|
+
return parsed
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function demangleAotSymbol(
|
|
269
|
+
bundle: SourceMapBundle,
|
|
270
|
+
fileKey: string | undefined,
|
|
271
|
+
fnName: string,
|
|
272
|
+
): { name?: string; source?: string; line?: number } | null {
|
|
273
|
+
if (!fnName || fnName.startsWith('(')) return null
|
|
274
|
+
const cacheKey = `${fileKey ?? ''}|${fnName}`
|
|
275
|
+
if (bundle.demangledCache.has(cacheKey))
|
|
276
|
+
return bundle.demangledCache.get(cacheKey) ?? null
|
|
277
|
+
let resolvedFile = fileKey
|
|
278
|
+
let declLine = fileKey
|
|
279
|
+
? bundle.declByFileAndName.get(`${fileKey}|${fnName}`)
|
|
280
|
+
: undefined
|
|
281
|
+
if (declLine === undefined) {
|
|
282
|
+
const byName = bundle.declByName.get(fnName)
|
|
283
|
+
if (byName) {
|
|
284
|
+
resolvedFile = byName.file
|
|
285
|
+
declLine = byName.line
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (!resolvedFile || declLine === undefined) {
|
|
289
|
+
bundle.demangledCache.set(cacheKey, null)
|
|
290
|
+
return null
|
|
291
|
+
}
|
|
292
|
+
const map = getDecodedMap(bundle, resolvedFile)
|
|
293
|
+
if (!map) {
|
|
294
|
+
bundle.demangledCache.set(cacheKey, null)
|
|
295
|
+
return null
|
|
296
|
+
}
|
|
297
|
+
const row = map.lines[declLine - 1]
|
|
298
|
+
let origName: string | undefined
|
|
299
|
+
let origSrc: string | undefined
|
|
300
|
+
let origLine: number | undefined
|
|
301
|
+
if (row) {
|
|
302
|
+
for (const seg of row) {
|
|
303
|
+
if (!origSrc && map.sources[seg.srcIdx]) {
|
|
304
|
+
origSrc = map.sources[seg.srcIdx]
|
|
305
|
+
origLine = seg.origLine + 1
|
|
306
|
+
}
|
|
307
|
+
if (seg.nameIdx >= 0 && map.names[seg.nameIdx]) {
|
|
308
|
+
origName = map.names[seg.nameIdx]
|
|
309
|
+
origSrc = map.sources[seg.srcIdx] || origSrc
|
|
310
|
+
origLine = seg.origLine + 1
|
|
311
|
+
break
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const out =
|
|
316
|
+
origName || origSrc ? { name: origName, source: origSrc, line: origLine } : null
|
|
317
|
+
bundle.demangledCache.set(cacheKey, out)
|
|
318
|
+
return out
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function resolveAotLocation(
|
|
322
|
+
bundle: SourceMapBundle,
|
|
323
|
+
url: string,
|
|
324
|
+
line1: number,
|
|
325
|
+
col1: number,
|
|
326
|
+
fnName: string,
|
|
327
|
+
): { url: string; line: number; column: number; name?: string } | null {
|
|
328
|
+
if (!url) return null
|
|
329
|
+
const match = /one-native-aot\/([a-f0-9]{64}\.[a-z.]+)$/.exec(url)
|
|
330
|
+
const fileKey = match ? match[1]! : basename(url)
|
|
331
|
+
const map = getDecodedMap(bundle, fileKey)
|
|
332
|
+
if (!map) return null
|
|
333
|
+
const demangled = demangleAotSymbol(bundle, fileKey, fnName)
|
|
334
|
+
if (line1 > 0) {
|
|
335
|
+
const resolved = lookupPosition(map, line1, Math.max(1, col1))
|
|
336
|
+
if (resolved) {
|
|
337
|
+
return {
|
|
338
|
+
url: resolved.source,
|
|
339
|
+
line: resolved.line,
|
|
340
|
+
column: resolved.column,
|
|
341
|
+
name: demangled?.name ?? resolved.name,
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const fallbackSource = demangled?.source ?? map.sources[0]
|
|
346
|
+
if (!fallbackSource) return null
|
|
347
|
+
return {
|
|
348
|
+
url: fallbackSource,
|
|
349
|
+
line: demangled?.line ?? -1,
|
|
350
|
+
column: -1,
|
|
351
|
+
name: demangled?.name,
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function symbolicateCpuProfile(cpuProfilePath: string, bundle: SourceMapBundle): void {
|
|
356
|
+
if (!Object.keys(bundle.rawMaps).length || !existsSync(cpuProfilePath)) return
|
|
357
|
+
const parsed: unknown = JSON.parse(readFileSync(cpuProfilePath, 'utf8'))
|
|
358
|
+
if (!parsed || typeof parsed !== 'object') return
|
|
359
|
+
const nodes = Reflect.get(parsed, 'nodes')
|
|
360
|
+
if (!Array.isArray(nodes)) return
|
|
361
|
+
let changed = false
|
|
362
|
+
for (const node of nodes) {
|
|
363
|
+
if (!node || typeof node !== 'object') continue
|
|
364
|
+
const frame = Reflect.get(node, 'callFrame')
|
|
365
|
+
if (!frame || typeof frame !== 'object') continue
|
|
366
|
+
const url = Reflect.get(frame, 'url')
|
|
367
|
+
const lineNumber = Reflect.get(frame, 'lineNumber')
|
|
368
|
+
const columnNumber = Reflect.get(frame, 'columnNumber')
|
|
369
|
+
const fnName = Reflect.get(frame, 'functionName')
|
|
370
|
+
if (typeof url !== 'string' || !url) continue
|
|
371
|
+
const resolved = resolveAotLocation(
|
|
372
|
+
bundle,
|
|
373
|
+
url,
|
|
374
|
+
typeof lineNumber === 'number' && lineNumber >= 0 ? lineNumber + 1 : -1,
|
|
375
|
+
typeof columnNumber === 'number' && columnNumber >= 0 ? columnNumber + 1 : 1,
|
|
376
|
+
typeof fnName === 'string' ? fnName : '',
|
|
377
|
+
)
|
|
378
|
+
if (!resolved) continue
|
|
379
|
+
Reflect.set(frame, 'url', resolved.url)
|
|
380
|
+
if (resolved.line > 0) Reflect.set(frame, 'lineNumber', resolved.line - 1)
|
|
381
|
+
if (resolved.column > 0) Reflect.set(frame, 'columnNumber', resolved.column - 1)
|
|
382
|
+
if (
|
|
383
|
+
resolved.name &&
|
|
384
|
+
(!fnName || fnName === '(anonymous)' || fnName !== resolved.name)
|
|
385
|
+
) {
|
|
386
|
+
Reflect.set(frame, 'functionName', resolved.name)
|
|
387
|
+
}
|
|
388
|
+
changed = true
|
|
389
|
+
}
|
|
390
|
+
if (changed) writeFileSync(cpuProfilePath, JSON.stringify(parsed))
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function symbolicateTrace(tracePath: string, bundle: SourceMapBundle): void {
|
|
394
|
+
if (!bundle.declByName.size || !existsSync(tracePath)) return
|
|
395
|
+
const parsed: unknown = JSON.parse(readFileSync(tracePath, 'utf8'))
|
|
396
|
+
if (!parsed || typeof parsed !== 'object') return
|
|
397
|
+
const events = Reflect.get(parsed, 'traceEvents')
|
|
398
|
+
if (!Array.isArray(events)) return
|
|
399
|
+
let changed = false
|
|
400
|
+
for (const ev of events) {
|
|
401
|
+
if (!ev || typeof ev !== 'object') continue
|
|
402
|
+
if (Reflect.get(ev, 'cat') !== 'js.sample') continue
|
|
403
|
+
const name = Reflect.get(ev, 'name')
|
|
404
|
+
if (typeof name !== 'string' || !name || name.startsWith('(')) continue
|
|
405
|
+
const demangled = demangleAotSymbol(bundle, undefined, name)
|
|
406
|
+
if (demangled?.name && demangled.name !== name) {
|
|
407
|
+
Reflect.set(ev, 'name', demangled.name)
|
|
408
|
+
changed = true
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (changed) writeFileSync(tracePath, JSON.stringify(parsed))
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function symbolicateFnList(list: NativeProfileFunction[], bundle: SourceMapBundle): void {
|
|
415
|
+
for (const entry of list) {
|
|
416
|
+
const resolved = resolveAotLocation(
|
|
417
|
+
bundle,
|
|
418
|
+
entry.url,
|
|
419
|
+
entry.line,
|
|
420
|
+
entry.column,
|
|
421
|
+
entry.name,
|
|
422
|
+
)
|
|
423
|
+
if (!resolved) continue
|
|
424
|
+
entry.url = resolved.url
|
|
425
|
+
if (resolved.line > 0) entry.line = resolved.line
|
|
426
|
+
if (resolved.column > 0) entry.column = resolved.column
|
|
427
|
+
if (resolved.name) entry.name = resolved.name
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function symbolicateReport(report: NativeProfileReport, bundle: SourceMapBundle): void {
|
|
432
|
+
if (!Object.keys(bundle.rawMaps).length) return
|
|
433
|
+
symbolicateFnList(report.topJsFunctions, bundle)
|
|
434
|
+
if (report.topJsCallers) symbolicateFnList(report.topJsCallers, bundle)
|
|
435
|
+
for (const comp of report.topReactComponents) {
|
|
436
|
+
const demangled = demangleAotSymbol(bundle, undefined, comp.name)
|
|
437
|
+
if (demangled?.name && demangled.name !== comp.name) {
|
|
438
|
+
comp.name = demangled.source
|
|
439
|
+
? `${demangled.name} (${basename(demangled.source)})`
|
|
440
|
+
: demangled.name
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function detectSimulatorBundleId(udid: string): string {
|
|
446
|
+
const list = execFileSync('xcrun', ['simctl', 'spawn', udid, 'launchctl', 'list'], {
|
|
447
|
+
encoding: 'utf8',
|
|
448
|
+
})
|
|
449
|
+
const running: string[] = []
|
|
450
|
+
for (const line of list.split('\n')) {
|
|
451
|
+
const match = /^\s*(\d+)\s+\S+\s+UIKitApplication:([^[]+)\[/.exec(line)
|
|
452
|
+
if (!match) continue
|
|
453
|
+
const id = match[2]!.trim()
|
|
454
|
+
if (!id.startsWith('com.apple.')) running.push(id)
|
|
455
|
+
}
|
|
456
|
+
if (running.length === 1) return running[0]!
|
|
457
|
+
if (running.length > 1) {
|
|
458
|
+
throw new Error(
|
|
459
|
+
`Multiple user apps are running on simulator ${udid}: ${running.join(', ')}. Pass --bundle-id <id>.`,
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
throw new Error(
|
|
463
|
+
`No running user app found on simulator ${udid}. Launch your app or pass --bundle-id <id>.`,
|
|
464
|
+
)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function simContainer(udid: string, bundleId: string): string {
|
|
468
|
+
return execFileSync('xcrun', ['simctl', 'get_app_container', udid, bundleId, 'data'], {
|
|
469
|
+
encoding: 'utf8',
|
|
470
|
+
}).trim()
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function waitForFile(path: string, timeoutMs: number): Promise<string> {
|
|
474
|
+
const deadline = Date.now() + timeoutMs
|
|
475
|
+
while (Date.now() < deadline) {
|
|
476
|
+
if (existsSync(path)) {
|
|
477
|
+
const content = readFileSync(path, 'utf8')
|
|
478
|
+
if (content.trim().startsWith('{')) return content
|
|
479
|
+
}
|
|
480
|
+
await new Promise((r) => setTimeout(r, 150))
|
|
481
|
+
}
|
|
482
|
+
throw new Error(`Timed out after ${timeoutMs}ms waiting for profiler result at ${path}`)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function formatLocation(url: string, line: number): string {
|
|
486
|
+
if (!url) return ''
|
|
487
|
+
const parts = url.split('/')
|
|
488
|
+
const short = parts.length > 2 ? parts.slice(-2).join('/') : url
|
|
489
|
+
return line > 0 ? `${short}:${line}` : short
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function printSummary(
|
|
493
|
+
report: NativeProfileReport,
|
|
494
|
+
outFiles: { cpuprofile: string; trace: string; summary: string },
|
|
495
|
+
): void {
|
|
496
|
+
console.log(
|
|
497
|
+
`One Profiler: ${report.bundleId || 'app'} (build ${report.build}, route ${report.route})`,
|
|
498
|
+
)
|
|
499
|
+
console.log(
|
|
500
|
+
` duration: ${report.durationMs.toFixed(1)} ms | fps: ${report.fps.toFixed(1)} (${report.frames} frames, ${report.droppedFrames} dropped, longest ${report.longestFrameMs.toFixed(1)} ms)`,
|
|
501
|
+
)
|
|
502
|
+
console.log(
|
|
503
|
+
` main thread busy: ${report.mainThreadBusyMs.toFixed(1)} ms (${report.mainThreadBusyPct.toFixed(1)}%) | JS ${report.jsMs.toFixed(1)} ms | Native ${report.nativeMs.toFixed(1)} ms`,
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
const phaseRows = Object.entries(report.phases)
|
|
507
|
+
.filter(([, ms]) => ms > 0.05)
|
|
508
|
+
.sort((a, b) => b[1] - a[1])
|
|
509
|
+
if (phaseRows.length) {
|
|
510
|
+
console.log('\nTop native & runtime phases:')
|
|
511
|
+
const denom = Math.max(1, report.mainThreadBusyMs)
|
|
512
|
+
for (const [name, ms] of phaseRows.slice(0, 10)) {
|
|
513
|
+
const pct = ((ms / denom) * 100).toFixed(1)
|
|
514
|
+
console.log(
|
|
515
|
+
` ${name.padEnd(16)} ${ms.toFixed(1).padStart(8)} ms (${pct.padStart(5)}%)`,
|
|
516
|
+
)
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (report.topJsFunctions.length) {
|
|
521
|
+
console.log(`\nTop JS functions by self time (${report.jsSamples} samples):`)
|
|
522
|
+
for (const fn of report.topJsFunctions.slice(0, 15)) {
|
|
523
|
+
const loc = formatLocation(fn.url, fn.line)
|
|
524
|
+
console.log(
|
|
525
|
+
` ${fn.selfMs.toFixed(1).padStart(7)} ms self (${fn.selfPct.toFixed(1).padStart(5)}%) | ${fn.totalMs.toFixed(1).padStart(7)} ms total | ${fn.name}${loc ? ` (${loc})` : ''}`,
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
} else {
|
|
529
|
+
console.log(`\nTop JS functions: none (${report.jsSamples} samples)`)
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (report.topJsCallers && report.topJsCallers.length) {
|
|
533
|
+
console.log('\nTop JS callers by inclusive time:')
|
|
534
|
+
for (const fn of report.topJsCallers.slice(0, 10)) {
|
|
535
|
+
const loc = formatLocation(fn.url, fn.line)
|
|
536
|
+
console.log(
|
|
537
|
+
` ${fn.totalMs.toFixed(1).padStart(7)} ms total (${String(fn.totalSamples).padStart(3)} samples) | ${fn.selfMs.toFixed(1).padStart(6)} ms self | ${fn.name}${loc ? ` (${loc})` : ''}`,
|
|
538
|
+
)
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (report.topReactComponents.length) {
|
|
543
|
+
console.log(`\nTop React components (${report.reactCommits} commits):`)
|
|
544
|
+
for (const comp of report.topReactComponents.slice(0, 10)) {
|
|
545
|
+
const timing =
|
|
546
|
+
comp.totalMs > 0 ? `${comp.totalMs.toFixed(1)} ms` : `${comp.renders} renders`
|
|
547
|
+
console.log(
|
|
548
|
+
` ${comp.name.padEnd(36)} ${timing.padStart(12)} (${comp.commits} commits)`,
|
|
549
|
+
)
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
console.log('\nArtifacts:')
|
|
554
|
+
console.log(` cpuprofile: ${outFiles.cpuprofile}`)
|
|
555
|
+
console.log(` trace: ${outFiles.trace}`)
|
|
556
|
+
console.log(` summary: ${outFiles.summary}`)
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function printHelp(): void {
|
|
560
|
+
console.log(`
|
|
561
|
+
peach perf native: profile a running One v3 iOS simulator or device app
|
|
562
|
+
|
|
563
|
+
usage:
|
|
564
|
+
peach perf native [--bundle-id <id>] [--udid <sim-udid>] [--duration <sec>] [-o <prefix>]
|
|
565
|
+
peach perf native --device <device-udid> --bundle-id <id> --scheme <url-scheme> [--duration <sec>] [-o <prefix>]
|
|
566
|
+
peach perf native --overlay <on|off|toggle> [--bundle-id <id>] [--udid <sim-udid>]
|
|
567
|
+
peach perf native --start|--stop [--bundle-id <id>] [--udid <sim-udid>] [-o <prefix>]
|
|
568
|
+
|
|
569
|
+
options:
|
|
570
|
+
--bundle-id <id> App bundle identifier (auto-detected if one user app is running on simulator)
|
|
571
|
+
--udid <id> Simulator UDID (default: booted)
|
|
572
|
+
--device <id> Physical iOS device UDID (uses xcrun devicectl)
|
|
573
|
+
--scheme <name> App URL scheme for physical device trigger
|
|
574
|
+
--duration <sec> Capture window in seconds (default: 5)
|
|
575
|
+
-o, --output <path> Output file prefix or directory (default: /tmp/one-profile-<ts>)
|
|
576
|
+
--source-maps <path> Path to OneJavaScriptSourceMaps-<uuid>.json or build directory for AoT symbolication
|
|
577
|
+
--overlay <mode> Toggle floating overlay: on, off, or toggle
|
|
578
|
+
--start Start profiling without waiting
|
|
579
|
+
--stop Stop an active profile and pull artifacts
|
|
580
|
+
--json Print JSON summary to stdout
|
|
581
|
+
`)
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
585
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function asNum(value: unknown): number {
|
|
589
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : 0
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function asStr(value: unknown): string {
|
|
593
|
+
return typeof value === 'string' ? value : ''
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function parseNativeReport(value: unknown): NativeProfileReport | undefined {
|
|
597
|
+
if (!isRecord(value)) return undefined
|
|
598
|
+
const phasesRaw = isRecord(value.phases) ? value.phases : {}
|
|
599
|
+
const phases: Record<string, number> = {}
|
|
600
|
+
for (const [k, v] of Object.entries(phasesRaw)) {
|
|
601
|
+
if (typeof v === 'number' && Number.isFinite(v)) phases[k] = v
|
|
602
|
+
}
|
|
603
|
+
const files = isRecord(value.files) ? value.files : {}
|
|
604
|
+
const parseFnList = (raw: unknown): NativeProfileFunction[] =>
|
|
605
|
+
Array.isArray(raw)
|
|
606
|
+
? raw.filter(isRecord).map((row) => ({
|
|
607
|
+
name: asStr(row.name),
|
|
608
|
+
url: asStr(row.url),
|
|
609
|
+
line: asNum(row.line),
|
|
610
|
+
column: asNum(row.column),
|
|
611
|
+
selfMs: asNum(row.selfMs),
|
|
612
|
+
totalMs: asNum(row.totalMs),
|
|
613
|
+
selfSamples: asNum(row.selfSamples),
|
|
614
|
+
totalSamples: asNum(row.totalSamples),
|
|
615
|
+
selfPct: asNum(row.selfPct),
|
|
616
|
+
}))
|
|
617
|
+
: []
|
|
618
|
+
const topJsFunctions = parseFnList(value.topJsFunctions)
|
|
619
|
+
const topJsCallers = parseFnList(value.topJsCallers)
|
|
620
|
+
const topReactComponents = Array.isArray(value.topReactComponents)
|
|
621
|
+
? value.topReactComponents.filter(isRecord).map((row) => ({
|
|
622
|
+
name: asStr(row.name),
|
|
623
|
+
commits: asNum(row.commits),
|
|
624
|
+
renders: asNum(row.renders),
|
|
625
|
+
totalMs: asNum(row.totalMs),
|
|
626
|
+
maxMs: asNum(row.maxMs),
|
|
627
|
+
}))
|
|
628
|
+
: []
|
|
629
|
+
return {
|
|
630
|
+
schema: asNum(value.schema),
|
|
631
|
+
bundleId: asStr(value.bundleId),
|
|
632
|
+
build: asStr(value.build),
|
|
633
|
+
executableUUID: asStr(value.executableUUID),
|
|
634
|
+
route: asStr(value.route),
|
|
635
|
+
startWallMs: asNum(value.startWallMs),
|
|
636
|
+
durationMs: asNum(value.durationMs),
|
|
637
|
+
frames: asNum(value.frames),
|
|
638
|
+
droppedFrames: asNum(value.droppedFrames),
|
|
639
|
+
fps: asNum(value.fps),
|
|
640
|
+
longestFrameMs: asNum(value.longestFrameMs),
|
|
641
|
+
mainThreadBusyMs: asNum(value.mainThreadBusyMs),
|
|
642
|
+
mainThreadBusyPct: asNum(value.mainThreadBusyPct),
|
|
643
|
+
jsMs: asNum(value.jsMs),
|
|
644
|
+
nativeMs: asNum(value.nativeMs),
|
|
645
|
+
phases,
|
|
646
|
+
jsSamples: asNum(value.jsSamples),
|
|
647
|
+
topJsFunctions,
|
|
648
|
+
topJsCallers,
|
|
649
|
+
reactCommits: asNum(value.reactCommits),
|
|
650
|
+
topReactComponents,
|
|
651
|
+
files: {
|
|
652
|
+
cpuprofile: asStr(files.cpuprofile),
|
|
653
|
+
trace: asStr(files.trace),
|
|
654
|
+
summary: asStr(files.summary),
|
|
655
|
+
},
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function parseReportEnvelope(raw: string): {
|
|
660
|
+
ok: boolean
|
|
661
|
+
error?: string
|
|
662
|
+
report?: NativeProfileReport
|
|
663
|
+
} {
|
|
664
|
+
const parsed: unknown = JSON.parse(raw)
|
|
665
|
+
if (!isRecord(parsed)) {
|
|
666
|
+
return { ok: false, error: 'Invalid profiler response JSON' }
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
ok: Boolean(parsed.ok),
|
|
670
|
+
error: typeof parsed.error === 'string' ? parsed.error : undefined,
|
|
671
|
+
report: parseNativeReport(parsed.report),
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
export async function runPerfNative(args: string[]): Promise<number> {
|
|
676
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
677
|
+
printHelp()
|
|
678
|
+
return 0
|
|
679
|
+
}
|
|
680
|
+
const { values } = parseArgs({
|
|
681
|
+
args,
|
|
682
|
+
options: {
|
|
683
|
+
'bundle-id': { type: 'string' },
|
|
684
|
+
udid: { type: 'string', default: 'booted' },
|
|
685
|
+
device: { type: 'string' },
|
|
686
|
+
scheme: { type: 'string' },
|
|
687
|
+
duration: { type: 'string', default: '5' },
|
|
688
|
+
output: { type: 'string', short: 'o' },
|
|
689
|
+
'source-maps': { type: 'string' },
|
|
690
|
+
overlay: { type: 'string' },
|
|
691
|
+
start: { type: 'boolean', default: false },
|
|
692
|
+
stop: { type: 'boolean', default: false },
|
|
693
|
+
json: { type: 'boolean', default: false },
|
|
694
|
+
},
|
|
695
|
+
strict: true,
|
|
696
|
+
})
|
|
697
|
+
|
|
698
|
+
const durationSec = Number(values.duration)
|
|
699
|
+
if (!Number.isFinite(durationSec) || durationSec <= 0) {
|
|
700
|
+
throw new Error('--duration must be a positive number of seconds')
|
|
701
|
+
}
|
|
702
|
+
const commandId = randomUUID().slice(0, 12)
|
|
703
|
+
const outPrefix = resolve(values.output ?? `/tmp/one-profile-${Date.now()}`)
|
|
704
|
+
const outBase =
|
|
705
|
+
existsSync(outPrefix) && statSync(outPrefix).isDirectory()
|
|
706
|
+
? join(outPrefix, `one-profile-${Date.now()}`)
|
|
707
|
+
: outPrefix.replace(/\.(cpuprofile|trace\.json|summary\.json|json)$/, '')
|
|
708
|
+
|
|
709
|
+
if (values.device) {
|
|
710
|
+
const bundleId = values['bundle-id']
|
|
711
|
+
if (!bundleId) throw new Error('--device requires --bundle-id')
|
|
712
|
+
if (values.overlay) {
|
|
713
|
+
if (!values.scheme)
|
|
714
|
+
throw new Error('--device requires --scheme for runtime triggers')
|
|
715
|
+
const mode =
|
|
716
|
+
values.overlay === 'on' ? '1' : values.overlay === 'off' ? '0' : 'toggle'
|
|
717
|
+
execFileSync('xcrun', [
|
|
718
|
+
'devicectl',
|
|
719
|
+
'device',
|
|
720
|
+
'process',
|
|
721
|
+
'launch',
|
|
722
|
+
'--device',
|
|
723
|
+
values.device,
|
|
724
|
+
bundleId,
|
|
725
|
+
'--payload-url',
|
|
726
|
+
`${values.scheme}://__one_profiler/overlay?visible=${mode}`,
|
|
727
|
+
])
|
|
728
|
+
console.log(`overlay ${values.overlay} sent to ${bundleId} on ${values.device}`)
|
|
729
|
+
return 0
|
|
730
|
+
}
|
|
731
|
+
if (!values.scheme)
|
|
732
|
+
throw new Error('--device requires --scheme to trigger capture on a running app')
|
|
733
|
+
const url = values.stop
|
|
734
|
+
? `${values.scheme}://__one_profiler/stop?id=${commandId}`
|
|
735
|
+
: `${values.scheme}://__one_profiler/start?durationMs=${Math.round(durationSec * 1000)}&id=${commandId}`
|
|
736
|
+
execFileSync('xcrun', [
|
|
737
|
+
'devicectl',
|
|
738
|
+
'device',
|
|
739
|
+
'process',
|
|
740
|
+
'launch',
|
|
741
|
+
'--device',
|
|
742
|
+
values.device,
|
|
743
|
+
bundleId,
|
|
744
|
+
'--payload-url',
|
|
745
|
+
url,
|
|
746
|
+
])
|
|
747
|
+
if (values.start) {
|
|
748
|
+
console.log(`started profiling ${bundleId} on ${values.device}`)
|
|
749
|
+
return 0
|
|
750
|
+
}
|
|
751
|
+
if (!values.stop) {
|
|
752
|
+
await new Promise((r) => setTimeout(r, Math.round(durationSec * 1000) + 1500))
|
|
753
|
+
}
|
|
754
|
+
const pullDir = `${outBase}-device`
|
|
755
|
+
mkdirSync(pullDir, { recursive: true })
|
|
756
|
+
execFileSync('xcrun', [
|
|
757
|
+
'devicectl',
|
|
758
|
+
'device',
|
|
759
|
+
'copy',
|
|
760
|
+
'from',
|
|
761
|
+
'--device',
|
|
762
|
+
values.device,
|
|
763
|
+
'--domain-type',
|
|
764
|
+
'appDataContainer',
|
|
765
|
+
'--domain-identifier',
|
|
766
|
+
bundleId,
|
|
767
|
+
'--source',
|
|
768
|
+
'Library/Application Support/OneProfiler',
|
|
769
|
+
'--destination',
|
|
770
|
+
pullDir,
|
|
771
|
+
])
|
|
772
|
+
const resultFile = join(pullDir, 'OneProfiler', `result-${commandId}.json`)
|
|
773
|
+
if (!existsSync(resultFile)) {
|
|
774
|
+
throw new Error(`Device profiler result not found at ${resultFile}`)
|
|
775
|
+
}
|
|
776
|
+
const env = parseReportEnvelope(readFileSync(resultFile, 'utf8'))
|
|
777
|
+
if (!env.ok || !env.report) throw new Error(env.error ?? 'Device profiler failed')
|
|
778
|
+
const maps = loadSourceMapsBundle(values['source-maps'], env.report.executableUUID)
|
|
779
|
+
const pulledBase = join(pullDir, 'OneProfiler')
|
|
780
|
+
const outFiles = {
|
|
781
|
+
cpuprofile: `${outBase}.cpuprofile`,
|
|
782
|
+
trace: `${outBase}.trace.json`,
|
|
783
|
+
summary: `${outBase}.summary.json`,
|
|
784
|
+
}
|
|
785
|
+
mkdirSync(dirname(outBase), { recursive: true })
|
|
786
|
+
copyFileSync(
|
|
787
|
+
join(pulledBase, basename(env.report.files.cpuprofile)),
|
|
788
|
+
outFiles.cpuprofile,
|
|
789
|
+
)
|
|
790
|
+
copyFileSync(join(pulledBase, basename(env.report.files.trace)), outFiles.trace)
|
|
791
|
+
symbolicateCpuProfile(outFiles.cpuprofile, maps)
|
|
792
|
+
symbolicateTrace(outFiles.trace, maps)
|
|
793
|
+
symbolicateReport(env.report, maps)
|
|
794
|
+
env.report.files = outFiles
|
|
795
|
+
writeFileSync(outFiles.summary, JSON.stringify(env.report, null, 2) + '\n')
|
|
796
|
+
rmSync(pullDir, { recursive: true, force: true })
|
|
797
|
+
if (values.json) console.log(JSON.stringify(env.report, null, 2))
|
|
798
|
+
else printSummary(env.report, outFiles)
|
|
799
|
+
return 0
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
const udid = values.udid
|
|
803
|
+
const bundleId = values['bundle-id'] ?? detectSimulatorBundleId(udid)
|
|
804
|
+
const container = simContainer(udid, bundleId)
|
|
805
|
+
const profilerDir = join(container, 'Library/Application Support/OneProfiler')
|
|
806
|
+
mkdirSync(profilerDir, { recursive: true })
|
|
807
|
+
|
|
808
|
+
const commandFile = join(profilerDir, 'command.json')
|
|
809
|
+
const resultFile = join(profilerDir, `result-${commandId}.json`)
|
|
810
|
+
rmSync(resultFile, { force: true })
|
|
811
|
+
|
|
812
|
+
const action = values.overlay
|
|
813
|
+
? 'overlay'
|
|
814
|
+
: values.start
|
|
815
|
+
? 'start'
|
|
816
|
+
: values.stop
|
|
817
|
+
? 'stop'
|
|
818
|
+
: 'capture'
|
|
819
|
+
const payload: Record<string, unknown> = { id: commandId, action }
|
|
820
|
+
if (action === 'overlay') {
|
|
821
|
+
if (values.overlay === 'on' || values.overlay === '1' || values.overlay === 'true') {
|
|
822
|
+
payload.visible = true
|
|
823
|
+
} else if (
|
|
824
|
+
values.overlay === 'off' ||
|
|
825
|
+
values.overlay === '0' ||
|
|
826
|
+
values.overlay === 'false'
|
|
827
|
+
) {
|
|
828
|
+
payload.visible = false
|
|
829
|
+
}
|
|
830
|
+
} else if (action === 'capture') {
|
|
831
|
+
payload.durationMs = Math.round(durationSec * 1000)
|
|
832
|
+
payload.sampleJs = true
|
|
833
|
+
payload.react = true
|
|
834
|
+
} else if (action === 'start') {
|
|
835
|
+
payload.sampleJs = true
|
|
836
|
+
payload.react = true
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
writeFileSync(commandFile, JSON.stringify(payload))
|
|
840
|
+
execFileSync('xcrun', [
|
|
841
|
+
'simctl',
|
|
842
|
+
'notify_post',
|
|
843
|
+
udid,
|
|
844
|
+
`dev.one.profiler.command.${bundleId}`,
|
|
845
|
+
])
|
|
846
|
+
|
|
847
|
+
const timeoutMs =
|
|
848
|
+
action === 'capture' ? Math.round(durationSec * 1000) + 15_000 : 10_000
|
|
849
|
+
const rawResult = await waitForFile(resultFile, timeoutMs)
|
|
850
|
+
rmSync(resultFile, { force: true })
|
|
851
|
+
|
|
852
|
+
if (action === 'overlay' || action === 'start') {
|
|
853
|
+
if (values.json) console.log(rawResult)
|
|
854
|
+
else console.log(`${action} succeeded for ${bundleId}`)
|
|
855
|
+
return 0
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const env = parseReportEnvelope(rawResult)
|
|
859
|
+
if (!env.ok || !env.report) {
|
|
860
|
+
throw new Error(env.error ?? 'Profiler capture failed')
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
const report = env.report
|
|
864
|
+
const maps = loadSourceMapsBundle(values['source-maps'], report.executableUUID)
|
|
865
|
+
const outFiles = {
|
|
866
|
+
cpuprofile: `${outBase}.cpuprofile`,
|
|
867
|
+
trace: `${outBase}.trace.json`,
|
|
868
|
+
summary: `${outBase}.summary.json`,
|
|
869
|
+
}
|
|
870
|
+
mkdirSync(dirname(outBase), { recursive: true })
|
|
871
|
+
if (report.files.cpuprofile && existsSync(report.files.cpuprofile)) {
|
|
872
|
+
copyFileSync(report.files.cpuprofile, outFiles.cpuprofile)
|
|
873
|
+
symbolicateCpuProfile(outFiles.cpuprofile, maps)
|
|
874
|
+
}
|
|
875
|
+
if (report.files.trace && existsSync(report.files.trace)) {
|
|
876
|
+
copyFileSync(report.files.trace, outFiles.trace)
|
|
877
|
+
symbolicateTrace(outFiles.trace, maps)
|
|
878
|
+
}
|
|
879
|
+
symbolicateReport(report, maps)
|
|
880
|
+
report.files = outFiles
|
|
881
|
+
writeFileSync(outFiles.summary, JSON.stringify(report, null, 2) + '\n')
|
|
882
|
+
|
|
883
|
+
if (values.json) console.log(JSON.stringify(report, null, 2))
|
|
884
|
+
else printSummary(report, outFiles)
|
|
885
|
+
return 0
|
|
886
|
+
}
|