Skip to content

Commit 0a0aac9

Browse files
authored
chore: support bench multiple case in pr (#62)
1 parent ca4f327 commit 0a0aac9

File tree

7 files changed

+124
-56
lines changed

7 files changed

+124
-56
lines changed

.github/workflows/monitor-metrics.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: Monitor Metrics
4141
id: monitor
4242
run: |
43-
result=$(cd scripts && MONITOR=1 node ./dist/compare.js ${{ matrix.product }})
43+
result=$(cd scripts && MONITOR=1 pnpm compare ${{ matrix.product }})
4444
echo "$result"
4545
echo "diff-result=${result//$'\n'/'@@'}" >> $GITHUB_OUTPUT
4646
if [[ $result =~ "Threshold exceeded" ]]; then

.github/workflows/pr-bench.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ jobs:
6868
run: pnpm run install:scripts
6969

7070
- name: 🚀 Run specified benchmark
71-
run: cd scripts && PR_NUMBER=${{ inputs.prNumber }} GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} pnpm start ${{ inputs.product }}
71+
run: cd scripts && PR_NUMBER=${{ inputs.prNumber }} GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} pnpm start:pr ${{ inputs.product }}
7272

7373
- id: print-results
7474
name: Print results
7575
run: |
76-
result=$(cd scripts && node ./dist/compare.js ${{ inputs.product }})
76+
result=$(cd scripts && pnpm compare ${{ inputs.product }})
7777
echo "$result"
7878
echo "diff-result=${result//$'\n'/'@@'}" >> $GITHUB_OUTPUT
7979
if [[ $result =~ "Threshold exceeded" ]]; then

scripts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"version": "0.1.0",
44
"scripts": {
55
"start": "npm run build && node ./dist/main.js",
6+
"start:pr": "npm run build && node ./dist/pr-bench.js",
7+
"compare": "npm run build && node ./dist/compare.js",
68
"dev": "tsc --watch",
79
"build": "rm -rf ./dist && tsc"
810
},

scripts/src/compare.ts

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import axios from 'axios';
22
import { readJson } from 'fs-extra';
3-
import { DefaultBenchCase, getCommitLink, getMetricsPath } from './shared';
3+
import {
4+
DefaultBenchCase,
5+
ValidMetricsForCase,
6+
getCommitLink,
7+
getMetricsPath,
8+
} from './shared';
49

510
const productName = process.argv[2] || 'MODERNJS_FRAMEWORK';
611

