build-start-rebuild-perf 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/.github/workflows/plan-release.yml +94 -0
  2. package/.github/workflows/publish.yml +43 -0
  3. package/.github/workflows/test.yml +33 -0
  4. package/.release-plan.json +26 -0
  5. package/.zed/debug.json +0 -0
  6. package/CHANGELOG.md +23 -0
  7. package/README.md +60 -1
  8. package/RELEASE.md +27 -0
  9. package/bin/cli.js +40 -0
  10. package/biome.json +28 -0
  11. package/lib/browser.js +133 -0
  12. package/lib/log.js +24 -0
  13. package/lib/measure.js +78 -0
  14. package/lib/program.js +34 -0
  15. package/lib/server.js +94 -0
  16. package/lib/utils.js +45 -0
  17. package/package.json +35 -3
  18. package/pnpm-workspace.yaml +3 -0
  19. package/renovate.json +4 -0
  20. package/test/cli.test.js +50 -0
  21. package/test-ember-app/.editorconfig +19 -0
  22. package/test-ember-app/.ember-cli +19 -0
  23. package/test-ember-app/.github/workflows/ci.yml +47 -0
  24. package/test-ember-app/.prettierignore +13 -0
  25. package/test-ember-app/.prettierrc.js +12 -0
  26. package/test-ember-app/.stylelintignore +5 -0
  27. package/test-ember-app/.stylelintrc.js +3 -0
  28. package/test-ember-app/.template-lintrc.js +3 -0
  29. package/test-ember-app/.watchmanconfig +3 -0
  30. package/test-ember-app/README.md +56 -0
  31. package/test-ember-app/app/app.js +17 -0
  32. package/test-ember-app/app/components/.gitkeep +0 -0
  33. package/test-ember-app/app/controllers/.gitkeep +0 -0
  34. package/test-ember-app/app/deprecation-workflow.js +24 -0
  35. package/test-ember-app/app/helpers/.gitkeep +0 -0
  36. package/test-ember-app/app/index.html +24 -0
  37. package/test-ember-app/app/models/.gitkeep +0 -0
  38. package/test-ember-app/app/router.js +9 -0
  39. package/test-ember-app/app/routes/.gitkeep +0 -0
  40. package/test-ember-app/app/styles/app.css +1 -0
  41. package/test-ember-app/app/templates/application.hbs +5 -0
  42. package/test-ember-app/config/ember-cli-update.json +18 -0
  43. package/test-ember-app/config/environment.js +46 -0
  44. package/test-ember-app/config/optional-features.json +7 -0
  45. package/test-ember-app/config/targets.js +9 -0
  46. package/test-ember-app/ember-cli-build.js +18 -0
  47. package/test-ember-app/eslint.config.mjs +122 -0
  48. package/test-ember-app/package.json +84 -0
  49. package/test-ember-app/public/robots.txt +3 -0
  50. package/test-ember-app/testem.js +21 -0
  51. package/test-ember-app/tests/helpers/index.js +42 -0
  52. package/test-ember-app/tests/index.html +39 -0
  53. package/test-ember-app/tests/integration/.gitkeep +0 -0
  54. package/test-ember-app/tests/test-helper.js +14 -0
  55. package/test-ember-app/tests/unit/.gitkeep +0 -0
  56. package/vitest.config.js +9 -0
