Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions scripts/performance/lib/reportAsAPrComment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { createMessage } from './reportAsAPrComment.ts'

void describe('reportAsAPrComment', () => {
void describe('createMessage', () => {
const TEST_BUNDLE = 'test'
const PR_NUMBER = 123

const BASE_BUNDLE_SIZES = [{ name: TEST_BUNDLE, value: 100 }]
const MEMORY_BASE_PERFORMANCE = [{ name: TEST_BUNDLE, value: 100 }]
const CPU_BASE_PERFORMANCE = [{ name: TEST_BUNDLE, value: 100 }]

const MEMORY_LOCAL_PERFORMANCE = [{ testProperty: TEST_BUNDLE, sdkMemoryBytes: 101, sdkMemoryPercentage: 10 }]
const CPU_LOCAL_PERFORMANCE = [{ name: TEST_BUNDLE, value: 101 }]

void it('should generate a report with performance results', () => {
const localBundleSizes = { test: 101 }

const message = createMessage(
BASE_BUNDLE_SIZES,
localBundleSizes,
MEMORY_BASE_PERFORMANCE,
MEMORY_LOCAL_PERFORMANCE,
CPU_BASE_PERFORMANCE,
CPU_LOCAL_PERFORMANCE,
PR_NUMBER
)

assert.equal(
message,
`| 📦 Bundle Name | Base Size | Local Size | 𝚫 | 𝚫% | Status |
| --- | --- | --- | --- | --- | --- |
| Test | 100 B | 101 B | 1 B | +1.00% | ✅ |
</details>

<details>
<summary>🚀 CPU Performance</summary>

| Action Name | Base Average Cpu Time (ms) | Local Average Cpu Time (ms) | 𝚫 |
| --- | --- | --- | --- |
| test | 100.000 | 101.000 | 1.000 |

</details>

<details>
<summary>🧠 Memory Performance</summary>

| Action Name | Base Consumption Memory (bytes) | Local Consumption Memory (bytes) | 𝚫 (bytes) |
| --- | --- | --- | --- |
| test | 100 B | 101 B | 1 B |

</details>

🔗 [RealWorld](https://datadoghq.dev/browser-sdk-test-playground/realworld-scenario/?prNumber=123)

`
)
})

void it('should add a warning when the size increase is above the threshold', () => {
const localBundleSizes = { test: 150 }

const message = createMessage(
BASE_BUNDLE_SIZES,
localBundleSizes,
MEMORY_BASE_PERFORMANCE,
MEMORY_LOCAL_PERFORMANCE,
CPU_BASE_PERFORMANCE,
CPU_LOCAL_PERFORMANCE,
PR_NUMBER
)

assertContains(message, '| Test | 100 B | 150 B | 50 B | +50.00% | ⚠️ |')
assertContains(message, '⚠️ The increase is particularly high and exceeds 5%. Please check the changes.')
})
})
})

function assertContains(actual: string, expected: string) {
assert.ok(
actual.includes(expected),
['Expected string to contain:', ` expected: "${expected}"`, ` actual: "${actual}"`].join('\n')
)
}
14 changes: 5 additions & 9 deletions scripts/performance/lib/reportAsAPrComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ export async function reportAsPrComment(
const cpuBasePerformance = await fetchPerformanceMetrics('cpu', testNames, lastCommonCommit)
const cpuLocalPerformance = await fetchPerformanceMetrics('cpu', testNames, LOCAL_COMMIT_SHA || '')
const memoryBasePerformance = await fetchPerformanceMetrics('memory', testNames, lastCommonCommit)
const differenceBundle = compare(baseBundleSizes, localBundleSizes)
const differenceCpu = compare(cpuBasePerformance, cpuLocalPerformance)
const commentId = await retrieveExistingCommentId(pr.number)
const message = createMessage(
differenceBundle,
differenceCpu,
baseBundleSizes,
localBundleSizes,
memoryBasePerformance,
Expand Down Expand Up @@ -129,9 +125,7 @@ async function updateOrAddComment(message: string, prNumber: number, commentId:
})
}

function createMessage(
differenceBundle: PerformanceDifference[],
differenceCpu: PerformanceDifference[],
export function createMessage(
baseBundleSizes: PerformanceMetric[],
localBundleSizes: BundleSizes,
memoryBasePerformance: PerformanceMetric[],
Expand All @@ -140,14 +134,16 @@ function createMessage(
cpuLocalPerformance: PerformanceMetric[],
prNumber: number
): string {
const differenceBundle = compare(baseBundleSizes, localBundleSizes)
const differenceCpu = compare(cpuBasePerformance, cpuLocalPerformance)
let highIncreaseDetected = false
const bundleRows = differenceBundle.map((diff, index) => {
const baseSize = formatSize(baseBundleSizes[index].value)
const localSize = formatSize(localBundleSizes[diff.name])
const diffSize = formatSize(diff.change)
const sign = typeof diff.percentageChange === 'number' && diff.percentageChange > 0 ? '+' : ''
const sign = (diff.percentageChange as number) > 0 ? '+' : ''
let status = '✅'
if (typeof diff.percentageChange === 'number' && diff.percentageChange > SIZE_INCREASE_THRESHOLD) {
if ((diff.percentageChange as number) > SIZE_INCREASE_THRESHOLD) {
status = '⚠️'
highIncreaseDetected = true
}
Expand Down