-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
test(react-start): benchmarking setup w/ platformatic/flame and autocannon #6457
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
Merged
+866
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,7 @@ | ||
| # Flamegraph profiles | ||
| **/cpu-profile-*.md | ||
| **/cpu-profile-*.html | ||
| **/cpu-profile-*.pb | ||
| **/heap-profile-*.html | ||
| **/heap-profile-*.pb | ||
| **/heap-profile-*.md |
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,28 @@ | ||
| { | ||
| "name": "tanstack-react-start-flamegraph-bench", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "NODE_ENV=production vite build --mode=production", | ||
| "start": "NODE_ENV=production node ./tests/server.js", | ||
| "start:prof": "NODE_ENV=production flame run --md-format=detailed ./tests/server.js", | ||
| "bench": "pnpm build && pnpm bench:run", | ||
| "bench:run": "concurrently -k -s first \"pnpm start:prof\" \"sleep 3 && node ./tests/bench.js\"" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/react-router": "workspace:*", | ||
| "@tanstack/react-start": "workspace:*", | ||
| "express": "^4.21.2", | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0", | ||
| "srvx": "^0.10.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@platformatic/flame": "latest", | ||
| "@vitejs/plugin-react": "^4.3.4", | ||
| "autocannon": "^8.0.0", | ||
| "concurrently": "9.2.1", | ||
| "typescript": "^5.7.2", | ||
| "vite": "^7.3.1" | ||
| } | ||
| } |
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,5 @@ | ||
| /// <reference types="vite/client" /> | ||
| import { hydrateRoot } from 'react-dom/client' | ||
| import { StartClient } from '@tanstack/react-start/client' | ||
|
|
||
| hydrateRoot(document, <StartClient />) |
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,86 @@ | ||
| /* eslint-disable */ | ||
|
|
||
| // @ts-nocheck | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
|
|
||
| // This file was automatically generated by TanStack Router. | ||
| // You should NOT make any changes in this file as it will be overwritten. | ||
| // Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. | ||
|
|
||
| import { Route as rootRouteImport } from './routes/__root' | ||
| import { Route as IndexRouteImport } from './routes/index' | ||
| import { Route as PageIdRouteImport } from './routes/page.$id' | ||
|
|
||
| const IndexRoute = IndexRouteImport.update({ | ||
| id: '/', | ||
| path: '/', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
| const PageIdRoute = PageIdRouteImport.update({ | ||
| id: '/page/$id', | ||
| path: '/page/$id', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
|
|
||
| export interface FileRoutesByFullPath { | ||
| '/': typeof IndexRoute | ||
| '/page/$id': typeof PageIdRoute | ||
| } | ||
| export interface FileRoutesByTo { | ||
| '/': typeof IndexRoute | ||
| '/page/$id': typeof PageIdRoute | ||
| } | ||
| export interface FileRoutesById { | ||
| __root__: typeof rootRouteImport | ||
| '/': typeof IndexRoute | ||
| '/page/$id': typeof PageIdRoute | ||
| } | ||
| export interface FileRouteTypes { | ||
| fileRoutesByFullPath: FileRoutesByFullPath | ||
| fullPaths: '/' | '/page/$id' | ||
| fileRoutesByTo: FileRoutesByTo | ||
| to: '/' | '/page/$id' | ||
| id: '__root__' | '/' | '/page/$id' | ||
| fileRoutesById: FileRoutesById | ||
| } | ||
| export interface RootRouteChildren { | ||
| IndexRoute: typeof IndexRoute | ||
| PageIdRoute: typeof PageIdRoute | ||
| } | ||
|
|
||
| declare module '@tanstack/react-router' { | ||
| interface FileRoutesByPath { | ||
| '/': { | ||
| id: '/' | ||
| path: '/' | ||
| fullPath: '/' | ||
| preLoaderRoute: typeof IndexRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| '/page/$id': { | ||
| id: '/page/$id' | ||
| path: '/page/$id' | ||
| fullPath: '/page/$id' | ||
| preLoaderRoute: typeof PageIdRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const rootRouteChildren: RootRouteChildren = { | ||
| IndexRoute: IndexRoute, | ||
| PageIdRoute: PageIdRoute, | ||
| } | ||
| export const routeTree = rootRouteImport | ||
| ._addFileChildren(rootRouteChildren) | ||
| ._addFileTypes<FileRouteTypes>() | ||
|
|
||
| import type { getRouter } from './router.tsx' | ||
| import type { createStart } from '@tanstack/react-start' | ||
| declare module '@tanstack/react-start' { | ||
| interface Register { | ||
| ssr: true | ||
| router: Awaited<ReturnType<typeof getRouter>> | ||
| } | ||
| } |
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,11 @@ | ||
| import { createRouter } from '@tanstack/react-router' | ||
| import { routeTree } from './routeTree.gen' | ||
|
|
||
| export function getRouter() { | ||
| const router = createRouter({ | ||
| routeTree, | ||
| scrollRestoration: true, | ||
| }) | ||
|
|
||
| return router | ||
| } |
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,28 @@ | ||
| import * as React from 'react' | ||
| import { | ||
| HeadContent, | ||
| Outlet, | ||
| Scripts, | ||
| createRootRoute, | ||
| } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createRootRoute({ | ||
| component: RootComponent, | ||
| }) | ||
|
|
||
| function RootComponent() { | ||
| return ( | ||
| <html> | ||
| <head> | ||
| <meta charSet="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>Flamegraph Benchmark</title> | ||
| <HeadContent /> | ||
| </head> | ||
| <body> | ||
| <Outlet /> | ||
| <Scripts /> | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
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,26 @@ | ||
| import { Link, createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/')({ | ||
| component: IndexComponent, | ||
| }) | ||
|
|
||
| function IndexComponent() { | ||
| return ( | ||
| <div> | ||
| <h1 data-testid="page-title">Flamegraph Benchmark - Index</h1> | ||
| <p>Navigate to any page to start:</p> | ||
| <div> | ||
| {Array.from({ length: 10 }, (_, i) => ( | ||
| <Link | ||
| key={i} | ||
| to="/page/$id" | ||
| params={{ id: String(i) }} | ||
| style={{ marginRight: '10px' }} | ||
| > | ||
| Page {i} | ||
| </Link> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
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,29 @@ | ||
| import { Link, createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/page/$id')({ | ||
| component: PageComponent, | ||
| }) | ||
|
|
||
| function PageComponent() { | ||
| const { id } = Route.useParams() | ||
|
|
||
| // 100 links = 100 buildLocation calls during SSR | ||
| return ( | ||
| <div> | ||
| <h1 data-testid="page-title">Page {id}</h1> | ||
| <div> | ||
| {Array.from({ length: 100 }, (_, i) => ( | ||
| <Link | ||
| key={i} | ||
| to="/page/$id" | ||
| params={{ id: String(i % 10) }} | ||
| data-testid={`link-${i}`} | ||
| style={{ display: 'block', margin: '2px 0' }} | ||
| > | ||
| Go to Page {i % 10} | ||
| </Link> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
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,7 @@ | ||
| import handler from '@tanstack/react-start/server-entry' | ||
|
|
||
| export default { | ||
| fetch(request: Request) { | ||
| return handler.fetch(request) | ||
| }, | ||
| } |
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,39 @@ | ||
| import autocannon from 'autocannon' | ||
|
|
||
| const BASE_URL = 'http://localhost:3000' | ||
|
|
||
| const instance = autocannon({ | ||
| url: BASE_URL, | ||
| connections: 100, // concurrent connections | ||
| duration: 30, // seconds | ||
| pipelining: 1, // requests per connection | ||
| requests: [ | ||
| { | ||
| setupRequest: (req) => { | ||
| // Pick a random page for each request | ||
| const randomPage = '/page/' + Math.floor(Math.random() * 1000) | ||
| return { ...req, path: randomPage } | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| autocannon.track(instance, { renderProgressBar: true }) | ||
|
|
||
| instance.on('done', (results) => { | ||
| console.log('\n=== SSR Benchmark Results ===') | ||
| console.log(`Total requests: ${results.requests.total}`) | ||
| console.log(`Requests/sec: ${results.requests.average}`) | ||
| console.log(`Latency (avg): ${results.latency.average}ms`) | ||
| console.log(`Latency (p99): ${results.latency.p99}ms`) | ||
| console.log( | ||
| `Throughput: ${(results.throughput.average / 1024 / 1024).toFixed(2)} MB/s`, | ||
| ) | ||
|
|
||
| if (results.errors) { | ||
| console.log(`Errors: ${results.errors}`) | ||
| } | ||
|
|
||
| // Exit after a short delay to allow the server profiler to finish | ||
| setTimeout(() => process.exit(0), 1000) | ||
| }) |
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,30 @@ | ||
| import { toNodeHandler } from 'srvx/node' | ||
| import express from 'express' | ||
|
|
||
| const port = process.env.PORT || 3000 | ||
|
|
||
| async function createStartServer() { | ||
| const server = (await import('../dist/server/server.js')).default | ||
| const nodeHandler = toNodeHandler(server.fetch) | ||
|
|
||
| const app = express() | ||
|
|
||
| // Serve static client assets (matching basic example pattern) | ||
| app.use(express.static('../dist/client', { redirect: false })) | ||
|
|
||
| app.use(async (req, res, next) => { | ||
| try { | ||
| await nodeHandler(req, res) | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
| }) | ||
|
|
||
| return { app } | ||
| } | ||
|
|
||
| createStartServer().then(async ({ app }) => | ||
| app.listen(port, () => { | ||
| console.log(`Flamegraph bench server: http://localhost:${port}`) | ||
| }), | ||
| ) | ||
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,19 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
| "module": "ESNext", | ||
| "skipLibCheck": true, | ||
| "moduleResolution": "bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| }, | ||
| "include": ["src", "tests"] | ||
| } |
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,10 @@ | ||
| import { defineConfig } from 'vite' | ||
| import { tanstackStart } from '@tanstack/react-start/plugin/vite' | ||
| import viteReact from '@vitejs/plugin-react' | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| port: 3000, | ||
| }, | ||
| plugins: [tanstackStart(), viteReact()], | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/router
Length of output: 109
🏁 Script executed:
# Check the file contents cat -n e2e/react-start/flamegraph-bench/tests/server.jsRepository: TanStack/router
Length of output: 994
🏁 Script executed:
# Check the module type of the file (is it ESM or CommonJS?) head -20 e2e/react-start/flamegraph-bench/tests/server.jsRepository: TanStack/router
Length of output: 610
🏁 Script executed:
Repository: TanStack/router
Length of output: 954
🌐 Web query:
express.static relative path resolution behavior💡 Result:
Short answer: express.static resolves a relative root path relative to the current working directory (process.cwd()), i.e. where you launched node — not relative to the module file. Use an absolute path (e.g. path.join(__dirname, 'public')) so resolution is deterministic. [1][2]
Details (one line each):
Example:
Sources:
[1] Express docs — Serving static files.
[2] StackOverflow explanation showing express.static uses the process launch directory.
🌐 Web query:
Node.js process.env.PORT string named pipe behavior💡 Result:
Short answer: If process.env.PORT is a string that looks like a path (on Windows: begins with \?\pipe\ or \.\pipe), Node's server.listen treats it as an IPC path (named pipe) instead of a TCP port. A numeric string ("3000") is treated as a port number, but a pipe-style string is treated as an IPC path — so pass a Number (e.g. Number(process.env.PORT) or parseInt) when you mean a TCP port, or pass the pipe string when you want a named pipe. [1][2]
Example:
const port = Number(process.env.PORT) || 3000;
server.listen(port);
const pipe = process.env.PORT; // e.g. "\\?\pipe\myapp"
server.listen(pipe);
References:
🏁 Script executed:
Repository: TanStack/router
Length of output: 153
🏁 Script executed:
Repository: TanStack/router
Length of output: 153
🏁 Script executed:
Repository: TanStack/router
Length of output: 527
🏁 Script executed:
Repository: TanStack/router
Length of output: 41
🏁 Script executed:
# Check the vite config to understand where dist is built cat e2e/react-start/flamegraph-bench/vite.config.tsRepository: TanStack/router
Length of output: 315
🏁 Script executed:
Repository: TanStack/router
Length of output: 84
🏁 Script executed:
Repository: TanStack/router
Length of output: 878
🏁 Script executed:
Repository: TanStack/router
Length of output: 175
🏁 Script executed:
Repository: TanStack/router
Length of output: 573
🏁 Script executed:
Repository: TanStack/router
Length of output: 41
🏁 Script executed:
Repository: TanStack/router
Length of output: 211
🏁 Script executed:
Repository: TanStack/router
Length of output: 81
🏁 Script executed:
Repository: TanStack/router
Length of output: 4371
Coerce PORT to number and resolve static asset path relative to module location.
The code passes
process.env.PORTas a string toserver.listen(), which Node treats as an IPC pipe path on Windows instead of a TCP port. Additionally,express.static('../dist/client')resolves relative to the process working directory, so it points to the wrong location when the build output is in./dist/client. Coerce PORT to a number and resolve the asset path relative to this file.Proposed fix
import { toNodeHandler } from 'srvx/node' import express from 'express' +import path from 'node:path' +import { fileURLToPath } from 'node:url' -const port = process.env.PORT || 3000 +const port = Number(process.env.PORT) || 3000 +const __dirname = path.dirname(fileURLToPath(import.meta.url)) +const clientDir = path.resolve(__dirname, '../dist/client') // Serve static client assets (matching basic example pattern) - app.use(express.static('../dist/client', { redirect: false })) + app.use(express.static(clientDir, { redirect: false }))📝 Committable suggestion
🤖 Prompt for AI Agents