Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

cache-commit.bench.ts

114 lines3.9 KB

1import { bench, describe, expect } from 'vitest'
2import { commitMatches } from '../src/load-client'
3import type { AnyRouteMatch, AnyRouter } from '../src'
4import type { LoaderFlight, LoadTransaction } from '../src/load-client'
5​
6type Match = AnyRouteMatch & { _flight?: LoaderFlight }
7​
8const workloads = [0, 4, 10, 100, 1000, 5000].flatMap((size) => [
9 [size, 4],
10 [size, 16],
11])
12​
13// Run each family separately with -t to keep GC from other workloads out of
14// short operations. This measures commit/cache maintenance, not navigation.
15for (const family of ['snapshots', 'mixed flights', 'preload flights']) {
16 describe.each(workloads)(
17 `${family}: %i cached matches, %i active matches`,
18 (size, activeCount) => {
19 for (const retainedFraction of [1, 0.5, 0]) {
20 const retainedCount = Math.floor(size * retainedFraction)
21 let aborts = 0
22 const entries = Array.from({ length: size }, (_, index) => {
23 const retained =
24 retainedFraction === 1 ||
25 (retainedFraction === 0.5 && index % 2 === 1)
26 const hasFlight =
27 family === 'preload flights' ||
28 (family === 'mixed flights' && index % 10 === 0)
29 // Keep loader scheduling and AbortController allocation outside this
30 // ownership benchmark. Real abort events are covered in the unit tests.
31 const flight: LoaderFlight | undefined = hasFlight
32 ? [
33 Promise.resolve([0, undefined]),
34 {
35 abort: () => {
36 aborts++
37 },
38 } as AbortController,
39 1,
40 ]
41 : undefined
42 const match = {
43 id: String(index),
44 routeId: retained ? 'retained' : 'expired',
45 status: 'success',
46 updatedAt: 0,
47 _flight: flight,
48 } as Match
49 return { match, flight, retained }
50 })
51 const cached = new Map(entries.map(({ match }) => [match.id, match]))
52 const departing = entries.filter(
53 (entry) => !entry.retained && entry.flight,
54 )
55 const matches = Array.from({ length: activeCount }, (_, index) => ({
56 id: `active-${index}`,
57 routeId: 'retained',
58 status: 'success',
59 })) as Array<Match>
60 const tx = [
61 new AbortController(),
62 0,
63 undefined,
64 [],
65 0,
66 Promise.resolve(),
67 ] as unknown as LoadTransaction
68 const router = {
69 _tx: tx,
70 _committed: matches,
71 _cache: cached,
72 options: {},
73 routesById: {
74 retained: { options: { loader: () => {}, gcTime: Infinity } },
75 expired: { options: { loader: () => {}, gcTime: 0 } },
76 },
77 stores: { setMatches: () => {} },
78 } as unknown as AnyRouter
79 const run = () => {
80 router._cache = cached
81 for (const entry of departing) {
82 entry.match._flight = entry.flight
83 entry.flight![2] = 1
84 }
85 commitMatches(router, tx, matches)
86 }
87 run()
88 expect(router._cache.size).toBe(retainedCount)
89 expect(aborts).toBe(departing.length)
90 for (const entry of entries) {
91 if (entry.retained) {
92 expect(router._cache.get(entry.match.id)).toBe(entry.match)
93 expect(entry.match._flight).toBe(entry.flight)
94 if (entry.flight) {
95 expect(entry.flight[2]).toBe(1)
96 }
97 } else {
98 expect(entry.match._flight).toBeUndefined()
99 }
100 }
101 const batch = size <= 100 ? 100 : 1
102 bench(
103 `${retainedFraction * 100}% retained (${batch} commits)`,
104 () => {
105 for (let index = 0; index < batch; index++) {
106 run()
107 }
108 },
109 { time: 500, warmupTime: 100 },
110 )
111 }
112 },
113 )