-
Notifications
You must be signed in to change notification settings - Fork 54
add memory benchmarking #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avivkeller
wants to merge
1
commit into
main
Choose a base branch
from
membench
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
|
|
||
| import { BASE, BENCHMARK_FILE, HEAD } from '../constants.mjs'; | ||
|
|
||
| const UNITS = ['B', 'KB', 'MB', 'GB']; | ||
|
|
||
| const formatBytes = bytes => { | ||
| if (!bytes) { | ||
| return '0 B'; | ||
| } | ||
|
|
||
| const unit = Math.min( | ||
| Math.floor(Math.log(Math.abs(bytes)) / Math.log(1024)), | ||
| UNITS.length - 1 | ||
| ); | ||
| return `${(bytes / Math.pow(1024, unit)).toFixed(2)} ${UNITS[unit]}`; | ||
| }; | ||
|
|
||
| const formatSeconds = seconds => | ||
| Math.abs(seconds) < 1 | ||
| ? `${(seconds * 1_000).toFixed(2)} ms` | ||
| : `${seconds.toFixed(2)} s`; | ||
|
|
||
| const formatPercent = (base, diff) => { | ||
| if (base === 0) { | ||
| return 'n/a'; | ||
| } | ||
|
|
||
| const percent = (diff / base) * 100; | ||
| return `${percent > 0 ? '+' : ''}${percent.toFixed(2)}%`; | ||
| }; | ||
|
|
||
| const formatDiff = (base, head, formatter) => { | ||
| const diff = head - base; | ||
| const value = `${diff > 0 ? '+' : ''}${formatter(diff)}`; | ||
| return `${value} (${formatPercent(base, diff)})`; | ||
| }; | ||
|
|
||
| const readBenchmark = async directory => { | ||
| try { | ||
| return JSON.parse( | ||
| await readFile(path.join(directory, BENCHMARK_FILE), 'utf8') | ||
| ); | ||
| } catch (error) { | ||
| if (error.code === 'ENOENT') { | ||
| return null; | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const METRICS = [ | ||
| { | ||
| label: 'Elapsed time', | ||
| value: benchmark => benchmark.elapsedSeconds, | ||
| format: formatSeconds, | ||
| }, | ||
| { | ||
| label: 'User CPU time', | ||
| value: benchmark => benchmark.userCpuSeconds, | ||
| format: formatSeconds, | ||
| }, | ||
| { | ||
| label: 'System CPU time', | ||
| value: benchmark => benchmark.systemCpuSeconds, | ||
| format: formatSeconds, | ||
| }, | ||
| { | ||
| label: 'Peak resident memory', | ||
| value: benchmark => benchmark.maxRssKiB * 1024, | ||
| format: formatBytes, | ||
| }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Builds a Markdown comparison of generation performance. | ||
| * | ||
| * Missing benchmark files are expected while artifacts created before | ||
| * performance measurement are still being used as the comparison base. | ||
| * | ||
| * @param {string} baseDirectory - Base artifact directory | ||
| * @param {string} headDirectory - Head artifact directory | ||
| * @returns {Promise<string>} Markdown table, or an empty string | ||
| */ | ||
| export const comparePerformance = async ( | ||
| baseDirectory = BASE, | ||
| headDirectory = HEAD | ||
| ) => { | ||
| const [base, head] = await Promise.all( | ||
| [baseDirectory, headDirectory].map(readBenchmark) | ||
| ); | ||
|
|
||
| if (!base || !head) { | ||
| return ''; | ||
| } | ||
|
|
||
| const rows = METRICS.map(({ label, value, format }) => { | ||
| const baseValue = value(base); | ||
| const headValue = value(head); | ||
|
|
||
| if (!Number.isFinite(baseValue) || !Number.isFinite(headValue)) { | ||
| throw new TypeError(`Invalid ${label.toLowerCase()} benchmark value`); | ||
| } | ||
|
|
||
| return `| ${label} | ${format(baseValue)} | ${format(headValue)} | ${formatDiff(baseValue, headValue, format)} |`; | ||
| }); | ||
|
|
||
| return [ | ||
| '### Performance', | ||
| '| Metric | Base | Head | Diff |', | ||
| '|-|-|-|-|', | ||
| ...rows, | ||
| ].join('\n'); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { execFile } from 'node:child_process'; | ||
| import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { test } from 'node:test'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { promisify } from 'node:util'; | ||
|
|
||
| import { comparePerformance } from '../../scripts/comparators/performance.mjs'; | ||
|
|
||
| const benchmark = { | ||
| elapsedSeconds: 2, | ||
| userCpuSeconds: 1, | ||
| systemCpuSeconds: 0.5, | ||
| maxRssKiB: 1024, | ||
| }; | ||
|
|
||
| const executeFile = promisify(execFile); | ||
| const comparatorDirectory = fileURLToPath( | ||
| new URL('../../scripts/comparators/', import.meta.url) | ||
| ); | ||
|
|
||
| const createDirectories = async t => { | ||
| const root = await mkdtemp(path.join(tmpdir(), 'doc-kit-')); | ||
| const base = path.join(root, 'base'); | ||
| const head = path.join(root, 'head'); | ||
|
|
||
| await Promise.all([ | ||
| mkdir(base, { recursive: true }), | ||
| mkdir(head, { recursive: true }), | ||
| ]); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
|
|
||
| return { base, head }; | ||
| }; | ||
|
|
||
| const writeBenchmark = (directory, value = benchmark) => | ||
| writeFile( | ||
| path.join(directory, 'benchmark.json'), | ||
| JSON.stringify(value), | ||
| 'utf8' | ||
| ); | ||
|
|
||
| const runComparator = async (name, base, head) => { | ||
| const { stdout } = await executeFile( | ||
| process.execPath, | ||
| [path.join(comparatorDirectory, `${name}.mjs`)], | ||
| { | ||
| env: { ...process.env, BASE: base, HEAD: head, GENERATOR: 'test' }, | ||
| } | ||
| ); | ||
|
|
||
| return stdout; | ||
| }; | ||
|
|
||
| test('comparePerformance formats benchmark differences', async t => { | ||
| const { base, head } = await createDirectories(t); | ||
|
|
||
| await Promise.all([ | ||
| writeBenchmark(base), | ||
| writeBenchmark(head, { | ||
| elapsedSeconds: 3, | ||
| userCpuSeconds: 0.75, | ||
| systemCpuSeconds: 0.5, | ||
| maxRssKiB: 1536, | ||
| }), | ||
| ]); | ||
|
|
||
| const result = await comparePerformance(base, head); | ||
|
|
||
| assert.match( | ||
| result, | ||
| /Elapsed time \| 2\.00 s \| 3\.00 s \| \+1\.00 s \(\+50\.00%\)/ | ||
| ); | ||
| assert.match( | ||
| result, | ||
| /User CPU time \| 1\.00 s \| 750\.00 ms \| -250\.00 ms \(-25\.00%\)/ | ||
| ); | ||
| assert.match( | ||
| result, | ||
| /Peak resident memory \| 1\.00 MB \| 1\.50 MB \| \+512\.00 KB \(\+50\.00%\)/ | ||
| ); | ||
| }); | ||
|
|
||
| test('comparePerformance omits results when an artifact has no benchmark', async t => { | ||
| const { base, head } = await createDirectories(t); | ||
|
|
||
| await writeBenchmark(head); | ||
|
|
||
| assert.equal(await comparePerformance(base, head), ''); | ||
| }); | ||
|
|
||
| test('comparePerformance rejects invalid benchmark values', async t => { | ||
| const { base, head } = await createDirectories(t); | ||
|
|
||
| await Promise.all([ | ||
| writeBenchmark(base), | ||
| writeBenchmark(head, { ...benchmark, maxRssKiB: undefined }), | ||
| ]); | ||
|
|
||
| await assert.rejects( | ||
| comparePerformance(base, head), | ||
| /Invalid peak resident memory benchmark value/ | ||
| ); | ||
| }); | ||
|
|
||
| test('file-size comparator combines output and performance results', async t => { | ||
| const { base, head } = await createDirectories(t); | ||
|
|
||
| await Promise.all([ | ||
| writeFile(path.join(base, 'result.txt'), 'base', 'utf8'), | ||
| writeFile(path.join(head, 'result.txt'), 'a larger result', 'utf8'), | ||
| writeBenchmark(base), | ||
| writeBenchmark(head, { ...benchmark, elapsedSeconds: 3 }), | ||
| ]); | ||
|
|
||
| const result = await runComparator('file-size', base, head); | ||
|
|
||
| assert.equal(result.match(/## `test` Generator/g)?.length, 1); | ||
| assert.match(result, /### Output size/); | ||
| assert.match(result, /`result\.txt`/); | ||
| assert.match(result, /### Performance/); | ||
| assert.doesNotMatch(result, /benchmark\.json/); | ||
| }); | ||
|
|
||
| test('object comparator treats benchmark data as metadata', async t => { | ||
| const { base, head } = await createDirectories(t); | ||
|
|
||
| await Promise.all([ | ||
| writeFile(path.join(base, 'result.json'), '{"value":true}', 'utf8'), | ||
| writeFile(path.join(head, 'result.json'), '{"value":true}', 'utf8'), | ||
| writeBenchmark(base), | ||
| writeBenchmark(head, { ...benchmark, maxRssKiB: 2048 }), | ||
| ]); | ||
|
|
||
| const result = await runComparator('object-assertion', base, head); | ||
|
|
||
| assert.equal(result.match(/## `test` Generator/g)?.length, 1); | ||
| assert.doesNotMatch(result, /### Output\n/); | ||
| assert.match(result, /### Performance/); | ||
| assert.doesNotMatch(result, /benchmark\.json/); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.