@ohos-perf/cli 0.19.0 → 0.21.0

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.
Files changed (2) hide show
  1. package/dist/index.js +172 -0
  2. package/package.json +49 -49
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "fs
5
5
  import { join } from "path";
6
6
  import {
7
7
  discoverCases,
8
+ describeDecision,
8
9
  loadCases,
9
10
  OhosPerfError as OhosPerfError2,
10
11
  parseScenario
@@ -371,6 +372,45 @@ stopped ${stopped}
371
372
  `, exitCode: 1 };
372
373
  }
373
374
  }
375
+ async function runAgentCommand(deps2, req, opts) {
376
+ if (req.task.trim() === "") {
377
+ return {
378
+ stdout: "",
379
+ stderr: "agent: the task must not be empty\n",
380
+ exitCode: 1
381
+ };
382
+ }
383
+ try {
384
+ const result = await deps2.agent(req);
385
+ if (opts.json) return { stdout: json(result), stderr: "", exitCode: 0 };
386
+ const { transcript, device } = result;
387
+ const outcome = transcript.status === "completed" ? "completed (the screen confirmed it)" : transcript.status === "unverified" ? "unverified (the screen did not confirm it)" : transcript.status === "refused" ? "refused (an irreversible action was stopped)" : `exhausted (the step cap, ${req.maxSteps}, was reached)`;
388
+ const lines = transcript.steps.map((step) => {
389
+ const did = step.action === void 0 ? step.status : `${describeStep(step)} \u2014 ${step.status}`;
390
+ const moved = step.navigated ? "moved" : "no change";
391
+ const detail = step.detail === void 0 ? "" : `
392
+ ${step.detail}`;
393
+ return ` ${String(step.index + 1).padStart(2)}. ${did} (${moved}): ${step.reason}${detail}`;
394
+ });
395
+ return {
396
+ stdout: `agent ${transcript.app} on ${device.id} (${device.kind})
397
+ task ${transcript.task}
398
+ result ${outcome}
399
+ walk ${transcript.steps.length} steps, ${transcript.statesReached} states reached
400
+ ` + lines.map((line) => `${line}
401
+ `).join(""),
402
+ stderr: "",
403
+ exitCode: transcript.status === "completed" ? 0 : 1
404
+ };
405
+ } catch (err) {
406
+ return { stdout: "", stderr: `${failureLine(err)}
407
+ `, exitCode: 1 };
408
+ }
409
+ }
410
+ function describeStep(step) {
411
+ if (step.action === void 0) return step.status;
412
+ return describeDecision({ action: step.action, reason: step.reason });
413
+ }
374
414
  async function runRender(deps2, req, opts) {
375
415
  try {
376
416
  const result = await deps2.render(req);
@@ -449,6 +489,66 @@ function packagePathError(packagePath) {
449
489
  }
450
490
  return void 0;
451
491
  }
492
+ function binaryPathError(packagePath) {
493
+ let stats;
494
+ try {
495
+ stats = statSync(packagePath);
496
+ } catch {
497
+ return `no such package: ${packagePath}`;
498
+ }
499
+ if (!stats.isFile()) return `not a file: ${packagePath}`;
500
+ const dot = packagePath.lastIndexOf(".");
501
+ const extension = dot <= 0 ? "" : packagePath.slice(dot + 1).toLowerCase();
502
+ if (extension === "a") {
503
+ return `an .a archive is not read: pass the .so files, or the package that holds them`;
504
+ }
505
+ if (extension !== "hap" && extension !== "hsp" && extension !== "zip" && extension !== "so") {
506
+ return `not a package or library (expected .hap, .hsp, .zip or .so): ${packagePath}`;
507
+ }
508
+ return void 0;
509
+ }
510
+ var LTO_WORD = {
511
+ present: "LTO",
512
+ absent: "no LTO",
513
+ unknown: "LTO unknown"
514
+ };
515
+ async function runOptimization(deps2, req, opts) {
516
+ const pathError = binaryPathError(req.packagePath);
517
+ if (pathError !== void 0) return { stdout: "", stderr: `${pathError}
518
+ `, exitCode: 1 };
519
+ try {
520
+ const result = await deps2.opt(req);
521
+ if (opts.json) {
522
+ return {
523
+ stdout: json({ analysis: result.analysis, capabilities: result.capabilities }),
524
+ stderr: "",
525
+ exitCode: 0
526
+ };
527
+ }
528
+ const analysis = result.analysis;
529
+ let stdout = `package ${analysis.package}
530
+ `;
531
+ stdout += `binaries ${analysis.totals.binaries}, ${analysis.totals.lto} with LTO${analysis.totals.debug === 0 ? "" : `, ${analysis.totals.debug} with debug info`}${analysis.totals.stripped === 0 ? "" : `, ${analysis.totals.stripped} stripped`}
532
+ `;
533
+ for (const binary of analysis.binaries) {
534
+ const facts = binary.facts;
535
+ const verdict = binary.verdict;
536
+ const basis = verdict.basis === "none" ? "" : ` (${verdict.basis})`;
537
+ stdout += `binary ${binary.path} ${verdict.level}${basis}
538
+ `;
539
+ stdout += ` ${LTO_WORD[facts.lto]}${facts.ltoMarkers.length === 0 ? "" : ` via ${facts.ltoMarkers[0]}`}, ${facts.instructions.total} instructions, ${facts.symbols.functions} functions${facts.debug ? ", debug info" : ""}
540
+ `;
541
+ if (verdict.signals.length > 0) {
542
+ stdout += ` signals ${verdict.signals.join(", ")}
543
+ `;
544
+ }
545
+ }
546
+ return { stdout, stderr: "", exitCode: 0 };
547
+ } catch (err) {
548
+ return { stdout: "", stderr: `${failureLine(err)}
549
+ `, exitCode: 1 };
550
+ }
551
+ }
452
552
  async function runScenarioCommand(deps2, input, opts) {
453
553
  if (input.casesDir !== void 0) {
454
554
  if (input.scenarioPath !== "") {
@@ -737,6 +837,11 @@ import {
737
837
  renderFlamegraphHtml,
738
838
  renderFlamegraphSvg,
739
839
  hapSection,
840
+ analyzeBinaries,
841
+ extractBinaries,
842
+ httpAgentModel,
843
+ readAgentConfig,
844
+ runAgent,
740
845
  readSceneMetadata,
741
846
  readUiTree,
742
847
  resolveAppIdentity,
@@ -961,6 +1066,21 @@ function realDeps(version = packageVersion(), overrides = {}) {
961
1066
  const collectDevice = overrides.collectDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
962
1067
  const gestureDevice = overrides.gestureDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
963
1068
  const exploreDevice = overrides.exploreDevice ?? ((target) => connect(target === void 0 ? {} : { target }));
1069
+ const agentDevice = overrides.agentDevice ?? exploreDevice;
1070
+ const agentModel = overrides.agentModel ?? ((name) => {
1071
+ const config = readAgentConfig(process.env);
1072
+ if (config === void 0) {
1073
+ throw new OhosPerfError3(
1074
+ "no model endpoint is configured; set OHOS_PERF_AGENT_BASE_URL and OHOS_PERF_AGENT_API_KEY (or GLM_BASE_URL and GLM_API_KEY), and optionally OHOS_PERF_AGENT_MODEL (or GLM_MODEL)",
1075
+ { code: "AGENT_BAD_CONFIG" }
1076
+ );
1077
+ }
1078
+ return httpAgentModel({
1079
+ ...config,
1080
+ ...name === void 0 ? {} : { model: name },
1081
+ fetch: (url, init) => fetch(url, init)
1082
+ });
1083
+ });
964
1084
  const doctor = {
965
1085
  resolveHdc,
966
1086
  async resolveStreamer() {
@@ -1327,6 +1447,19 @@ function realDeps(version = packageVersion(), overrides = {}) {
1327
1447
  }
1328
1448
  return { analysis, capabilities, outDir, artifacts: artifacts.map(([name]) => name) };
1329
1449
  },
1450
+ async opt(req) {
1451
+ const bytes = readFileSync2(req.packagePath);
1452
+ const name = basename(req.packagePath);
1453
+ const analysis = analyzeBinaries(extractBinaries({ path: name, bytes }), name);
1454
+ const capabilities = [{
1455
+ id: "static.hap",
1456
+ available: true,
1457
+ collector: "offline",
1458
+ fidelity: "exact",
1459
+ probe: { command: "", stdout: "", stderr: "", code: 0 }
1460
+ }];
1461
+ return { analysis, capabilities };
1462
+ },
1330
1463
  tools() {
1331
1464
  return queryDefinitions().map((definition) => ({
1332
1465
  name: definition.name,
@@ -1364,6 +1497,17 @@ function realDeps(version = packageVersion(), overrides = {}) {
1364
1497
  ])]
1365
1498
  };
1366
1499
  },
1500
+ async agent(req) {
1501
+ const model = agentModel(req.model);
1502
+ const device = await agentDevice(req.target);
1503
+ const transcript = await runAgent(device, model, {
1504
+ task: req.task,
1505
+ app: req.app,
1506
+ maxSteps: req.maxSteps,
1507
+ ...req.allowIrreversible === true ? { allowIrreversible: true } : {}
1508
+ });
1509
+ return { transcript, device: { id: device.id, kind: device.kind } };
1510
+ },
1367
1511
  async run(req) {
1368
1512
  const device = await connect(req.target === void 0 ? {} : { target: req.target });
1369
1513
  const runner = new ScenarioRunner(device, {
@@ -1761,6 +1905,11 @@ function buildProgram(deps2, io, opts = {}) {
1761
1905
  )
1762
1906
  );
1763
1907
  });
1908
+ program2.command("opt").description(
1909
+ "inspect a package's arm64 libraries: optimization level, LTO, debug info"
1910
+ ).argument("<package>", "path to a .hap, .hsp, .zip or .so file").option("--json", "print the analysis as JSON").action(async (packagePath, opts2) => {
1911
+ io.emit(await runOptimization(deps2, { packagePath }, { json: opts2.json }));
1912
+ });
1764
1913
  program2.command("run").description("run a declarative scenario, capturing one scene per step").argument("[scenario]", "scenario JSON file (omit when using --cases)").requiredOption("--out <dir>", "run output directory (one scene per step)").option("--cases <dir>", "run every *.scenario.json in this directory as one suite").option("--filter <regex>", "anchored regex over case ids; only with --cases").option("--target <id>", "run against this hdc target instead of the first connected one").option("--rounds <n>", "repeat every step this many times (overrides the scenario)").option("--only <ids>", "comma-separated step ids to run, in scenario order").option("--keep-going", "continue after a failed step instead of stopping").option("--json", "print the run result as JSON").action(async (scenarioPath, opts2) => {
1765
1914
  let rounds;
1766
1915
  let only;
@@ -1823,6 +1972,29 @@ function buildProgram(deps2, io, opts = {}) {
1823
1972
  )
1824
1973
  );
1825
1974
  });
1975
+ program2.command("agent").description("drive an app toward a task with a model, and verify the screen reached").argument("<app>", "bundle name to drive, e.g. com.example.app").argument("<task>", "what the model should accomplish, in your words").option("--max-steps <n>", "stop after this many model turns", "50").option("--model <name>", "model name to send; defaults to the configured one").option("--allow-irreversible", "let the guard's refusals through (payment, ordering, ...)").option("--target <id>", "drive this hdc target instead of the first connected one").option("--json", "print the transcript as JSON").action(async (app, task, opts2) => {
1976
+ let maxSteps;
1977
+ try {
1978
+ maxSteps = parseMaxSteps(opts2.maxSteps);
1979
+ } catch (err) {
1980
+ io.emit({ stdout: "", stderr: `${failureLine(err)}\\n`, exitCode: 1 });
1981
+ return;
1982
+ }
1983
+ io.emit(
1984
+ await runAgentCommand(
1985
+ deps2,
1986
+ {
1987
+ app,
1988
+ task,
1989
+ maxSteps,
1990
+ ...opts2.model === void 0 ? {} : { model: opts2.model },
1991
+ ...opts2.allowIrreversible === true ? { allowIrreversible: true } : {},
1992
+ ...opts2.target === void 0 ? {} : { target: opts2.target }
1993
+ },
1994
+ { json: opts2.json }
1995
+ )
1996
+ );
1997
+ });
1826
1998
  program2.command("tools").description("list the named drill-down queries an agent can call").option("--json", "print the query definitions as JSON").action((opts2) => {
1827
1999
  io.emit(runTools(deps2, opts2));
1828
2000
  });
package/package.json CHANGED
@@ -1,49 +1,49 @@
1
- {
2
- "name": "@ohos-perf/cli",
3
- "version": "0.19.0",
4
- "description": "Command-line interface for HarmonyOS performance analysis: doctor, caps, collect, analyze, query, tools, compare, report, gesture, emulator and selector-driven scenarios",
5
- "keywords": [
6
- "harmonyos",
7
- "openharmony",
8
- "ohos",
9
- "performance",
10
- "profiling",
11
- "cli",
12
- "hapray"
13
- ],
14
- "type": "module",
15
- "bin": {
16
- "ohos-perf": "./dist/index.js"
17
- },
18
- "files": [
19
- "dist"
20
- ],
21
- "engines": {
22
- "node": "^22.13.0 || >=23.4.0"
23
- },
24
- "license": "MIT",
25
- "author": "growvv",
26
- "homepage": "https://growvv.github.io/ohos-perf/",
27
- "repository": {
28
- "type": "git",
29
- "url": "git+https://github.com/growvv/ohos-perf.git",
30
- "directory": "packages/cli"
31
- },
32
- "bugs": {
33
- "url": "https://github.com/growvv/ohos-perf/issues"
34
- },
35
- "publishConfig": {
36
- "access": "public"
37
- },
38
- "dependencies": {
39
- "@ohos-perf/core": "0.19.0",
40
- "commander": "^12.1.0"
41
- },
42
- "scripts": {
43
- "build": "tsup src/index.ts --format esm --target node22",
44
- "typecheck": "tsc --noEmit -p tsconfig.test.json",
45
- "pretest": "npm run build -w @ohos-perf/core",
46
- "test": "vitest run",
47
- "prepublishOnly": "npm run build"
48
- }
49
- }
1
+ {
2
+ "name": "@ohos-perf/cli",
3
+ "version": "0.21.0",
4
+ "description": "Command-line interface for HarmonyOS performance analysis: doctor, caps, collect, analyze, query, tools, compare, report, gesture, emulator and selector-driven scenarios",
5
+ "keywords": [
6
+ "harmonyos",
7
+ "openharmony",
8
+ "ohos",
9
+ "performance",
10
+ "profiling",
11
+ "cli",
12
+ "hapray"
13
+ ],
14
+ "type": "module",
15
+ "bin": {
16
+ "ohos-perf": "./dist/index.js"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "engines": {
22
+ "node": "^22.13.0 || >=23.4.0"
23
+ },
24
+ "license": "MIT",
25
+ "author": "growvv",
26
+ "homepage": "https://growvv.github.io/ohos-perf/",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/growvv/ohos-perf.git",
30
+ "directory": "packages/cli"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/growvv/ohos-perf/issues"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "dependencies": {
39
+ "@ohos-perf/core": "0.21.0",
40
+ "commander": "^12.1.0"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup src/index.ts --format esm --target node22",
44
+ "typecheck": "tsc --noEmit -p tsconfig.test.json",
45
+ "pretest": "npm run build -w @ohos-perf/core",
46
+ "test": "vitest run",
47
+ "prepublishOnly": "npm run build"
48
+ }
49
+ }