-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
WIP: Add typography performance tests #8918
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
Draft
shakeelmohamed
wants to merge
3
commits into
processing:main
Choose a base branch
from
shakeelmohamed:bench-typography
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.
+143
−0
Draft
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <!-- Guide to writing performance tests for p5.js source code. --> | ||
|
|
||
| # Performance Testing | ||
|
|
||
| Performance testing is an essential part of measuring how fasst isolated code paths take to execute under a known range of conditions. | ||
|
|
||
| Implementation notes: | ||
|
|
||
| - All pre-existing benchmarks included the full p5 instance setup which adds ~50ms to each test. The typography performance tests show how to isolate performance testing away from setup overhead. | ||
| - The `textToPoints() single word, with render` test draws the calculated points, others just run `textToPoints()`. | ||
|
|
||
| ## Typography Tests | ||
|
|
||
| To isolate typography tests, run the following: | ||
|
|
||
| ```shell | ||
| npx vitest bench test/bench/typography.bench.js --reporter=verbose | ||
| ``` |
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,125 @@ | ||
| import { bench, describe } from "vitest"; | ||
| import p5 from "../../src/app"; | ||
| import { WEBGL, WEBGPU } from "../../src/core/constants"; | ||
|
|
||
| const fontFile = "../../test/manual-test-examples/type/font/LiberationSans-Bold.ttf"; | ||
|
|
||
| const strs = { | ||
| single: "Performance", | ||
| ten: "Performance testing 10 words at a time is exhaustive! Right?", | ||
| paragraph: Array.from({ length: 10 }, (_, i) => | ||
| `${i === 0 ? "\t": ""}Performance is vital in all aspects of text rendering, even 10 lines at a time. This is line ${i + 1} of 10.` | ||
| ).join("\n") // This will hit around 15fps, 21275 points | ||
| }; | ||
|
|
||
| // Parameterizing test cases by function ensures consitency in test parameters across all renderers. | ||
| // Future tests should follow a similar format (e.g. TO_CONTOURS_CASES, etc) | ||
| const TO_POINTS_CASES = [ | ||
| {label: "textToPoints() single word", str: strs.single, textSize: 20, sampleFactor: 0.5, points: 317, variance: 5, render: false}, | ||
| {label: "textToPoints() single word, 150pt", str: strs.single, textSize: 150, sampleFactor: 0.5, points: 2336, variance: 50, render: false}, | ||
| {label: "textToPoints() single word, with render", str: strs.single, textSize: 20, sampleFactor: 0.5, points: 317, variance: 5, render: true}, | ||
| {label: "textToPoints() 10 words", str: strs.ten, textSize: 20, sampleFactor: 0.5, points: 1453, variance: 5, render: false}, | ||
| {label: "textToPoints() paragraph", str: strs.paragraph, textSize: 20, sampleFactor: 0.5, points: 21275, variance: 50, render: false}, | ||
| ]; | ||
|
|
||
| async function bootstrap(w = 400, h = 400, renderer = undefined) { | ||
| var myp5; | ||
| var font; | ||
| new p5(function (p) { | ||
| p.setup = async function () { | ||
| myp5 = p; | ||
| font = await p.loadFont(fontFile); | ||
| p.createCanvas(w, h, renderer); | ||
| }; | ||
| }); | ||
| await vi.waitFor(() => { | ||
| if (myp5 === undefined) throw new Error("not ready"); | ||
| }); | ||
| return { myp5, font }; | ||
| } | ||
|
|
||
| function drawPoints(myp5, points) { | ||
| for (let point of points) { | ||
| myp5.point(point.x, point.y); | ||
| } | ||
| } | ||
|
|
||
| const options = { iterations: 20, time: 500 }; | ||
|
|
||
| describe("Typography: bench 2D", function() { | ||
| var myp5, font; | ||
|
|
||
| for (let testCase of TO_POINTS_CASES) { | ||
| bench( | ||
| testCase.label, | ||
| async () => { | ||
| const points = font.textToPoints(testCase.str, 10, 20, { sampleFactor: testCase.sampleFactor }); | ||
| assert.closeTo(points.length, testCase.points, testCase.variance); | ||
| if (testCase.render) { | ||
| drawPoints(myp5, points); | ||
| } | ||
| }, | ||
| { | ||
| ...options, | ||
| setup: async () => { | ||
| ({myp5, font} = await bootstrap()); | ||
| myp5.textSize(testCase.textSize); | ||
| }, | ||
| teardown: () => myp5.remove() | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| describe("Typography: bench WebGL", function() { | ||
| var myp5, font; | ||
|
|
||
| for (let testCase of TO_POINTS_CASES) { | ||
| bench( | ||
| testCase.label, | ||
| async () => { | ||
| const points = font.textToPoints(testCase.str, 10, 20, { sampleFactor: testCase.sampleFactor }); | ||
| assert.closeTo(points.length, testCase.points, testCase.variance); | ||
| if (testCase.render) { | ||
| drawPoints(myp5, points); | ||
| } | ||
| }, | ||
| { | ||
| ...options, | ||
| setup: async () => { | ||
| ({myp5, font} = await bootstrap(400, 400, WEBGL)); | ||
| myp5.textSize(testCase.textSize); | ||
| }, | ||
| teardown: () => myp5.remove() | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| describe("Typography: bench WebGPU", function() { | ||
| var myp5, font; | ||
|
|
||
| for (let testCase of TO_POINTS_CASES) { | ||
| bench( | ||
| testCase.label, | ||
| async () => { | ||
| const points = font.textToPoints(testCase.str, 10, 20, { sampleFactor: testCase.sampleFactor }); | ||
| assert.closeTo(points.length, testCase.points, testCase.variance); | ||
| if (testCase.render) { | ||
| drawPoints(myp5, points); | ||
| } | ||
| }, | ||
| { | ||
| ...options, | ||
| setup: async () => { | ||
| ({myp5, font} = await bootstrap(400, 400, WEBGPU)); | ||
| myp5.textSize(testCase.textSize); | ||
| }, | ||
| teardown: () => myp5.remove() | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| }); | ||
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.