MCPcopy Create free account
hub / github.com/NomicFoundation/hardhat / profileSystem

Function profileSystem

scripts/profiler/main.ts:384–450  ·  view source on GitHub ↗
(
  scenario: Scenario,
  resolved: ResolvedCommand,
  runDir: string,
  baseEnv: NodeJS.ProcessEnv,
  args: ProfileArgs,
)

Source from the content-addressed store, hash-verified

382}
383
384function profileSystem(
385 scenario: Scenario,
386 resolved: ResolvedCommand,
387 runDir: string,
388 baseEnv: NodeJS.ProcessEnv,
389 args: ProfileArgs,
390): RunRecord {
391 const perfData = path.join(runDir, "perf.data");
392 const wrapped = wrapWithPerfCpu(
393 resolved.command,
394 perfData,
395 args.sampleRateHz,
396 );
397
398 const env: NodeJS.ProcessEnv = {
399 ...baseEnv,
400 NODE_OPTIONS: mergeNodeOptions(baseEnv.NODE_OPTIONS, PERF_JS_NODE_OPTIONS),
401 };
402
403 logStep(`[${scenario.id}] system profile: ${resolved.command}`);
404 const record = runInScenario(
405 wrapped,
406 scenario,
407 env,
408 commandLogPath(runDir, Mode.System),
409 args.showOutput,
410 );
411
412 // Resolving JS frame names depends on the /tmp/perf-<pid>.map files the
413 // profiled processes just wrote. Symbolize to ensure output is
414 // self-contained.
415 const samples = path.join(runDir, PERF_SCRIPT_OUTPUT_FILENAME);
416
417 // Remove artifacts from a previous run in the same --out-dir, so that a
418 // crash between symbolizing and compressing does not leave stale samples
419 // behind.
420 rmSync(`${samples}.gz`, { force: true });
421
422 const symbolized = runPostProcess(
423 `perf script -i ${shellQuote(perfData)} > ${shellQuote(samples)}`,
424 );
425
426 runPostProcess(
427 `perf report -i ${shellQuote(perfData)} --stdio --no-children --no-call-graph --sort dso > ${shellQuote(path.join(runDir, "dso.txt"))}`,
428 );
429 runPostProcess(
430 `perf report -i ${shellQuote(perfData)} --stdio --no-children --no-call-graph --sort dso,sym --percent-limit 0.05 > ${shellQuote(path.join(runDir, "report.txt"))}`,
431 );
432
433 if (symbolized) {
434 runPostProcess(`gzip -f ${shellQuote(samples)}`);
435 } else {
436 // A truncated samples file would masquerade as a complete profile.
437 rmSync(samples, { force: true });
438 }
439
440 // perf.data is machine-bound and large, so only keep it if the user
441 // explicitly requested it or if symbolization failed

Callers 1

runProfileFunction · 0.85

Calls 8

wrapWithPerfCpuFunction · 0.90
mergeNodeOptionsFunction · 0.90
logStepFunction · 0.90
shellQuoteFunction · 0.90
runInScenarioFunction · 0.85
commandLogPathFunction · 0.85
runPostProcessFunction · 0.85
toRunRecordFunction · 0.85

Tested by

no test coverage detected