Open History to see the last commit
1// @vitest-environment node
2β
3import { afterAll, bench, describe, expect } from 'vitest'
4import { runWithStartContext } from '@tanstack/start-storage-context'
5import { createMiddleware } from '../src/createMiddleware'
6import { executeMiddleware } from '../src/createServerFn'
7import type { StartStorageContext } from '@tanstack/start-storage-context'
8import type { ServerFnMiddlewareOptions } from '../src/createServerFn'
9β
10type SendContextMode = 'absent' | 'empty' | 'present'
11type Context = Readonly<Record<string, string | number>>
12β
13type Scenario = {
14 name: string
15 env: 'client' | 'server'
16 headers: 'absent' | 'supplied'
17 depth: 0 | 1 | 4
18 initialSendContext: SendContextMode
19 nextSendContext: SendContextMode
20}
21β
22type NextOptions = {
23 context: Context
24 sendContext?: Context
25 headers?: Headers
26}
27β
28type InputOptions = Pick<ServerFnMiddlewareOptions, 'method' | 'serverFnMeta'> &
29 NextOptions & {
30 data: number
31 }
32β
33type Input = {
34 options: Readonly<InputOptions>
35 snapshot: ReturnType<typeof snapshotOptions>
36 storage: StartStorageContext
37}
38β
39const batchSize = 160
40const benchOptions = { warmupIterations: 100, time: 2_000, throws: true }
41const sendContextScenarios: Array<{
42 name: string
43 initial: SendContextMode
44 next: SendContextMode
45}> = [
46 { name: 'absent', initial: 'absent', next: 'absent' },
47 { name: 'empty', initial: 'empty', next: 'empty' },
48 { name: 'present', initial: 'present', next: 'present' },
49 { name: 'empty-from-next', initial: 'absent', next: 'empty' },
50 { name: 'present-from-next', initial: 'absent', next: 'present' },
51]
52β
53const scenarios: Array<Scenario> = (['server', 'client'] as const).flatMap(
54 (env) =>
55 (['absent', 'supplied'] as const).flatMap((headers) =>
56 ([0, 1, 4] as const).flatMap((depth) =>
57 sendContextScenarios
58 .filter((context) => depth > 0 || context.initial === context.next)
59 .map((context) => ({
60 name: `${env}-headers-${headers}-send-${context.name}-layers-${depth}`,
61 env,
62 headers,
63 depth,
64 initialSendContext: context.initial,
65 nextSendContext: context.next,
66 })),
67 ),
68 ),
69)
70β
71function snapshotOptions(options: NextOptions) {
72 return {
73 keys: Object.keys(options),
74 context: { ...options.context },
75 sendContext:
76 options.sendContext === undefined
77 ? undefined
78 : { ...options.sendContext },
79 }
80}
81β
82function snapshotHeaders(headers: Headers) {
83 return {
84 headers,
85 entries: Array.from(headers),
86 cookies: headers.getSetCookie(),
87 }
88}
89β
90function createWorkload(scenario: Scenario) {
91 const suppliedHeaders = scenario.headers === 'supplied'
92 const initialHeaders = new Headers({
93 'x-initial': 'one',
94 'x-shared': 'initial',
95 })
96 initialHeaders.append('set-cookie', 'first=one; Path=/')
97 initialHeaders.append('set-cookie', 'second=two; Path=/')
98 const headerSnapshots = [snapshotHeaders(initialHeaders)]
99 const expectedHeaders = new Headers(
100 suppliedHeaders ? initialHeaders : undefined,
101 )
102 const nextInputs: Array<{
103 options: Readonly<NextOptions>
104 snapshot: ReturnType<typeof snapshotOptions>
105 }> = []
106 const middlewares = Array.from({ length: scenario.depth }, (_, index) => {
107 const nextOptions: NextOptions = {
108 context: Object.freeze({ [`hop${index}`]: index, shared: `hop${index}` }),
109 }
110 if (scenario.nextSendContext !== 'absent') {
111 nextOptions.sendContext = Object.freeze<Context>(
112 scenario.nextSendContext === 'empty'
113 ? {}
114 : { [`reply${index}`]: index, shared: `reply${index}` },
115 )
116 }
117 // Only client middleware can supply headers through its public next().
118 if (scenario.env === 'client' && suppliedHeaders) {
119 const headers = new Headers({
120 'x-shared': `hop${index}`,
121 [`x-hop-${index}`]: String(index),
122 })
123 headers.append('set-cookie', `hop${index}=${index}; Path=/`)
124 headerSnapshots.push(snapshotHeaders(headers))
125 nextOptions.headers = headers
126 expectedHeaders.set('x-shared', `hop${index}`)
127 expectedHeaders.set(`x-hop-${index}`, String(index))
128 expectedHeaders.append('set-cookie', `hop${index}=${index}; Path=/`)
129 }
130 nextInputs.push({
131 options: Object.freeze(nextOptions),
132 snapshot: snapshotOptions(nextOptions),
133 })
134 if (scenario.env === 'client') {
135 return createMiddleware({ type: 'function' }).client(({ next }) =>
136 next(nextOptions),
137 )
138 }
139 return createMiddleware({ type: 'function' }).server(({ next }) =>
140 next(nextOptions),
141 )
142 })
143 const inputs = Array.from({ length: batchSize }, (_, index): Input => {
144 const options: InputOptions = {
145 method: 'POST',
146 data: index,
147 serverFnMeta: Object.freeze({ id: 'middleware-benchmark' }),
148 context: Object.freeze({
149 seed: index,
150 trusted: 'server-context',
151 shared: 'initial',
152 }),
153 }
154 if (suppliedHeaders) {
155 options.headers = initialHeaders
156 }
157 if (scenario.initialSendContext !== 'absent') {
158 options.sendContext = Object.freeze<Context>(
159 scenario.initialSendContext === 'empty'
160 ? {}
161 : { initialReply: 'present', shared: 'initial' },
162 )
163 }
164 return {
165 options: Object.freeze(options),
166 snapshot: snapshotOptions(options),
167 storage: {
168 request: new Request(
169 `http://localhost/_serverFn/middleware-benchmark?i=${index}`,
170 ),
171 getRouter() {
172 throw new Error('Function middleware does not need a router')
173 },
174 startOptions: {},
175 contextAfterGlobalMiddlewares: {},
176 executedRequestMiddlewares: new Set(),
177 handlerType: 'serverFn',
178 },
179 }
180 })
181β
182 // The client phase uses the real Start context used during SSR calls.
183 function invoke(input: Input) {
184 return runWithStartContext(input.storage, () =>
185 executeMiddleware(middlewares, scenario.env, input.options),
186 )
187 }
188β
189 async function verify() {
190 for (const input of inputs) {
191 const { options, snapshot } = input
192 const result = await invoke(input)
193 expect(result.error).toBeUndefined()
194 expect(result.data).toBe(options.data)
195 // Handler results and transport belong to the server-function benchmarks.
196 expect(result.result).toBeUndefined()
197 const context = { ...options.context }
198 const sendContext = { ...options.sendContext }
199 for (const next of nextInputs) {
200 Object.assign(context, next.options.context)
201 Object.assign(sendContext, next.options.sendContext)
202 }
203 expect({ ...result.context }).toEqual(context)
204 // Compare absent payload contents while preserving explicit empty objects.
205 expect({ ...result.sendContext }).toEqual(sendContext)
206 if (scenario.nextSendContext !== 'absent') {
207 expect(result.sendContext).toBeDefined()
208 }
209 if (scenario.env === 'client') {
210 expect(result.headers).toBeDefined()
211 }
212 const headers = new Headers(result.headers)
213 expect(Array.from(headers)).toEqual(Array.from(expectedHeaders))
214 expect(headers.getSetCookie()).toEqual(expectedHeaders.getSetCookie())
215 expect(snapshotOptions(options)).toEqual(snapshot)
216 }
217 for (const { options, snapshot } of nextInputs) {
218 expect(snapshotOptions(options)).toEqual(snapshot)
219 }
220 for (const { headers, entries, cookies } of headerSnapshots) {
221 expect(Array.from(headers)).toEqual(entries)
222 expect(headers.getSetCookie()).toEqual(cookies)
223 }
224 }
225β
226 async function batch() {
227 // Every call settles before the next; no work escapes the measured batch.
228 for (const input of inputs) {
229 await invoke(input)
230 }
231 }
232β
233 function invokeIndex(index: number) {
234 return invoke(inputs[index]!)
235 }
236β
237 return { verify, batch, invokeIndex }
238}
239β
240function createMixedWorkload(env: Scenario['env']) {
241 const absent = createWorkload({
242 name: 'mixed-absent',
243 env,
244 headers: 'absent',
245 depth: 4,
246 initialSendContext: 'absent',
247 nextSendContext: 'absent',
248 })
249 const supplied = createWorkload({
250 name: 'mixed-supplied',
251 env,
252 headers: 'supplied',
253 depth: 4,
254 initialSendContext: 'present',
255 nextSendContext: 'present',
256 })
257β
258 async function verify() {
259 await absent.verify()
260 await supplied.verify()
261 }
262β
263 async function batch() {
264 // Interleave 120 absent calls and 40 supplied calls in each 160-call batch.
265 for (let index = 0; index < batchSize; index++) {
266 await (index % 4 === 0 ? supplied : absent).invokeIndex(index)
267 }
268 }
269β
270 return { verify, batch }
271}
272β
273const workloads: Array<
274 ReturnType<typeof createWorkload> | ReturnType<typeof createMixedWorkload>
275> = []
276β
277afterAll(async () => {
278 for (const workload of workloads) {
279 await workload.verify()
280 }
281})
282β
283for (const scenario of scenarios) {
284 describe(`function middleware ${scenario.name}`, () => {
285 let workload: ReturnType<typeof createWorkload> | undefined
286β
287 bench('160 calls', () => workload!.batch(), {
288 ...benchOptions,
289 // Tinybench awaits setup before warmup and measurement. Only selected cases
290 // allocate fixtures, and the measured callback contains no assertions.
291 setup: async () => {
292 if (!workload) {
293 workload = createWorkload(scenario)
294 workloads.push(workload)
295 }
296 await workload.verify()
297 },
298 })
299 })
300}
301β
302for (const env of ['server', 'client'] as const) {
303 describe(`function middleware ${env}-mixed-75-absent-25-supplied-layers-4`, () => {
304 let workload: ReturnType<typeof createMixedWorkload> | undefined
305β
306 bench('160 calls', () => workload!.batch(), {
307 ...benchOptions,
308 setup: async () => {
309 if (!workload) {
310 workload = createMixedWorkload(env)
311 workloads.push(workload)
312 }
313 await workload.verify()
314 },
315 })
316 })
317}