diff --git a/packages/platform-apple/src/core/__tests__/perf-target.test.ts b/packages/platform-apple/src/core/__tests__/perf-target.test.ts index 490aa5c119..44072b0019 100644 --- a/packages/platform-apple/src/core/__tests__/perf-target.test.ts +++ b/packages/platform-apple/src/core/__tests__/perf-target.test.ts @@ -1,8 +1,12 @@ import { test } from 'vitest'; import assert from 'node:assert/strict'; import { promises as fs } from 'node:fs'; -import { IOS_DEVICE } from '../../__tests__/device-fixtures.ts'; -import { parseApplePsOutput, resolveIosDevicePerfTarget } from '../perf-target.ts'; +import { IOS_DEVICE, MACOS_DEVICE } from '../../__tests__/device-fixtures.ts'; +import { + parseApplePsOutput, + readAppleProcessSamples, + resolveIosDevicePerfTarget, +} from '../perf-target.ts'; import { createLocalAppleToolProvider, withAppleToolProvider } from '../tool-provider.ts'; test('parseApplePsOutput reads pid cpu rss and command columns', () => { @@ -28,6 +32,43 @@ test('parseApplePsOutput reads pid cpu rss and command columns', () => { ]); }); +test('parseApplePsOutput accepts a comma decimal separator', () => { + const rows = parseApplePsOutput('123 12,5 45678 /Applications/Test.app/Contents/MacOS/Test'); + + assert.deepEqual(rows, [ + { + pid: 123, + cpuPercent: 12.5, + rssKb: 45678, + command: '/Applications/Test.app/Contents/MacOS/Test', + }, + ]); +}); + +test('readAppleProcessSamples pins the C locale for the macOS ps read', async () => { + let psEnv: NodeJS.ProcessEnv | undefined; + const provider = createLocalAppleToolProvider({ + runCommand: async (cmd, _args, options) => { + if (cmd === 'ps') psEnv = options?.envOverrides; + return { + exitCode: 0, + stdout: '123 3.5 2048 /Applications/Test.app/Contents/MacOS/Test\n', + stderr: '', + }; + }, + }); + + const samples = await withAppleToolProvider(provider, () => + readAppleProcessSamples(MACOS_DEVICE, { + executableName: 'Test', + executablePath: '/Applications/Test.app/Contents/MacOS/Test', + }), + ); + + assert.equal(psEnv?.LC_ALL, 'C.UTF-8'); + assert.equal(samples.length, 1); +}); + test('resolveIosDevicePerfTarget skips nested extensions and other installed copies', async () => { const containers = 'file:///private/var/containers/Bundle/Application'; const appUrl = `${containers}/DEV/App.app/`; diff --git a/packages/platform-apple/src/core/perf-target.ts b/packages/platform-apple/src/core/perf-target.ts index 120bc5f90e..c489e92fa0 100644 --- a/packages/platform-apple/src/core/perf-target.ts +++ b/packages/platform-apple/src/core/perf-target.ts @@ -12,6 +12,10 @@ import { runAppleToolCommand, runXcrun, type ScopedSimctlCommand } from './tool- const APPLE_PERF_TIMEOUT_MS = 15_000; +// BSD ps formats %cpu with the user's LC_NUMERIC (`12,5` under de_DE); pin a C locale that +// keeps argv as UTF-8 text, and still accept a comma from reads that cannot be pinned. +const PS_LOCALE_ENV: NodeJS.ProcessEnv = { LC_ALL: 'C.UTF-8' }; + export type AppleProcessSample = { pid: number; cpuPercent: number; @@ -22,7 +26,7 @@ export type AppleProcessSample = { export function parseApplePsOutput(stdout: string): AppleProcessSample[] { const rows: AppleProcessSample[] = []; for (const line of splitNonEmptyTrimmedLines(stdout)) { - const match = line.match(/^(\d+)\s+([0-9]+(?:\.[0-9]+)?)\s+(\d+)\s+(.+)$/); + const match = line.match(/^(\d+)\s+([0-9]+(?:[.,][0-9]+)?)\s+(\d+)\s+(.+)$/); if (!match) continue; const [pidText, cpuText, rssText, commandText] = match.slice(1); if ( @@ -34,7 +38,7 @@ export function parseApplePsOutput(stdout: string): AppleProcessSample[] { continue; } const pid = Number(pidText); - const cpuPercent = Number(cpuText); + const cpuPercent = Number(cpuText.replace(',', '.')); const rssKb = Number(rssText); const command = commandText.trim(); if (!Number.isFinite(pid) || !Number.isFinite(cpuPercent) || !Number.isFinite(rssKb)) { @@ -155,6 +159,7 @@ export async function readAppleProcessSamples( const result = isMacOs(device) ? await runAppleToolCommand('ps', ['-axo', 'pid=,%cpu=,rss=,command='], { timeoutMs: APPLE_PERF_TIMEOUT_MS, + envOverrides: PS_LOCALE_ENV, }) : await runAppleSimulatorProcessCommand( buildSimctlArgsForDevice(device, [ @@ -179,5 +184,6 @@ async function runAppleSimulatorProcessCommand(args: ScopedSimctlCommand): Promi if (result.exitCode === 0) return result; return await runAppleToolCommand('ps', ['-axo', 'pid=,%cpu=,rss=,command='], { timeoutMs: APPLE_PERF_TIMEOUT_MS, + envOverrides: PS_LOCALE_ENV, }); }