22import assert from 'node:assert/strict' ;
33import { fork } from 'node:child_process' ;
44import { once } from 'node:events' ;
5- import { mkdtemp , rm , writeFile } from 'node:fs/promises' ;
5+ import { mkdtemp , rm , stat , writeFile } from 'node:fs/promises' ;
66import os from 'node:os' ;
77import path from 'node:path' ;
88import { DatabaseSync } from 'node:sqlite' ;
99import { pathToFileURL } from 'node:url' ;
1010import { setTimeout as delay } from 'node:timers/promises' ;
1111import { parseArgs } from 'node:util' ;
12+ import { uploadPhaseProfile } from './upload-phase-profile.js' ;
1213import {
1314 cloneCatalog ,
1415 fileResidency ,
@@ -26,6 +27,9 @@ const { values } = parseArgs({
2627 'helper-timeout' : { type : 'boolean' } ,
2728 'fresh-copy' : { type : 'boolean' } ,
2829 'page-probe' : { type : 'string' } ,
30+ 'profile-reader' : { type : 'boolean' } ,
31+ 'seed-layout' : { type : 'string' , default : 'fresh' } ,
32+ 'mixed-history' : { type : 'boolean' } ,
2933 out : { type : 'string' } ,
3034 root : { type : 'string' } ,
3135 round : { type : 'string' } ,
@@ -37,16 +41,27 @@ const accounts = Number(values.accounts),
3741assert . ok ( Number . isInteger ( accounts ) && accounts >= 2 && accounts <= 32 ) ;
3842assert . ok ( Number . isInteger ( rows ) && rows >= 0 && rows * accounts <= 4_194_304 ) ;
3943assert . ok ( Number . isInteger ( restarts ) && restarts >= 1 && restarts <= 64 ) ;
44+ assert . ok ( [ 'fresh' , 'legacy' , 'upgraded' ] . includes ( values [ 'seed-layout' ] ) ) ;
45+ if ( values [ 'seed-layout' ] !== 'fresh' && ! process . send )
46+ assert . ok ( values . compare , 'legacy and upgraded fixtures need a prior compiled build' ) ;
4047if ( values [ 'helper-timeout' ] ) assert . ok ( ! values . uncapped && rows >= 4096 ) ;
48+ if ( values [ 'profile-reader' ] )
49+ assert . ok ( ! values . uncapped && ! values [ 'helper-timeout' ] && rows >= 4096 ) ;
4150const owner = ( i : number ) => 'cold-fixture-' + i ;
4251const token = ( i : number ) => 'test-cold-quota-local-fixture-only-' + i ;
43- const bytes = 77 ,
44- content = 'x' . repeat ( 128 ) ,
52+ const sizes = values [ 'mixed-history' ] ? [ 0 , 77 , 77 , 2048 , 13 , 65536 , 311 , 1024 ] : [ 77 ] ;
53+ const historyBytes =
54+ Math . floor ( rows / sizes . length ) * sizes . reduce ( ( a , b ) => a + b , 0 ) +
55+ sizes . slice ( 0 , rows % sizes . length ) . reduce ( ( a , b ) => a + b , 0 ) ;
56+ const content = 'x' . repeat ( 128 ) ,
4557 probes = 20 ,
4658 spacing = 2 ;
4759async function seed ( root : string ) {
48- const { Store } = await import ( '../src/store/store.js' ) ;
49- const s = new Store ( root ) ;
60+ const { Store } : typeof import ( '../src/store/store.js' ) =
61+ values [ 'seed-layout' ] === 'fresh'
62+ ? await import ( '../src/store/store.js' )
63+ : await import ( pathToFileURL ( path . resolve ( values . compare ! , 'src/store/store.js' ) ) . href ) ;
64+ const s = new Store ( root , { cappedUploads : ! values . uncapped } ) ;
5065 try {
5166 const quiet = await s . addFile (
5267 owner ( accounts ) ,
@@ -71,8 +86,9 @@ async function seed(root: string) {
7186 id,
7287 owner : o ,
7388 name : 'history' ,
74- bytes,
75- sha256 : hex . padStart ( 64 , '0' ) ,
89+ bytes : sizes [ i % sizes . length ] ,
90+ sha256 : values [ 'mixed-history' ] && i % 3 === 0 ? 'e' . repeat ( 64 ) : hex . padStart ( 64 , '0' ) ,
91+ ...( values [ 'mixed-history' ] && i % 4 === 0 && { allowedNodes : [ 'fixture' ] } ) ,
7692 createdAt : 1 ,
7793 } ) ,
7894 ) ;
@@ -98,18 +114,30 @@ async function gateway() {
98114 } ) ;
99115 const start = performance . now ( ) ;
100116 const restore = values [ 'helper-timeout' ] ? withholdReader ( ) : undefined ;
117+ const profile = values [ 'profile-reader' ]
118+ ? uploadPhaseProfile (
119+ accounts ,
120+ new Map ( Array . from ( { length : accounts } , ( _ , i ) => [ 'Bearer ' + token ( i ) , i ] ) ) ,
121+ )
122+ : undefined ;
101123 const s = await setup ( undefined , {
102124 dataDir : values . root ,
103125 keys,
104126 ...( ! values . uncapped && {
105- maxStoredBytesPerSubject : rows * bytes + ( round + 1 ) * 2 * content . length ,
127+ maxStoredBytesPerSubject : historyBytes + ( round + 1 ) * 2 * content . length ,
106128 } ) ,
107129 } ) ;
108130 try {
109131 const disconnected = once ( process , 'disconnect' ) ;
132+ profile ?. attach ( s . store , s . app . server ) ;
133+ if ( profile )
134+ process . on ( 'message' , ( m ) => {
135+ if ( m === 'quota-phase-profile' ) process . send ! ( { phases : profile . snapshot ( ) } ) ;
136+ } ) ;
110137 process . send ! ( { base : await s . listen ( ) , ready : performance . now ( ) - start } ) ;
111138 await disconnected ;
112139 } finally {
140+ profile ?. close ( ) ;
113141 restore ?.( ) ;
114142 await s . cleanup ( ) ;
115143 }
@@ -123,12 +151,15 @@ const stats = (times: number[]) => {
123151const sample = ( ) => ( {
124152 uploads : [ ] as number [ ] ,
125153 quiet : [ ] as number [ ] ,
154+ fixedQuiet : [ ] as number [ ] ,
155+ fixedWaveP95 : [ ] as number [ ] ,
126156 lag : [ ] as number [ ] ,
127157 wave : [ ] as number [ ] ,
128158 quietGaps : [ ] as number [ ] ,
129159 lateProbes : 0 ,
130160} ) ;
131161const measurements = ( ) => ( {
162+ phases : [ ] as ReturnType < ReturnType < typeof uploadPhaseProfile > [ 'snapshot' ] > [ ] ,
132163 cold : sample ( ) ,
133164 warm : sample ( ) ,
134165 startup : [ ] as number [ ] ,
@@ -145,6 +176,23 @@ async function measure() {
145176 current = measurements ( ) ,
146177 prior = measurements ( ) ,
147178 ids = new Set < string > ( ) ;
179+ const { fileIndexCommand } = await import ( '../src/client/file-index.js' ) ,
180+ initial = fileIndexCommand ( root ) ,
181+ initialBytes = ( await stat ( path . join ( root , 'state.sqlite' ) ) ) . size ;
182+ assert . equal (
183+ initial . layout ,
184+ values [ 'seed-layout' ] === 'fresh' && ! values . uncapped ? 'scope-bytes' : 'scope-only' ,
185+ ) ;
186+ const maintenanceAt = performance . now ( ) ,
187+ prepared = values [ 'seed-layout' ] === 'upgraded' ? fileIndexCommand ( root , true ) : undefined ;
188+ const index = {
189+ initial : initial . layout ,
190+ served : prepared ?. layout ?? initial . layout ,
191+ rebuilt : prepared ?. changed ?? false ,
192+ maintenance_ms : prepared ? + ( performance . now ( ) - maintenanceAt ) . toFixed ( 2 ) : 0 ,
193+ initial_db_bytes : initialBytes ,
194+ prepared_db_bytes : ( await stat ( path . join ( root , 'state.sqlite' ) ) ) . size ,
195+ } ;
148196 const processes = restarts * ( values . compare ? 2 : 1 ) ;
149197 for ( let round = 0 ; round < processes ; round ++ ) {
150198 // AB, BA: each version sees the first and second position equally often.
@@ -175,6 +223,8 @@ async function measure() {
175223 root ,
176224 ] ;
177225 if ( values . uncapped ) args . push ( '--uncapped' ) ;
226+ if ( values [ 'mixed-history' ] ) args . push ( '--mixed-history' ) ;
227+ if ( values [ 'profile-reader' ] ) args . push ( '--profile-reader' ) ;
178228 if ( values [ 'helper-timeout' ] ) args . push ( '--helper-timeout' ) ;
179229 if ( before ) args . push ( '--compare' , values . compare ! ) ;
180230 const child = fork ( import . meta. filename , args , {
@@ -211,13 +261,15 @@ async function measure() {
211261 if ( probePages ) residency . push ( await probePages ( ) ) ;
212262 for ( const phase of [ cold , warm ] ) {
213263 const start = performance . now ( ) ;
264+ const fixedFrom = phase . fixedQuiet . length ;
214265 let finished = Infinity ;
215266 const arrivals : number [ ] = [ ] ;
216- const probe = async ( ) => {
267+ const probe = async ( fixed = false ) => {
217268 const at = performance . now ( ) ;
218269 await read ( ) ;
219270 const end = performance . now ( ) ;
220271 phase . quiet . push ( end - at ) ;
272+ if ( fixed ) phase . fixedQuiet . push ( end - at ) ;
221273 arrivals . push ( end ) ;
222274 } ;
223275 await Promise . all ( [
@@ -253,7 +305,7 @@ async function measure() {
253305 await delay ( Math . max ( 0 , target - performance . now ( ) ) ) ;
254306 const t = performance . now ( ) ;
255307 phase . lag . push ( Math . max ( 0 , t - target ) ) ;
256- await probe ( ) ;
308+ await probe ( true ) ;
257309 } ) ,
258310 ) ,
259311 ( async ( ) => {
@@ -274,6 +326,7 @@ async function measure() {
274326 finished ,
275327 ] ;
276328 phase . quietGaps . push ( Math . max ( ...points . slice ( 1 ) . map ( ( n , i ) => n - points [ i ] ! ) ) ) ;
329+ phase . fixedWaveP95 . push ( stats ( phase . fixedQuiet . slice ( fixedFrom ) ) . p95 ! ) ;
277330 }
278331 if ( ! values . uncapped ) {
279332 for ( let i = 0 ; i < accounts ; i ++ ) {
@@ -282,6 +335,13 @@ async function measure() {
282335 assert . equal ( ( await r . json ( ) ) . error . code , 'storage_quota_exceeded' ) ;
283336 }
284337 }
338+ if ( values [ 'profile-reader' ] ) {
339+ const reply = once ( child , 'message' , { signal : AbortSignal . timeout ( 10000 ) } ) ;
340+ child . send ( 'quota-phase-profile' ) ;
341+ const [ m ] = await reply ;
342+ assert . equal ( m . phases . length , accounts ) ;
343+ ( before ? prior : current ) . phases . push ( m . phases ) ;
344+ }
285345 } finally {
286346 if ( child . connected ) child . disconnect ( ) ;
287347 assert . equal ( ( await ended ) [ 0 ] , 0 ) ;
@@ -295,7 +355,7 @@ async function measure() {
295355 for ( let i = 0 ; i < accounts ; i ++ ) {
296356 const r = all . get ( owner ( i ) , 'config:' + owner ( i ) ) ! ;
297357 assert . equal ( r . n , rows + processes * 2 ) ;
298- assert . equal ( r . bytes , rows * bytes + processes * 2 * content . length ) ;
358+ assert . equal ( r . bytes , historyBytes + processes * 2 * content . length ) ;
299359 }
300360 assert . equal ( db . prepare ( 'SELECT count(*) AS n FROM oaa_inline_inputs' ) . get ( ) ! . n , 0 ) ;
301361 } finally {
@@ -304,13 +364,16 @@ async function measure() {
304364 const describe = ( p : ReturnType < typeof sample > ) => ( {
305365 upload_ms : stats ( p . uploads ) ,
306366 other_owner_read_ms : stats ( p . quiet ) ,
367+ fixed_other_owner_read_ms : stats ( p . fixedQuiet ) ,
368+ fixed_other_owner_p95_per_wave_ms : p . fixedWaveP95 ,
307369 wave_ms : stats ( p . wave ) ,
308370 client_timer_lag_ms : stats ( p . lag ) ,
309371 max_quiet_response_gap_per_wave_ms : stats ( p . quietGaps ) ,
310372 late_probes : p . lateProbes ,
311373 wave_samples_ms : p . wave . map ( ( n ) => + n . toFixed ( 2 ) ) ,
312374 } ) ;
313375 const summarize = ( x : ReturnType < typeof measurements > ) => ( {
376+ ...( x . phases . length ? { cold_request_phases : x . phases } : { } ) ,
314377 ready_ms : stats ( x . startup ) ,
315378 cold : describe ( x . cold ) ,
316379 warm : describe ( x . warm ) ,
@@ -350,6 +413,8 @@ async function measure() {
350413 rows_per_account : rows ,
351414 restarts,
352415 quota : ! values . uncapped ,
416+ mixed_history : ! ! values [ 'mixed-history' ] ,
417+ index,
353418 forced_helper_timeout : ! ! values [ 'helper-timeout' ] ,
354419 fresh_fixture_inode : ! ! values [ 'fresh-copy' ] ,
355420 per_wave_probes : probes ,
0 commit comments