Skip to content

Commit 3eb636a

Browse files
committed
test: add cpu & memory monitoring for e2e tests
1 parent 8e4a18c commit 3eb636a

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

.github/workflows/e2e-tests-linux-split.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,9 @@ jobs:
328328
with:
329329
name: allure-results
330330
path: ./artifacts/reports/allure/results
331+
332+
- name: Upload performance metrics
333+
uses: actions/upload-artifact@v4
334+
with:
335+
name: allure-results
336+
path: ./artifacts/metrics

packages/e2e-tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ reports/
33
screenshots/
44
wallet-extension-safari-build/
55
src/support/walletConfiguration.ts
6+
metrics/

packages/e2e-tests/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@rpii/wdio-report-events": "8.0.2",
3535
"@types/flat": "5.0.5",
3636
"@types/node": "20.14.2",
37+
"@types/pidusage": "2.0.5",
3738
"@types/puppeteer": "7.0.4",
3839
"@typescript-eslint/eslint-plugin": "6.0.0",
3940
"@typescript-eslint/parser": "6.0.0",
@@ -53,6 +54,7 @@
5354
"eslint-plugin-import": "2.27.5",
5455
"eslint-plugin-wdio": "8.37.0",
5556
"flat": "6.0.1",
57+
"pidusage": "4.0.1",
5658
"testcontainers": "10.24.2",
5759
"ts-node": "10.9.1",
5860
"typescript": "^4.9.5",

packages/e2e-tests/src/hooks/scenarioTagRunner.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ import consoleManager from '../utils/consoleManager';
55

66
import allure from '@wdio/allure-reporter';
77
import testContext from '../utils/testContext';
8+
import { PidMonitor } from '../support/PidMonitor';
9+
const monitor = new PidMonitor(1000);
810

911
// eslint-disable-next-line no-unused-vars
1012
Before(async () => {
1113
if (String(process.env.SERVICE_WORKER_LOGS) === 'true') {
1214
await consoleManager.startLogsCollection();
1315
}
16+
const pidMonitorInitialized = await monitor.init();
17+
if (!pidMonitorInitialized) return;
18+
monitor.start();
1419
});
1520

16-
After({ tags: 'not @Pending and not @pending' }, async () => {
21+
After({ tags: 'not @Pending and not @pending' }, async (scenario) => {
22+
monitor.stop();
23+
monitor.saveToFile(`./metrics/${scenario.testCaseStartedId}-chrome-usage.json`);
24+
// allure.addAttachment('Perf data', monitor.data, 'text/plain');
25+
monitor.clear();
26+
1727
testContext.clearContext();
1828
await browser.reloadSession();
1929
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pidusage from 'pidusage';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import { exec } from 'node:child_process';
5+
import { promisify } from 'util';
6+
import { clearInterval } from 'node:timers';
7+
8+
const execAsync = promisify(exec);
9+
10+
interface UsageData {
11+
timestamp: string;
12+
cpu: number;
13+
memory: number;
14+
}
15+
16+
export class PidMonitor {
17+
get data(): UsageData[] {
18+
return this._data;
19+
}
20+
21+
private pid: number | undefined;
22+
private intervalMs: number;
23+
private _data: UsageData[] = [];
24+
private timer: ReturnType<typeof setTimeout> | undefined = undefined;
25+
26+
constructor(intervalMs = 1000) {
27+
this.intervalMs = intervalMs;
28+
}
29+
30+
public async init(): Promise<boolean> {
31+
const processName = 'Google Chrome for Testing';
32+
try {
33+
const { stdout } = await execAsync(`pgrep -x "${processName}"`);
34+
this.pid = Number(stdout.trim());
35+
return true;
36+
} catch (error) {
37+
console.error('Error finding PID:', error);
38+
return false;
39+
}
40+
}
41+
42+
public start(): void {
43+
if (!this.pid) {
44+
throw new Error('PID is not set. Did you call init()?');
45+
}
46+
if (this.timer) return;
47+
48+
this.timer = setInterval(async () => {
49+
try {
50+
const stats = await pidusage(this.pid!);
51+
this._data.push({
52+
timestamp: new Date().toISOString(),
53+
cpu: stats.cpu,
54+
memory: stats.memory
55+
});
56+
} catch {
57+
this.stop();
58+
}
59+
}, this.intervalMs);
60+
}
61+
62+
public stop(): void {
63+
if (this.timer) {
64+
clearInterval(this.timer);
65+
this.timer = undefined;
66+
}
67+
}
68+
69+
public clear(): void {
70+
this._data = [];
71+
}
72+
73+
public saveToFile(filePath: string): void {
74+
const dir = path.dirname(filePath);
75+
if (!fs.existsSync(dir)) {
76+
fs.mkdirSync(dir, { recursive: true });
77+
}
78+
fs.writeFileSync(filePath, JSON.stringify(this._data, undefined, 2), 'utf-8');
79+
}
80+
}

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12673,6 +12673,7 @@ __metadata:
1267312673
"@types/chai-string": 1.4.5
1267412674
"@types/flat": 5.0.5
1267512675
"@types/node": 20.14.2
12676+
"@types/pidusage": 2.0.5
1267612677
"@types/puppeteer": 7.0.4
1267712678
"@typescript-eslint/eslint-plugin": 6.0.0
1267812679
"@typescript-eslint/parser": 6.0.0
@@ -12695,6 +12696,7 @@ __metadata:
1269512696
eslint-plugin-import: 2.27.5
1269612697
eslint-plugin-wdio: 8.37.0
1269712698
flat: 6.0.1
12699+
pidusage: 4.0.1
1269812700
testcontainers: 10.24.2
1269912701
ts-node: 10.9.1
1270012702
typescript: ^4.9.5
@@ -23063,6 +23065,13 @@ __metadata:
2306323065
languageName: node
2306423066
linkType: hard
2306523067

23068+
"@types/pidusage@npm:2.0.5":
23069+
version: 2.0.5
23070+
resolution: "@types/pidusage@npm:2.0.5"
23071+
checksum: 24188bf108b9b5a2ccb16155a492ff1cca7d7bf07aa4e2649cce421e0940dd5b0cd06baa48da9b8bde46343463fc500a7070c0788a0ed3f3f57d3e3bcf4ac64d
23072+
languageName: node
23073+
linkType: hard
23074+
2306623075
"@types/pify@npm:5.0.1":
2306723076
version: 5.0.1
2306823077
resolution: "@types/pify@npm:5.0.1"
@@ -47956,6 +47965,15 @@ __metadata:
4795647965
languageName: node
4795747966
linkType: hard
4795847967

47968+
"pidusage@npm:4.0.1":
47969+
version: 4.0.1
47970+
resolution: "pidusage@npm:4.0.1"
47971+
dependencies:
47972+
safe-buffer: ^5.2.1
47973+
checksum: 89ce11679b9871f8da6f21160c734f195fb6230e4a1017235133ac5294b9b0ec94d6c55a66fba43474bac524a7845ea309f43f37ce0e5c8f4e23512fa14ef2b6
47974+
languageName: node
47975+
linkType: hard
47976+
4795947977
"pify@npm:5.0.0, pify@npm:^5.0.0":
4796047978
version: 5.0.0
4796147979
resolution: "pify@npm:5.0.0"

0 commit comments

Comments
 (0)