11cb0ef41Sopenharmony_ci// Test the throughput of the fs.WriteStream class. 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst path = require('path'); 51cb0ef41Sopenharmony_ciconst common = require('../common.js'); 61cb0ef41Sopenharmony_ciconst fs = require('fs'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst tmpdir = require('../../test/common/tmpdir'); 91cb0ef41Sopenharmony_citmpdir.refresh(); 101cb0ef41Sopenharmony_ciconst filename = path.resolve(tmpdir.path, 111cb0ef41Sopenharmony_ci `.removeme-benchmark-garbage-${process.pid}`); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 141cb0ef41Sopenharmony_ci dur: [5], 151cb0ef41Sopenharmony_ci encodingType: ['buf', 'asc', 'utf'], 161cb0ef41Sopenharmony_ci size: [2, 1024, 65535, 1024 * 1024], 171cb0ef41Sopenharmony_ci}); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cifunction main({ dur, encodingType, size }) { 201cb0ef41Sopenharmony_ci let encoding; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci let chunk; 231cb0ef41Sopenharmony_ci switch (encodingType) { 241cb0ef41Sopenharmony_ci case 'buf': 251cb0ef41Sopenharmony_ci chunk = Buffer.alloc(size, 'b'); 261cb0ef41Sopenharmony_ci break; 271cb0ef41Sopenharmony_ci case 'asc': 281cb0ef41Sopenharmony_ci chunk = 'a'.repeat(size); 291cb0ef41Sopenharmony_ci encoding = 'ascii'; 301cb0ef41Sopenharmony_ci break; 311cb0ef41Sopenharmony_ci case 'utf': 321cb0ef41Sopenharmony_ci chunk = 'ü'.repeat(Math.ceil(size / 2)); 331cb0ef41Sopenharmony_ci encoding = 'utf8'; 341cb0ef41Sopenharmony_ci break; 351cb0ef41Sopenharmony_ci default: 361cb0ef41Sopenharmony_ci throw new Error(`invalid encodingType: ${encodingType}`); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci try { 401cb0ef41Sopenharmony_ci fs.unlinkSync(filename); 411cb0ef41Sopenharmony_ci } catch { 421cb0ef41Sopenharmony_ci // Continue regardless of error. 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci let started = false; 461cb0ef41Sopenharmony_ci let ended = false; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci const f = fs.createWriteStream(filename); 491cb0ef41Sopenharmony_ci f.on('drain', write); 501cb0ef41Sopenharmony_ci f.on('open', write); 511cb0ef41Sopenharmony_ci f.on('close', done); 521cb0ef41Sopenharmony_ci f.on('finish', () => { 531cb0ef41Sopenharmony_ci ended = true; 541cb0ef41Sopenharmony_ci const written = fs.statSync(filename).size / 1024; 551cb0ef41Sopenharmony_ci try { 561cb0ef41Sopenharmony_ci fs.unlinkSync(filename); 571cb0ef41Sopenharmony_ci } catch { 581cb0ef41Sopenharmony_ci // Continue regardless of error. 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci bench.end(written / 1024); 611cb0ef41Sopenharmony_ci }); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci function write() { 651cb0ef41Sopenharmony_ci if (!started) { 661cb0ef41Sopenharmony_ci started = true; 671cb0ef41Sopenharmony_ci setTimeout(() => { 681cb0ef41Sopenharmony_ci f.end(); 691cb0ef41Sopenharmony_ci }, dur * 1000); 701cb0ef41Sopenharmony_ci bench.start(); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci while (false !== f.write(chunk, encoding)); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci function done() { 771cb0ef41Sopenharmony_ci if (!ended) 781cb0ef41Sopenharmony_ci f.emit('finish'); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci} 81