1- import { createReadStream } from 'node:fs'
2- import { writeFile , unlink } from 'node:fs/promises'
31import { randomBytes } from 'node:crypto'
2+ import { createReadStream } from 'node:fs'
3+ import { unlink , writeFile } from 'node:fs/promises'
4+
5+ import { envBaseSchema } from '../lib/schemas.js'
6+
7+ // Get the default value from the schema
8+ const optionals = envBaseSchema . structure ?. inner ?. optional || [ ]
9+ const hwmNode = optionals . find ( ( n : { key : string } ) => n . key === 'STORAGE_HIGH_WATER_MARK' )
10+ const CURRENT_HIGH_WATER_MARK = ( hwmNode ?. default as number ) ?? 1024 * 1024
11+
12+ function formatSize ( bytes : number ) : string {
13+ if ( bytes >= 1024 * 1024 ) return `${ bytes / ( 1024 * 1024 ) } MB`
14+ return `${ bytes / 1024 } KB`
15+ }
16+
17+ function getLabel ( value : number ) : string {
18+ const size = formatSize ( value )
19+ if ( value === 64 * 1024 ) return `${ size } (Node.js default)`
20+ if ( value === CURRENT_HIGH_WATER_MARK ) return `${ size } (current)`
21+ return size
22+ }
423
524const HIGH_WATER_MARKS = [
6- { name : '64KB (default)' , value : 64 * 1024 } ,
7- { name : '128KB' , value : 128 * 1024 } ,
8- { name : '256KB (current)' , value : 256 * 1024 } ,
9- { name : '512KB' , value : 512 * 1024 } ,
10- { name : '1MB' , value : 1024 * 1024 } ,
11- ]
25+ { value : 64 * 1024 } ,
26+ { value : 128 * 1024 } ,
27+ { value : 256 * 1024 } ,
28+ { value : 512 * 1024 } ,
29+ { value : 1024 * 1024 } ,
30+ ] . map ( ( h ) => ( { name : getLabel ( h . value ) , value : h . value } ) )
1231
1332const FILE_SIZES = [
1433 { name : '100MB' , bytes : 100 * 1024 * 1024 } ,
@@ -25,7 +44,7 @@ async function benchmark() {
2544 await writeFile ( testFile , randomBytes ( fileSize . bytes ) )
2645
2746 console . log ( `\n=== ${ fileSize . name } File ===` )
28- console . log ( 'Buffer Size' . padEnd ( 20 ) + 'Throughput (MB/s)' . padEnd ( 20 ) + ' Avg Duration (ms)' )
47+ console . log ( ` ${ 'Buffer Size' . padEnd ( 20 ) } ${ 'Throughput (MB/s)' . padEnd ( 20 ) } Avg Duration (ms)` )
2948 console . log ( '-' . repeat ( 60 ) )
3049
3150 for ( const hwm of HIGH_WATER_MARKS ) {
@@ -43,12 +62,10 @@ async function benchmark() {
4362 }
4463
4564 const avgDuration = durations . reduce ( ( a , b ) => a + b ) / durations . length
46- const throughput = ( fileSize . bytes / ( 1024 * 1024 ) ) / ( avgDuration / 1000 )
65+ const throughput = fileSize . bytes / ( 1024 * 1024 ) / ( avgDuration / 1000 )
4766
4867 console . log (
49- hwm . name . padEnd ( 20 ) +
50- throughput . toFixed ( 2 ) . padEnd ( 20 ) +
51- avgDuration . toFixed ( 2 )
68+ `${ hwm . name . padEnd ( 20 ) } ${ throughput . toFixed ( 2 ) . padEnd ( 20 ) } ${ avgDuration . toFixed ( 2 ) } ` ,
5269 )
5370 }
5471
@@ -57,4 +74,4 @@ async function benchmark() {
5774 }
5875}
5976
60- benchmark ( ) . catch ( console . error )
77+ await benchmark ( )
0 commit comments