Skip to content

Commit 777f42f

Browse files
authored
👷 [Performance script] fix regression on size increase warnings (#3845)
* ✅add a test on performance PR message * 🐛remove number type checks
1 parent ddb4cb4 commit 777f42f

File tree

2 files changed

+90
-9
lines changed

2 files changed

+90
-9
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import assert from 'node:assert/strict'
2+
import { describe, it } from 'node:test'
3+
import { createMessage } from './reportAsAPrComment.ts'
4+
5+
void describe('reportAsAPrComment', () => {
6+
void describe('createMessage', () => {
7+
const TEST_BUNDLE = 'test'
8+
const PR_NUMBER = 123
9+
10+
const BASE_BUNDLE_SIZES = [{ name: TEST_BUNDLE, value: 100 }]
11+
const MEMORY_BASE_PERFORMANCE = [{ name: TEST_BUNDLE, value: 100 }]
12+
const CPU_BASE_PERFORMANCE = [{ name: TEST_BUNDLE, value: 100 }]
13+
14+
const MEMORY_LOCAL_PERFORMANCE = [{ testProperty: TEST_BUNDLE, sdkMemoryBytes: 101, sdkMemoryPercentage: 10 }]
15+
const CPU_LOCAL_PERFORMANCE = [{ name: TEST_BUNDLE, value: 101 }]
16+
17+
void it('should generate a report with performance results', () => {
18+
const localBundleSizes = { test: 101 }
19+
20+
const message = createMessage(
21+
BASE_BUNDLE_SIZES,
22+
localBundleSizes,
23+
MEMORY_BASE_PERFORMANCE,
24+
MEMORY_LOCAL_PERFORMANCE,
25+
CPU_BASE_PERFORMANCE,
26+
CPU_LOCAL_PERFORMANCE,
27+
PR_NUMBER
28+
)
29+
30+
assert.equal(
31+
message,
32+
`| 📦 Bundle Name | Base Size | Local Size | 𝚫 | 𝚫% | Status |
33+
| --- | --- | --- | --- | --- | --- |
34+
| Test | 100 B | 101 B | 1 B | +1.00% | ✅ |
35+
</details>
36+
37+
<details>
38+
<summary>🚀 CPU Performance</summary>
39+
40+
| Action Name | Base Average Cpu Time (ms) | Local Average Cpu Time (ms) | 𝚫 |
41+
| --- | --- | --- | --- |
42+
| test | 100.000 | 101.000 | 1.000 |
43+
44+
</details>
45+
46+
<details>
47+
<summary>🧠 Memory Performance</summary>
48+
49+
| Action Name | Base Consumption Memory (bytes) | Local Consumption Memory (bytes) | 𝚫 (bytes) |
50+
| --- | --- | --- | --- |
51+
| test | 100 B | 101 B | 1 B |
52+
53+
</details>
54+
55+
🔗 [RealWorld](https://datadoghq.dev/browser-sdk-test-playground/realworld-scenario/?prNumber=123)
56+
57+
`
58+
)
59+
})
60+
61+
void it('should add a warning when the size increase is above the threshold', () => {
62+
const localBundleSizes = { test: 150 }
63+
64+
const message = createMessage(
65+
BASE_BUNDLE_SIZES,
66+
localBundleSizes,
67+
MEMORY_BASE_PERFORMANCE,
68+
MEMORY_LOCAL_PERFORMANCE,
69+
CPU_BASE_PERFORMANCE,
70+
CPU_LOCAL_PERFORMANCE,
71+
PR_NUMBER
72+
)
73+
74+
assertContains(message, '| Test | 100 B | 150 B | 50 B | +50.00% | ⚠️ |')
75+
assertContains(message, '⚠️ The increase is particularly high and exceeds 5%. Please check the changes.')
76+
})
77+
})
78+
})
79+
80+
function assertContains(actual: string, expected: string) {
81+
assert.ok(
82+
actual.includes(expected),
83+
['Expected string to contain:', ` expected: "${expected}"`, ` actual: "${actual}"`].join('\n')
84+
)
85+
}

scripts/performance/lib/reportAsAPrComment.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,8 @@ export async function reportAsPrComment(
5252
const cpuBasePerformance = await fetchPerformanceMetrics('cpu', testNames, lastCommonCommit)
5353
const cpuLocalPerformance = await fetchPerformanceMetrics('cpu', testNames, LOCAL_COMMIT_SHA || '')
5454
const memoryBasePerformance = await fetchPerformanceMetrics('memory', testNames, lastCommonCommit)
55-
const differenceBundle = compare(baseBundleSizes, localBundleSizes)
56-
const differenceCpu = compare(cpuBasePerformance, cpuLocalPerformance)
5755
const commentId = await retrieveExistingCommentId(pr.number)
5856
const message = createMessage(
59-
differenceBundle,
60-
differenceCpu,
6157
baseBundleSizes,
6258
localBundleSizes,
6359
memoryBasePerformance,
@@ -129,9 +125,7 @@ async function updateOrAddComment(message: string, prNumber: number, commentId:
129125
})
130126
}
131127

132-
function createMessage(
133-
differenceBundle: PerformanceDifference[],
134-
differenceCpu: PerformanceDifference[],
128+
export function createMessage(
135129
baseBundleSizes: PerformanceMetric[],
136130
localBundleSizes: BundleSizes,
137131
memoryBasePerformance: PerformanceMetric[],
@@ -140,14 +134,16 @@ function createMessage(
140134
cpuLocalPerformance: PerformanceMetric[],
141135
prNumber: number
142136
): string {
137+
const differenceBundle = compare(baseBundleSizes, localBundleSizes)
138+
const differenceCpu = compare(cpuBasePerformance, cpuLocalPerformance)
143139
let highIncreaseDetected = false
144140
const bundleRows = differenceBundle.map((diff, index) => {
145141
const baseSize = formatSize(baseBundleSizes[index].value)
146142
const localSize = formatSize(localBundleSizes[diff.name])
147143
const diffSize = formatSize(diff.change)
148-
const sign = typeof diff.percentageChange === 'number' && diff.percentageChange > 0 ? '+' : ''
144+
const sign = (diff.percentageChange as number) > 0 ? '+' : ''
149145
let status = '✅'
150-
if (typeof diff.percentageChange === 'number' && diff.percentageChange > SIZE_INCREASE_THRESHOLD) {
146+
if ((diff.percentageChange as number) > SIZE_INCREASE_THRESHOLD) {
151147
status = '⚠️'
152148
highIncreaseDetected = true
153149
}

0 commit comments

Comments
 (0)