@@ -507,31 +507,59 @@ function _setupTests(testType) {
507507 return str ;
508508 }
509509
510- async function _testLargeFileRW ( filePath , sizeBytes ) {
510+ function stringToArrayBuffer ( string ) {
511+ const buffer = new ArrayBuffer ( string . length ) ;
512+ const array = new Uint8Array ( buffer ) ;
513+ for ( let i = 0 ; i < string . length ; i ++ ) {
514+ array [ i ] = string . charCodeAt ( i ) ;
515+ }
516+ return buffer ;
517+ }
518+
519+ async function _testLargeFileRW ( filePath , sizeBytes , isBinary ) {
511520 console . log ( "creating random string of size" , sizeBytes ) ;
512- const stringWrittenToFile = createRandomString ( sizeBytes ) ;
513- console . log ( "random string created" ) ;
521+ let timerLabel = "random data generated " + sizeBytes / 1024 / 1024 + "MB" ;
522+ console . time ( timerLabel ) ;
523+ let contentWrittenToFile = createRandomString ( sizeBytes ) ;
524+ const binaryConversion = stringToArrayBuffer ( contentWrittenToFile ) ;
525+ if ( isBinary ) {
526+ contentWrittenToFile = binaryConversion ;
527+ }
528+ console . timeEnd ( timerLabel ) ;
529+
530+ timerLabel = "File write completed " + sizeBytes / 1024 / 1024 + "MB" ;
531+ console . time ( timerLabel ) ;
514532 let resolveP , rejectP ;
515533 const writePromise = new Promise ( ( resolve , reject ) => { resolveP = resolve ; rejectP = reject ; } ) ;
516- fs . writeFile ( filePath , stringWrittenToFile , "utf8" , ( _err ) => {
534+ fs . writeFile ( filePath , contentWrittenToFile , isBinary ? fs . BYTE_ARRAY_ENCODING : "utf8" , ( _err ) => {
517535 if ( _err ) {
518536 rejectP ( _err ) ;
519537 return ;
520538 }
521539 resolveP ( ) ;
522540 } ) ;
523541 await writePromise ;
542+ console . timeEnd ( timerLabel ) ;
543+
544+ timerLabel = "File read completed " + sizeBytes / 1024 / 1024 + "MB" ;
545+ console . time ( timerLabel ) ;
524546
525547 const readPromise = new Promise ( ( resolve , reject ) => { resolveP = resolve ; rejectP = reject ; } ) ;
526- fs . readFile ( filePath , "utf8" , ( _err , data ) => {
548+ fs . readFile ( filePath , isBinary ? fs . BYTE_ARRAY_ENCODING : "utf8" , ( _err , data ) => {
527549 if ( _err ) {
528550 rejectP ( _err ) ;
529551 return ;
530552 }
531553 resolveP ( data ) ;
532554 } ) ;
533- const strReadFromFile = await readPromise ;
534- expect ( strReadFromFile ) . to . eql ( stringWrittenToFile ) ;
555+ const dataReadFromFile = await readPromise ;
556+ console . timeEnd ( timerLabel ) ;
557+
558+ timerLabel = "File verify completed " + sizeBytes / 1024 / 1024 + "MB" ;
559+ console . time ( timerLabel ) ;
560+
561+ expect ( dataReadFromFile ) . to . eql ( contentWrittenToFile ) ;
562+ console . timeEnd ( timerLabel ) ;
535563 }
536564
537565 let sizeMBs = [ 1 , 2 , 4 , 8 , 16 ] ;
@@ -547,6 +575,11 @@ function _setupTests(testType) {
547575 const filePath = `${ testPath } /browserWrite.txt` ;
548576 await _testLargeFileRW ( filePath , sizeMB * 1024 * 1024 ) ; // Roughly 4MB considering 1 byte per char
549577 } ) . timeout ( 60000 ) ;
578+
579+ it ( `Should phoenix ${ testType } read and write a large Binary file of size ${ sizeMB } MB` , async function ( ) {
580+ const filePath = `${ testPath } /browserWrite.txt` ;
581+ await _testLargeFileRW ( filePath , sizeMB * 1024 * 1024 , true ) ; // Roughly 4MB considering 1 byte per char
582+ } ) . timeout ( 60000 ) ;
550583 }
551584}
552585
0 commit comments