Skip to content

Commit 8eda63c

Browse files
Merge pull request #21316 from johanrd/bench/glimmer-syntax
add mitata harness for precompile (parse/normalize/precompile)
2 parents 1886f8d + 47e2535 commit 8eda63c

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

bin/precompile.bench.mjs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Mitata benchmark for `@glimmer/syntax` parse (`preprocess`), normalize
3+
* (ASTv1 → ASTv2 — where the loc-conversion hot path lives), and full
4+
* `precompile()` (via `ember-template-compiler`, which inlines
5+
* `@glimmer/compiler`).
6+
*
7+
* Run:
8+
* pnpm build # produces the dist artifacts this bench imports
9+
* pnpm bench:precompile
10+
*
11+
* To compare branches, check out each branch, build, run the bench, and diff
12+
* the ms/iter numbers.
13+
*
14+
* Sizes:
15+
* small — ~1.5k chars (route-template fragment)
16+
* medium — small × 3 (~4.5k chars)
17+
* large — small × 22 (~33k chars, scale of the largest real route
18+
* templates, e.g. Discourse's admin-user/index.gjs)
19+
*/
20+
21+
import { bench, do_not_optimize as doNotOptimize, run } from 'mitata';
22+
23+
/* eslint n/no-missing-import: "off" -- dist/ is built by `pnpm build`; may not exist at lint time */
24+
import { normalize, preprocess, src } from '../packages/@glimmer/syntax/dist/es/index.js';
25+
import { precompile } from '../dist/prod/packages/ember-template-compiler/index.js';
26+
27+
const SMALL = `<div class='user-profile {{if this.isPremium "premium"}}'>
28+
<header class='profile-header'>
29+
<img src={{this.avatarUrl}} alt={{this.username}} class='avatar' />
30+
<h2>{{this.displayName}}</h2>
31+
<p class='bio'>{{this.bio}}</p>
32+
{{#if this.isOwnProfile}}
33+
<button {{on 'click' this.editProfile}}>Edit Profile</button>
34+
{{/if}}
35+
</header>
36+
<nav class='profile-tabs'>
37+
{{#each this.tabs as |tab|}}
38+
<button
39+
class='tab {{if (eq tab.id this.activeTab) "active"}}'
40+
{{on 'click' (fn this.setTab tab.id)}}
41+
>
42+
{{tab.label}}{{#if tab.count}}<span class='count'>{{tab.count}}</span>{{/if}}
43+
</button>
44+
{{/each}}
45+
</nav>
46+
<section class='profile-content'>
47+
{{#if (eq this.activeTab 'posts')}}
48+
{{#each this.posts as |post|}}
49+
<article class='post-card'>
50+
<h3>{{post.title}}</h3><p>{{post.excerpt}}</p>
51+
<footer><time>{{post.createdAt}}</time><span>{{post.views}} views</span></footer>
52+
</article>
53+
{{else}}
54+
<p class='empty-state'>No posts yet.</p>
55+
{{/each}}
56+
{{else if (eq this.activeTab 'followers')}}
57+
{{#each this.followers as |follower|}}
58+
<div class='follower-card'>
59+
<img src={{follower.avatar}} alt={{follower.name}} />
60+
<span>{{follower.name}}</span>
61+
<button {{on 'click' (fn this.followUser follower.id)}}>
62+
{{if follower.isFollowing 'Unfollow' 'Follow'}}
63+
</button>
64+
</div>
65+
{{/each}}
66+
{{/if}}
67+
</section>
68+
</div>
69+
`;
70+
71+
const FIXTURES = {
72+
small: SMALL,
73+
medium: SMALL.repeat(3),
74+
large: SMALL.repeat(22),
75+
};
76+
77+
for (const [size, source] of Object.entries(FIXTURES)) {
78+
const chars = source.length;
79+
bench(`parse ${size} (${chars}c)`, () => doNotOptimize(preprocess(source)));
80+
bench(`normalize ${size} (${chars}c)`, () => doNotOptimize(normalize(new src.Source(source))));
81+
bench(`precompile ${size} (${chars}c)`, () => doNotOptimize(precompile(source)));
82+
}
83+
84+
await run({ throw: true });

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"scripts": {
3838
"actions-up": "pnpm dlx actions-up",
3939
"bench": "node ./bin/benchmark.mjs",
40+
"bench:precompile": "node --expose-gc bin/precompile.bench.mjs",
4041
"build:js": "rollup --config",
4142
"build:types": "node types/publish.mjs",
4243
"build": "npm-run-all build:*",
@@ -122,6 +123,7 @@
122123
"glob": "^8.0.3",
123124
"globals": "^16.0.0",
124125
"kill-port-process": "^3.2.1",
126+
"mitata": "^1.0.34",
125127
"mocha": "^11.0.0",
126128
"npm-run-all2": "^8.0.0",
127129
"prettier": "^3.5.3",

pnpm-lock.yaml

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)