Skip to content

Commit

Permalink
feat(tools): add tools command (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Almouro authored Sep 28, 2023
1 parent d9c2ca4 commit a981132
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 28 deletions.
20 changes: 14 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,35 @@ DEVELOPMENT_MODE=true node packages/commands/measure/dist/server/bin.js measure
To run the command locally:

```
node packages/commands/test/dist/bin.js test
```

This command is the equivalent of

```
flashlight test
```

### `tools` command

To run the command locally:

```
node packages/commands/tools/dist/bin.js tools
```

This command is the equivalent of

```
flashlight tools
```

### web-reporter

and run in another terminal:
Run in another terminal:

```
yarn workspace @perf-profiler/web-reporter start
```

Then in `packages/commands/report/src/App.tsx`, uncomment the lines to add your own measures:
Expand Down
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./packages/platforms/android",
"./packages/plugins/appium-helper",
"./packages/commands/test",
"./packages/commands/tools",
"./packages/core/reporter",
"./packages/core/web-reporter-ui",
"./packages/commands/report",
Expand Down
20 changes: 20 additions & 0 deletions packages/commands/tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@perf-profiler/tools",
"version": "0.1.0",
"author": {
"name": "Almouro",
"email": "alexandrem@bam.tech"
},
"license": "MIT",
"bin": {
"perf-profiler-tools": "dist/bin.js"
},
"scripts": {
"test": "tsc"
},
"dependencies": {
"@perf-profiler/profiler": "^0.10.7",
"@perf-profiler/shell": "^0.1.1",
"commander": "^9.4.0"
}
}
23 changes: 23 additions & 0 deletions packages/commands/tools/src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

import { program } from "commander";
import { processVideoFile } from "@perf-profiler/shell";
import { detectCurrentAppBundleId } from "@perf-profiler/profiler";

const toolsCommand = program.command("tools").description("Utility tools related to Flashlight");

toolsCommand
.command("android_get_bundle_id")
.description("Retrieves the focused app bundle id")
.action(() => {
console.log(detectCurrentAppBundleId().bundleId);
});

toolsCommand
.command("video_fix_metadata <videoFilePath>")
.description(
"When coming from AWS Device Farm or certain devices, it seems the video from flashlight test is not encoded properly"
)
.action((options) => {
processVideoFile(options.video_file_path, "destinationPath");
});
8 changes: 8 additions & 0 deletions packages/commands/tools/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../../tsconfig.module.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "./dist"
},
"include": ["src"]
}
2 changes: 2 additions & 0 deletions packages/core/shell/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const open = (path: string) => {
Logger.warn(`Failed to run "open ${path}"`);
}
};

export * from "./processVideoFile";
25 changes: 25 additions & 0 deletions packages/core/shell/src/processVideoFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { spawn } from "child_process";

const execAsync = (command: string) =>
new Promise<void>((resolve, reject) => {
const parts = command.split(" ");
const proc = spawn(parts[0], parts.slice(1));

proc.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Command ${command} failed with code ${code}`));
} else {
resolve();
}
});

proc.on("error", reject);
});

export const processVideoFile = async (filePath: string, destinationPath: string) => {
// When coming from AWS Device Farm or certain devices, it seems the video is not encoded properly
// VSync 0 is important since we have variable frame rate from adb shell screenrecord
await execAsync(
`ffmpeg -y -vsync 0 -i ${filePath} -c:v libx264 -crf 23 -c:a aac -b:a 128k ${destinationPath} -loglevel error`
);
};
1 change: 1 addition & 0 deletions packages/plugins/aws-device-farm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"@aws-sdk/client-device-farm": "^3.154.0",
"@perf-profiler/logger": "^0.3.1",
"@perf-profiler/shell": "^0.1.1",
"@perf-profiler/types": "^0.6.0",
"adm-zip": "^0.5.9",
"axios": "^1.1.3",
Expand Down
26 changes: 4 additions & 22 deletions packages/plugins/aws-device-farm/src/commands/checkResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,11 @@ import path from "path";
import { ArtifactType } from "@aws-sdk/client-device-farm";
import { Logger } from "@perf-profiler/logger";
import { TestCaseResult } from "@perf-profiler/types";
import { processVideoFile } from "@perf-profiler/shell";
import { testRepository } from "../repositories";
import { TMP_FOLDER } from "../TMP_FOLDER";
import { downloadFile } from "../utils/downloadFile";
import { unzip } from "../utils/unzip";
import { spawn } from "child_process";

const execAsync = (command: string) =>
new Promise<void>((resolve, reject) => {
const parts = command.split(" ");
const proc = spawn(parts[0], parts.slice(1));

proc.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Command ${command} failed with code ${code}`));
} else {
resolve();
}
});

proc.on("error", reject);
});

const changeVideoPathsOnResult = (
report: TestCaseResult,
Expand Down Expand Up @@ -77,13 +61,11 @@ export const checkResults = async ({
);
};

const processVideoFile = async (fileName: string) => {
const processVideo = async (fileName: string) => {
// When coming from AWS Device Farm, it seems the video is not encoded properly
Logger.info(`Fixing video metadata on ${fileName}...`);
// VSync 0 is important since we have variable frame rate from adb shell screenrecord
await execAsync(
`ffmpeg -y -vsync 0 -i ${tmpFolder}/${fileName} -c:v libx264 -crf 23 -c:a aac -b:a 128k ${reportDestinationPath}/${fileName} -loglevel error`
);
await processVideoFile(`${tmpFolder}/${fileName}`, `${reportDestinationPath}/${fileName}`);
Logger.info(`Video ${fileName} processed ✅`);
};

Expand All @@ -94,7 +76,7 @@ export const checkResults = async ({
}

if (file.endsWith(".mp4")) {
return processVideoFile(file);
return processVideo(file);
}

return Promise.resolve();
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{ "path": "./packages/platforms/android" },
{ "path": "./packages/plugins/appium-helper" },
{ "path": "./packages/commands/test" },
{ "path": "./packages/commands/tools" },
{ "path": "./packages/core/reporter" },
{ "path": "./packages/core/web-reporter-ui" },
{ "path": "./packages/commands/report" },
Expand Down

0 comments on commit a981132

Please sign in to comment.