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_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst tmpdir = require('../../test/common/tmpdir');
101cb0ef41Sopenharmony_citmpdir.refresh();
111cb0ef41Sopenharmony_ciconst filename = path.resolve(tmpdir.path,
121cb0ef41Sopenharmony_ci                              `.removeme-benchmark-garbage-${process.pid}`);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
151cb0ef41Sopenharmony_ci  encodingType: ['buf', 'asc', 'utf'],
161cb0ef41Sopenharmony_ci  filesize: [1000 * 1024],
171cb0ef41Sopenharmony_ci  highWaterMark: [1024, 4096, 65535, 1024 * 1024],
181cb0ef41Sopenharmony_ci  n: 1024,
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifunction main(conf) {
221cb0ef41Sopenharmony_ci  const { encodingType, highWaterMark, filesize } = conf;
231cb0ef41Sopenharmony_ci  let { n } = conf;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  let encoding = '';
261cb0ef41Sopenharmony_ci  switch (encodingType) {
271cb0ef41Sopenharmony_ci    case 'buf':
281cb0ef41Sopenharmony_ci      encoding = null;
291cb0ef41Sopenharmony_ci      break;
301cb0ef41Sopenharmony_ci    case 'asc':
311cb0ef41Sopenharmony_ci      encoding = 'ascii';
321cb0ef41Sopenharmony_ci      break;
331cb0ef41Sopenharmony_ci    case 'utf':
341cb0ef41Sopenharmony_ci      encoding = 'utf8';
351cb0ef41Sopenharmony_ci      break;
361cb0ef41Sopenharmony_ci    default:
371cb0ef41Sopenharmony_ci      throw new Error(`invalid encodingType: ${encodingType}`);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  // Make file
411cb0ef41Sopenharmony_ci  const buf = Buffer.allocUnsafe(filesize);
421cb0ef41Sopenharmony_ci  if (encoding === 'utf8') {
431cb0ef41Sopenharmony_ci    // ü
441cb0ef41Sopenharmony_ci    for (let i = 0; i < buf.length; i++) {
451cb0ef41Sopenharmony_ci      buf[i] = i % 2 === 0 ? 0xC3 : 0xBC;
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci  } else if (encoding === 'ascii') {
481cb0ef41Sopenharmony_ci    buf.fill('a');
491cb0ef41Sopenharmony_ci  } else {
501cb0ef41Sopenharmony_ci    buf.fill('x');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  try {
541cb0ef41Sopenharmony_ci    fs.unlinkSync(filename);
551cb0ef41Sopenharmony_ci  } catch {
561cb0ef41Sopenharmony_ci    // Continue regardless of error.
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci  const ws = fs.createWriteStream(filename);
591cb0ef41Sopenharmony_ci  ws.on('close', runTest.bind(null, filesize, highWaterMark, encoding, n));
601cb0ef41Sopenharmony_ci  ws.on('drain', write);
611cb0ef41Sopenharmony_ci  write();
621cb0ef41Sopenharmony_ci  function write() {
631cb0ef41Sopenharmony_ci    do {
641cb0ef41Sopenharmony_ci      n--;
651cb0ef41Sopenharmony_ci    } while (false !== ws.write(buf) && n > 0);
661cb0ef41Sopenharmony_ci    if (n === 0)
671cb0ef41Sopenharmony_ci      ws.end();
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_cifunction runTest(filesize, highWaterMark, encoding, n) {
721cb0ef41Sopenharmony_ci  assert(fs.statSync(filename).size === filesize * n);
731cb0ef41Sopenharmony_ci  const rs = fs.createReadStream(filename, {
741cb0ef41Sopenharmony_ci    highWaterMark,
751cb0ef41Sopenharmony_ci    encoding,
761cb0ef41Sopenharmony_ci  });
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  rs.on('open', () => {
791cb0ef41Sopenharmony_ci    bench.start();
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  let bytes = 0;
831cb0ef41Sopenharmony_ci  rs.on('data', (chunk) => {
841cb0ef41Sopenharmony_ci    bytes += chunk.length;
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  rs.on('end', () => {
881cb0ef41Sopenharmony_ci    try {
891cb0ef41Sopenharmony_ci      fs.unlinkSync(filename);
901cb0ef41Sopenharmony_ci    } catch {
911cb0ef41Sopenharmony_ci      // Continue regardless of error.
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci    // MB/sec
941cb0ef41Sopenharmony_ci    bench.end(bytes / (1024 * 1024));
951cb0ef41Sopenharmony_ci  });
961cb0ef41Sopenharmony_ci}
97