@@ -0,0 +1,94 @@
1
+ name: Plan Release
2
+ on:
3
+ workflow_dispatch:
4
+ push:
5
+ branches:
6
+ - main
7
+ - master
8
+ pull_request_target: # This workflow has permissions on the repo, do NOT run code from PRs in this workflow. See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
9
+ types:
10
+ - labeled
11
+ - unlabeled
12
+
13
+ concurrency:
14
+ group: plan-release # only the latest one of these should ever be running
15
+ cancel-in-progress: true
16
+
17
+ jobs:
18
+ is-this-a-release:
19
+ name: "Is this a release?"
20
+ runs-on: ubuntu-latest
21
+ outputs:
22
+ command: ${{ steps.check-release.outputs.command }}
23
+
24
+ steps:
25
+ - uses: actions/checkout@v4
26
+ with:
27
+ fetch-depth: 2
28
+ ref: 'main'
29
+ # This will only cause the `is-this-a-release` job to have a "command" of `release`
30
+ # when the .release-plan.json file was changed on the last commit.
31
+ - id: check-release
32
+ run: if git diff --name-only HEAD HEAD~1 | grep -w -q ".release-plan.json"; then echo "command=release"; fi >> $GITHUB_OUTPUT
33
+
34
+ create-prepare-release-pr:
35
+ name: Create Prepare Release PR
36
+ runs-on: ubuntu-latest
37
+ timeout-minutes: 5
38
+ needs: is-this-a-release
39
+ permissions:
40
+ contents: write
41
+ issues: read
42
+ pull-requests: write
43
+ # only run on push event or workflow dispatch if plan wasn't updated (don't create a release plan when we're releasing)
44
+ # only run on labeled event if the PR has already been merged
45
+ if: ((github.event_name == 'push' || github.event_name == 'workflow_dispatch') && needs.is-this-a-release.outputs.command != 'release') || (github.event_name == 'pull_request_target' && github.event.pull_request.merged == true)
46
+
47
+ steps:
48
+ - uses: actions/checkout@v4
49
+ # We need to download lots of history so that
50
+ # github-changelog can discover what's changed since the last release
51
+ with:
52
+ fetch-depth: 0
53
+ ref: 'main'
54
+ - uses: pnpm/action-setup@v4
55
+ - uses: actions/setup-node@v4
56
+ with:
57
+ node-version: 18
58
+ cache: pnpm
59
+ - run: pnpm install --frozen-lockfile
60
+ - name: "Generate Explanation and Prep Changelogs"
61
+ id: explanation
62
+ run: |
63
+ set +e
64
+ pnpm release-plan prepare 2> >(tee -a release-plan-stderr.txt >&2)
65
+
66
+ if [ $? -ne 0 ]; then
67
+ release_plan_output=$(cat release-plan-stderr.txt)
68
+ else
69
+ release_plan_output=$(jq .description .release-plan.json -r)
70
+ rm release-plan-stderr.txt
71
+
72
+ if [ $(jq '.solution | length' .release-plan.json) -eq 1 ]; then
73
+ new_version=$(jq -r '.solution[].newVersion' .release-plan.json)
74
+ echo "new_version=v$new_version" >> $GITHUB_OUTPUT
75
+ fi
76
+ fi
77
+ echo 'text<<EOF' >> $GITHUB_OUTPUT
78
+ echo "$release_plan_output" >> $GITHUB_OUTPUT
79
+ echo 'EOF' >> $GITHUB_OUTPUT
80
+ env:
81
+ GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
82
+
83
+ - uses: peter-evans/create-pull-request@v7
84
+ with:
85
+ commit-message: "Prepare Release ${{ steps.explanation.outputs.new_version}} using 'release-plan'"
86
+ labels: "internal"
87
+ branch: release-preview
88
+ title: Prepare Release ${{ steps.explanation.outputs.new_version }}
89
+ body: |
90
+ This PR is a preview of the release that [release-plan](https://github.com/embroider-build/release-plan) has prepared. To release you should just merge this PR 👍
91
+
92
+ -----------------------------------------
93
+
94
+ ${{ steps.explanation.outputs.text }}
@@ -0,0 +1,43 @@
1
+ # For every push to the primary branch with .release-plan.json modified,
2
+ # runs release-plan.
3
+
4
+ name: Publish Stable
5
+
6
+ on:
7
+ workflow_dispatch:
8
+ push:
9
+ branches:
10
+ - main
11
+ - master
12
+ paths:
13
+ - '.release-plan.json'
14
+
15
+ concurrency:
16
+ group: publish-${{ github.head_ref || github.ref }}
17
+ cancel-in-progress: true
18
+
19
+ jobs:
20
+ publish:
21
+ name: "NPM Publish"
22
+ runs-on: ubuntu-latest
23
+ permissions:
24
+ contents: write
25
+ pull-requests: write
26
+ id-token: write
27
+ attestations: write
28
+
29
+ steps:
30
+ - uses: actions/checkout@v4
31
+ - uses: pnpm/action-setup@v4
32
+ - uses: actions/setup-node@v4
33
+ with:
34
+ node-version: 18
35
+ # This creates an .npmrc that reads the NODE_AUTH_TOKEN environment variable
36
+ registry-url: 'https://registry.npmjs.org'
37
+ cache: pnpm
38
+ - run: pnpm install --frozen-lockfile
39
+ - name: Publish to NPM
40
+ run: NPM_CONFIG_PROVENANCE=true pnpm release-plan publish
41
+ env:
42
+ GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
43
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,33 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request: {}
8
+
9
+ jobs:
10
+ lint:
11
+ name: Lint
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: pnpm/action-setup@v4
16
+ - uses: actions/setup-node@v4
17
+ with:
18
+ cache: pnpm
19
+ - run: pnpm i --frozen-lockfile
20
+ - run: pnpm run lint
21
+
22
+ test:
23
+ name: Test
24
+ runs-on: ubuntu-latest
25
+ needs: [lint]
26
+ steps:
27
+ - uses: actions/checkout@v4
28
+ - uses: pnpm/action-setup@v4
29
+ - uses: actions/setup-node@v4
30
+ with:
31
+ cache: pnpm
32
+ - run: pnpm i --frozen-lockfile
33
+ - run: pnpm run test
@@ -0,0 +1,26 @@
1
+ {
2
+ "solution": {
3
+ "build-start-rebuild-perf": {
4
+ "impact": "patch",
5
+ "oldVersion": "0.0.0",
6
+ "newVersion": "0.0.1",
7
+ "tagName": "latest",
8
+ "constraints": [
9
+ {
10
+ "impact": "patch",
11
+ "reason": "Appears in changelog section :bug: Bug Fix"
12
+ },
13
+ {
14
+ "impact": "patch",
15
+ "reason": "Appears in changelog section :memo: Documentation"
16
+ },
17
+ {
18
+ "impact": "patch",
19
+ "reason": "Appears in changelog section :house: Internal"
20
+ }
21
+ ],
22
+ "pkgJSONPath": "./package.json"
23
+ }
24
+ },
25
+ "description": "## Release (2025-08-07)\n\n* build-start-rebuild-perf 0.0.1 (patch)\n\n#### :bug: Bug Fix\n* `build-start-rebuild-perf`\n * [#12](https://github.com/mainmatter/build-start-rebuild-perf/pull/12) fix pnpm workspace ([@mansona](https://github.com/mansona))\n\n#### :memo: Documentation\n* `build-start-rebuild-perf`\n * [#8](https://github.com/mainmatter/build-start-rebuild-perf/pull/8) Improve README ([@pichfl](https://github.com/pichfl))\n\n#### :house: Internal\n* `build-start-rebuild-perf`\n * [#7](https://github.com/mainmatter/build-start-rebuild-perf/pull/7) Run tests in CI ([@pichfl](https://github.com/pichfl))\n * [#6](https://github.com/mainmatter/build-start-rebuild-perf/pull/6) reset version in package.json ([@mansona](https://github.com/mansona))\n * [#4](https://github.com/mainmatter/build-start-rebuild-perf/pull/4) start using release-plan ([@mansona](https://github.com/mansona))\n\n#### Committers: 2\n- Chris Manson ([@mansona](https://github.com/mansona))\n- Florian Pichler ([@pichfl](https://github.com/pichfl))\n"
26
+ }
File without changes
package/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ ## Release (2025-08-07)
4
+
5
+ * build-start-rebuild-perf 0.0.1 (patch)
6
+
7
+ #### :bug: Bug Fix
8
+ * `build-start-rebuild-perf`
9
+ * [#12](https://github.com/mainmatter/build-start-rebuild-perf/pull/12) fix pnpm workspace ([@mansona](https://github.com/mansona))
10
+
11
+ #### :memo: Documentation
12
+ * `build-start-rebuild-perf`
13
+ * [#8](https://github.com/mainmatter/build-start-rebuild-perf/pull/8) Improve README ([@pichfl](https://github.com/pichfl))
14
+
15
+ #### :house: Internal
16
+ * `build-start-rebuild-perf`
17
+ * [#7](https://github.com/mainmatter/build-start-rebuild-perf/pull/7) Run tests in CI ([@pichfl](https://github.com/pichfl))
18
+ * [#6](https://github.com/mainmatter/build-start-rebuild-perf/pull/6) reset version in package.json ([@mansona](https://github.com/mansona))
19
+ * [#4](https://github.com/mainmatter/build-start-rebuild-perf/pull/4) start using release-plan ([@mansona](https://github.com/mansona))
20
+
21
+ #### Committers: 2
22
+ - Chris Manson ([@mansona](https://github.com/mansona))
23
+ - Florian Pichler ([@pichfl](https://github.com/pichfl))
package/README.md CHANGED
@@ -1,2 +1,61 @@
1
1
  # build-start-rebuild-perf
2
- Measure your build, app start up, and rebuild times in one go
2
+
3
+ Measures web app performance metrics:
4
+ - Dev server startup time
5
+ - Time to first paint
6
+ - Time to app load (waiting for an element selector)
7
+ - Reload time after a file changes
8
+
9
+ ## Usage
10
+
11
+ ```sh
12
+ pnpm dlx build-start-rebuild-perf [options]
13
+ ```
14
+
15
+ ### Example
16
+
17
+ ```sh
18
+ # assuming running in an Ember project with a <img class="logo" /> in the app layout
19
+ pnpm dlx build-start-rebuild-perf --file "app/router.js" --wait-for ".logo"
20
+ ```
21
+
22
+ ## Options
23
+
24
+ ```
25
+ -u, --url <url> URL to load (default: "http://localhost:4200")
26
+ -f, --file <path> File to touch to trigger a reload (no default, but app/router.js is an option)
27
+ -c, --command <cmd> Command to start dev server (default: "pnpm start")
28
+ -w, --wait-for <selector> Element selector to wait for (default: "body")
29
+ -l, --log-level <level> Set the log level (choices: "log", "warn", "error")
30
+ -h, --help display help for command
31
+ ```
32
+
33
+ ## Example Output
34
+
35
+ ```md
36
+ # Performance Results
37
+
38
+ | Dev Server Ready | First Paint | App Loaded | Reload after change |
39
+ | ---------------- | ----------- | ---------- | ------------------- |
40
+ | 5,523 ms | 5,618 ms | 6,142 ms | 918 ms |
41
+ ```
42
+
43
+ ## Share with us
44
+
45
+ Assuming you're in an Ember project and wondering if moving to Vite from the old Ember CLI is worth it, here's how you can use the script to create useful numbers:
46
+
47
+ 1. Start on your `main` branch or anywhwere you're still using Ember CLI with Webpack and Embroider
48
+ 2. Clear all caches (browser, build, etc) and remove your `node_modules`
49
+ 3. Run `pnpm install` to reinstall dependencies
50
+ 4. Run `build-start-rebuild-perf` once to get numbers for a cold start
51
+ 5. Run `build-start-rebuild-perf` again to measure a warm start
52
+ 6. Move to your `vite` branch where you enabled the new Vite based Embroider
53
+ 7. Repeat steps 2-5.
54
+
55
+ Share a your results with the Ember Initiative via [email](https://mainmatter.com/contact/), or [Mastodon](https://fosstodon.org/@mainmatter), or [Bluesky](https://bsky.app/profile/mainmatter.com).
56
+
57
+ ---
58
+
59
+ ## License
60
+
61
+ This project is part of the [Ember Initative](https://mainmatter.com/ember-initiative/). It is developed by and &copy; [Mainmatter GmbH](http://mainmatter.com) and contributors. It is released under the [MIT License](./LICENSE).
package/RELEASE.md ADDED
@@ -0,0 +1,27 @@
1
+ # Release Process
2
+
3
+ Releases in this repo are mostly automated using [release-plan](https://github.com/embroider-build/release-plan/). Once you label all your PRs correctly (see below) you will have an automatically generated PR that updates your CHANGELOG.md file and a `.release-plan.json` that is used to prepare the release once the PR is merged.
4
+
5
+ ## Preparation
6
+
7
+ Since the majority of the actual release process is automated, the remaining tasks before releasing are:
8
+
9
+ - correctly labeling **all** pull requests that have been merged since the last release
10
+ - updating pull request titles so they make sense to our users
11
+
12
+ Some great information on why this is important can be found at [keepachangelog.com](https://keepachangelog.com/en/1.1.0/), but the overall
13
+ guiding principle here is that changelogs are for humans, not machines.
14
+
15
+ When reviewing merged PR's the labels to be used are:
16
+
17
+ - breaking - Used when the PR is considered a breaking change.
18
+ - enhancement - Used when the PR adds a new feature or enhancement.
19
+ - bug - Used when the PR fixes a bug included in a previous release.
20
+ - documentation - Used when the PR adds or updates documentation.
21
+ - internal - Internal changes or things that don't fit in any other category.
22
+
23
+ **Note:** `release-plan` requires that **all** PRs are labeled. If a PR doesn't fit in a category it's fine to label it as `internal`
24
+
25
+ ## Release
26
+
27
+ Once the prep work is completed, the actual release is straight forward: you just need to merge the open [Plan Release](https://github.com/mainmatter/build-start-rebuild-perf/pulls?q=is%3Apr+is%3Aopen+%22Prepare+Release%22+in%3Atitle) PR
package/bin/cli.js ADDED
@@ -0,0 +1,40 @@
1
+ import { cleanupBrowser } from "../lib/browser.js";
2
+ import { setLogLevel } from "../lib/log.js";
3
+ import { formatResults, measure } from "../lib/measure.js";
4
+ import program from "../lib/program.js";
5
+ import { terminateServer } from "../lib/server.js";
6
+
7
+ // Parse arguments and run measurement
8
+ program.parse();
9
+ const options = program.opts();
10
+ // get defaults like this: const DEFAULTS = program.options.map((option) => option.defaultValue);
11
+
12
+ setLogLevel(options.level);
13
+
14
+ ["SIGINT", "SIGTERM", "SIGQUIT"].forEach((signal) => {
15
+ process.on(signal, async () => {
16
+ console.log(`\nReceived ${signal}, cleaning up...`);
17
+ await terminateServer();
18
+ await cleanupBrowser();
19
+ process.exit(0);
20
+ });
21
+ });
22
+
23
+ try {
24
+ let results = await measure(options);
25
+
26
+ console.log(
27
+ `
28
+ Measurement completed successfully!
29
+
30
+ # Performance Results
31
+
32
+ ${formatResults(results)}
33
+
34
+ `,
35
+ );
36
+ process.exit(0);
37
+ } catch (err) {
38
+ console.error("Measurement failed:", err.message);
39
+ process.exit(1);
40
+ }
package/biome.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/2.1.3/schema.json",
3
+ "files": {
4
+ "includes": ["bin/**", "lib/**", "src/**", "test/**", "*.js", "*.mjs", "*.json"],
5
+ "ignoreUnknown": true
6
+ },
7
+ "linter": {
8
+ "enabled": true,
9
+ "rules": {
10
+ "recommended": true,
11
+ "style": {
12
+ "useConst": "off"
13
+ }
14
+ }
15
+ },
16
+ "formatter": {
17
+ "enabled": true,
18
+ "indentStyle": "space",
19
+ "indentWidth": 2,
20
+ "lineWidth": 100
21
+ },
22
+ "javascript": {
23
+ "formatter": {
24
+ "semicolons": "always",
25
+ "quoteStyle": "double"
26
+ }
27
+ }
28
+ }
package/lib/browser.js ADDED
@@ -0,0 +1,133 @@
1
+ import { promises as fs } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { Launcher } from "chrome-launcher";
4
+ import puppeteer from "puppeteer-core";
5
+ import { error, log, warn } from "./log.js";
6
+
7
+ export const VIEWPORT = {
8
+ width: 1366,
9
+ height: 768,
10
+ };
11
+
12
+ let browser = null;
13
+ let page = null;
14
+
15
+ export async function initBrowser() {
16
+ browser = await puppeteer.launch({
17
+ executablePath: Launcher.getInstallations()[0],
18
+ headless: process.env.SHOW_BROWSER !== "true",
19
+ args: ["--no-sandbox"],
20
+ });
21
+
22
+ page = await browser.newPage();
23
+
24
+ // Setup page event listeners
25
+ page.on("console", (msg) => {
26
+ if (["error", "warning"].includes(msg.type())) {
27
+ warn(`PAGE ${msg.type().toUpperCase()}: ${msg.text()}`);
28
+ }
29
+ });
30
+
31
+ page.on("pageerror", (err) => {
32
+ error(`PAGE ERROR: ${err.message}`);
33
+ });
34
+
35
+ page.on("response", (resp) => {
36
+ if (!resp.ok() && resp.status() !== 302 && resp.status() !== 304) {
37
+ warn(`FAILED HTTP REQUEST TO ${resp.url()} Status: ${resp.status()}`);
38
+ }
39
+ });
40
+
41
+ await page.setViewport(VIEWPORT);
42
+
43
+ // Setup authentication if credentials are provided
44
+ if (process.env.AUTH_USER && process.env.AUTH_PASSWORD) {
45
+ await page.authenticate({
46
+ username: process.env.AUTH_USER,
47
+ password: process.env.AUTH_PASSWORD,
48
+ });
49
+ }
50
+ }
51
+
52
+ export async function measurePageLoad(url, waitForSelector = "body") {
53
+ log(`Loading page: ${url}`);
54
+
55
+ const response = await page.goto(url, {
56
+ timeout: 60_000,
57
+ waitUntil: "load",
58
+ });
59
+
60
+ if (!response.ok()) {
61
+ throw new Error(`Failed to load page: ${response.status()}`);
62
+ }
63
+
64
+ const atFirstPaint = performance.now();
65
+
66
+ log("Waiting for page to fully load...");
67
+ await page.waitForNetworkIdle();
68
+
69
+ log(`Waiting for element: ${waitForSelector}`);
70
+ await page.waitForSelector(waitForSelector, {
71
+ visible: true,
72
+ timeout: 60_000,
73
+ });
74
+
75
+ const atAppLoad = performance.now();
76
+
77
+ log(`Page loaded`);
78
+
79
+ return {
80
+ atFirstPaint,
81
+ atAppLoad,
82
+ };
83
+ }
84
+
85
+ export async function measureFileReload(filePath) {
86
+ if (!filePath) {
87
+ log("No file specified for reload test, skipping...");
88
+ return 0;
89
+ }
90
+
91
+ const resolvedPath = resolve(filePath);
92
+
93
+ try {
94
+ await fs.access(resolvedPath);
95
+ } catch {
96
+ warn(`File ${filePath} not found, skipping reload test...`);
97
+ return {};
98
+ }
99
+
100
+ log(`Testing reload with file: ${filePath}`);
101
+
102
+ log("Triggering file change...");
103
+ const atFileChange = performance.now();
104
+
105
+ const originalContent = await fs.readFile(resolvedPath, "utf8");
106
+ await fs.writeFile(resolvedPath, `${originalContent}\n// reload trigger`);
107
+
108
+ const atFileChanged = performance.now();
109
+
110
+ log("Waiting for hot reload to complete...");
111
+ await page.waitForNetworkIdle();
112
+
113
+ const atReloadComplete = performance.now();
114
+
115
+ log("Restoring original file content...");
116
+ await fs.writeFile(resolvedPath, originalContent);
117
+
118
+ return {
119
+ atFileChange,
120
+ atFileChanged,
121
+ atReloadComplete,
122
+ };
123
+ }
124
+
125
+ export async function cleanupBrowser() {
126
+ if (browser) {
127
+ try {
128
+ await browser.close();
129
+ } catch (err) {
130
+ error("Error closing browser:", err.message);
131
+ }
132
+ }
133
+ }
package/lib/log.js ADDED
@@ -0,0 +1,24 @@
1
+ let logLevel = "warn";
2
+
3
+ /**
4
+ * @param level {'log' | 'warn' | 'error'}
5
+ */
6
+ export function setLogLevel(level) {
7
+ logLevel = level;
8
+ }
9
+
10
+ export function log(message) {
11
+ if (logLevel === "log") {
12
+ console.log(message);
13
+ }
14
+ }
15
+
16
+ export function warn(message) {
17
+ if (logLevel === "log" || logLevel === "warn") {
18
+ console.warn(message);
19
+ }
20
+ }
21
+
22
+ export function error(message) {
23
+ console.error(message);
24
+ }
package/lib/measure.js ADDED
@@ -0,0 +1,78 @@
1
+ import { markdownTable } from "markdown-table";
2
+ import { cleanupBrowser, initBrowser, measureFileReload, measurePageLoad } from "./browser.js";
3
+ import { error, log } from "./log.js";
4
+ import { startServer, terminateServer } from "./server.js";
5
+ import { formatMs } from "./utils.js";
6
+
7
+ /**
8
+ * @typedef {Object} MeasureResults
9
+ * @property {number} atStart
10
+ * @property {number} atServerUp
11
+ * @property {number} atFirstPaint
12
+ * @property {number} atAppLoad
13
+ * @property {number} [atFileChange]
14
+ * @property {number} [atFileChanged]
15
+ * @property {number} [atReloadComplete]
16
+ */
17
+
18
+ /**
19
+ *
20
+ * @param {*} options
21
+ * @returns {Promise<MeasureResults>}
22
+ */
23
+ export async function measure(options) {
24
+ try {
25
+ await initBrowser();
26
+ log("Starting performance measurement...\n");
27
+
28
+ const atStart = performance.now();
29
+ const serverURL = await startServer(options);
30
+ const atServerUp = performance.now();
31
+ const pageLoadMetrics = await measurePageLoad(serverURL, options.waitFor);
32
+ const reloadMetrics = await measureFileReload(options.file);
33
+ await terminateServer();
34
+
35
+ return {
36
+ atStart,
37
+ atServerUp,
38
+ ...pageLoadMetrics,
39
+ ...reloadMetrics,
40
+ };
41
+ } catch (err) {
42
+ error("Performance measurement failed:", err.message);
43
+ throw err;
44
+ } finally {
45
+ await terminateServer();
46
+ await cleanupBrowser();
47
+ }
48
+ }
49
+
50
+ /**
51
+ *
52
+ * @param {MeasureResults} results
53
+ * @returns string
54
+ */
55
+ export function formatResults({
56
+ atStart,
57
+ atServerUp,
58
+ atFirstPaint,
59
+ atAppLoad,
60
+ atFileChanged,
61
+ atReloadComplete,
62
+ }) {
63
+ const reloaded = atReloadComplete !== undefined;
64
+
65
+ const tableData = [
66
+ ["Dev Server Ready", "First Paint", "App Loaded", reloaded && "Reload after change"].filter(
67
+ Boolean,
68
+ ),
69
+ [
70
+ formatMs(atServerUp - atStart),
71
+ formatMs(atFirstPaint - atStart),
72
+ formatMs(atAppLoad - atStart),
73
+ reloaded && formatMs(atReloadComplete - atFileChanged),
74
+ ].filter(Boolean),
75
+ ];
76
+
77
+ return markdownTable(tableData);
78
+ }
package/lib/program.js ADDED
@@ -0,0 +1,34 @@
1
+ import { Command, Option } from "commander";
2
+
3
+ const program = new Command();
4
+
5
+ program
6
+ .name("build-start-perf-test")
7
+ .description("Measures build and load performance for web applications")
8
+ .option("-u, --url <url>", "URL to load", "http://localhost:4200")
9
+ .option("-f, --file <path>", "File to touch to trigger a reload")
10
+ .option("-c, --command <cmd>", "Command to start dev server", "pnpm start")
11
+ .option("-w, --wait-for <selector>", "Element selector to wait for", "body")
12
+ .addOption(
13
+ new Option("-l, --log-level <level>", "Set the log level", "warn").choices([
14
+ "log",
15
+ "warn",
16
+ "error",
17
+ ]),
18
+ )
19
+ .addHelpText(
20
+ "after",
21
+ `
22
+ Measures:
23
+ - Build time
24
+ - Time to first paint
25
+ - Time to app load (waiting for specified element)
26
+ - Time to finished reload after file changes
27
+
28
+ Examples:
29
+ $ build-start-perf-test --url http://localhost:3000 --command "npm run dev"
30
+ $ build-start-perf-test --file app.js --wait-for ".app-container"
31
+ `,
32
+ );
33
+
34
+ export default program;