@@ -30,6 +35,7 @@ function formatValue(value: number, property: string) {
3035
function generateTable(
3136
base: Record<string, number>,
3237
current: Record<string, number>,
38+
caseName: string,
3339
) {
3440
const overThresholdTags: string[] = [];
3541
const properties = Object.keys(base);
@@ -58,55 +64,65 @@ function generateTable(
5864
).padEnd(10)} | ${formatValue(current[property], property).padEnd(
5965
10,
6066
)} | ${formattedPercent.padEnd(10)} |`;
61-
table.push(row);
6267

63-
if (percent > 5 && !property.includes('InstallTime')) {
64-
overThresholdTags.push(property);
68+
if (
69+
ValidMetricsForCase[
70+
caseName as keyof typeof ValidMetricsForCase
71+
]?.includes(property) ||
72+
!ValidMetricsForCase[caseName as keyof typeof ValidMetricsForCase]
73+
) {
74+
table.push(row);
75+
if (percent > 5 && !property.includes('InstallTime')) {
76+
overThresholdTags.push(property);
77+
}
6578
}
6679
});
6780

6881
console.log('');
6982
console.log(table.join('\n'));
83+
console.log('');
7084

7185
return overThresholdTags;
7286
}
7387

7488
export async function compare(productName: string) {
75-
const caseName =
76-
process.argv[3] ||
77-
DefaultBenchCase[productName as keyof typeof DefaultBenchCase];
78-
const { jsonPath, remoteURL } = await getMetricsPath(productName, caseName);
79-
80-
const allMetrics =
81-
process.env.MONITOR === '1'
82-
? (await axios.get(remoteURL)).data
83-
: await readJson(jsonPath);
84-
85-
let arr = Object.keys(allMetrics).map(key => {
86-
return { key: key, value: allMetrics[key] };
87-
});
88-
89-
arr.sort((a, b) => a.value.time - b.value.time);
90-
91-
const currentKey = arr[arr.length - 1].key;
92-
const baseKey = arr[arr.length - 2].key;
93-
const current = allMetrics[currentKey as any];
94-
const base = allMetrics[baseKey as any];
95-
const baseCommitLink = getCommitLink(productName, baseKey);
96-
const currentCommitLink = getCommitLink(productName, currentKey);
97-
98-
console.log(`case: ${caseName}`);
99-
console.log('base: ', baseCommitLink);
100-
console.log('current: ', currentCommitLink);
101-
102-
const overThresholdTags = generateTable(base, current);
103-
104-
if (overThresholdTags.length > 0) {
105-
console.log('');
106-
console.log(
107-
`Threshold exceeded in ${caseName}: `,
108-
JSON.stringify(overThresholdTags),
109-
);
89+
const cases = DefaultBenchCase[productName as keyof typeof DefaultBenchCase];
90+
91+
for (const caseName of cases) {
92+
const { jsonPath, remoteURL } = await getMetricsPath(productName, caseName);
93+
94+
const allMetrics =
95+
process.env.MONITOR === '1'
96+
? (await axios.get(remoteURL)).data
97+
: await readJson(jsonPath);
98+
99+
let arr = Object.keys(allMetrics).map(key => {
100+
return { key: key, value: allMetrics[key] };
101+
});
102+
103+
arr.sort((a, b) => a.value.time - b.value.time);
104+
105+
const currentKey = arr[arr.length - 1].key;
106+
const baseKey = arr[arr.length - 2].key;
107+
const current = allMetrics[currentKey as any];
108+
const base = allMetrics[baseKey as any];
109+
const baseCommitLink = getCommitLink(productName, baseKey);
110+
const currentCommitLink = getCommitLink(productName, currentKey);
111+
112+
console.log(`case: ${caseName}`);
113+
console.log('base: ', baseCommitLink);
114+
console.log('current: ', currentCommitLink);
115+
116+
const overThresholdTags = generateTable(base, current, caseName);
117+
118+
if (overThresholdTags.length > 0) {
119+
console.log('');
120+
console.log(
121+
`Threshold exceeded in ${caseName}: `,
122+
JSON.stringify(overThresholdTags),
123+
);
124+
console.log('');
125+
}
110126
}
111127
}
112128

scripts/src/main.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import logger from 'consola';
22
import { dev } from './runners/dev';
33
import { build } from './runners/build';
4-
import {
5-
DefaultBenchCase,
6-
cloneRepo,
7-
getDataPath,
8-
mergeMetrics,
9-
} from './shared';
4+
import { cloneRepo, getDataPath, mergeMetrics } from './shared';
105
import { remove } from 'fs-extra';
116
import { yarnInstall } from './runners/yarn-install';
127

13-
const productName = process.argv[2] || 'MODERNJS_FRAMEWORK';
14-
const caseName =
15-
process.argv[3] ||
16-
DefaultBenchCase[productName as keyof typeof DefaultBenchCase];
8+
const productName = process.argv[2];
9+
const caseName = process.argv[3];
1710

1811
async function main() {
1912
const dataPath = getDataPath(productName);

scripts/src/pr-bench.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { dev } from './runners/dev';
2+
import { build } from './runners/build';
3+
import {
4+
DefaultBenchCase,
5+
cloneRepo,
6+
getDataPath,
7+
mergeMetrics,
8+
} from './shared';
9+
import { remove } from 'fs-extra';
10+
import { yarnInstall } from './runners/yarn-install';
11+
12+
const productName = process.argv[2];
13+
14+
async function prBench() {
15+
const dataPath = getDataPath(productName);
16+
await remove(dataPath);
17+
18+
const cases = DefaultBenchCase[productName as keyof typeof DefaultBenchCase];
19+
20+
for (const caseName of cases) {
21+
await cloneRepo(productName, caseName);
22+
23+
process.env.PRODUCT_NAME = productName;
24+
process.env.CASE_NAME = caseName;
25+
26+
if (productName !== 'MODERNJS_MODULE') {
27+
await dev(productName, caseName);
28+
}
29+
30+
await build(productName, caseName);
31+
32+
try {
33+
await yarnInstall(productName, caseName);
34+
} catch (err) {
35+
console.log('failed to collect install size metrics:', err);
36+
}
37+
38+
await mergeMetrics(productName, caseName);
39+
}
40+
}
41+
42+
prBench();

scripts/src/shared/product.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ export enum REPO_NAME {
1515
}
1616

1717
export const DefaultBenchCase = {
18-
MODERNJS_FRAMEWORK: 'app-arco-pro-rspack',
19-
MODERNJS_MODULE: 'module-library',
20-
RSPRESS: 'rspress-website',
21-
RSBUILD: 'rsbuild-react',
18+
MODERNJS_FRAMEWORK: ['app-arco-pro-rspack'],
19+
MODERNJS_MODULE: ['module-library'],
20+
RSPRESS: ['rspress-website'],
21+
RSBUILD: ['rsbuild-react', 'rsbuild-arco-pro'],
22+
};
23+
24+
export const ValidMetricsForCase = {
25+
'rsbuild-react': [
26+
'coldInstallTime',
27+
'hotInstallTime',
28+
'dependenciesCount',
29+
'installSize',
30+
],
31+
'rsbuild-arco-pro': [
32+
'devColdBootTime',
33+
'buildColdBootTime',
34+
'minifiedBundleSize',
35+
'gzippedBundleSize',
36+
],
2237
};

0 commit comments

Comments
 (0)