<commit_before>// @noflow

'use strict';

const assert = require('assert');
const cp = require('child_process');

// Clocks the time taken to execute a test per cycle (secs).
function clock(count, fn) {
  const start = process.hrtime.bigint();
  for (let i = 0; i < count; ++i) {
    fn();
  }
  return Number(process.hrtime.bigint() - start);
}

if (require.main === module) {
  const modulePath = process.env.BENCHMARK_MODULE_PATH;
  assert(typeof modulePath === 'string');
  assert(process.send);
  const module = require(modulePath);

  clock(7, module.measure); // warm up
  global.gc();
  process.nextTick(() => {
    const memBaseline = process.memoryUsage().heapUsed;
    const clocked = clock(module.count, module.measure);
    process.send({
      name: module.name,
      clocked: clocked / module.count,
      memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
    });
  });
}

function sampleModule(modulePath) {
  return new Promise((resolve, reject) => {
    const env = { BENCHMARK_MODULE_PATH: modulePath };
    const child = cp.fork(__filename, { env });
    let message;
    let error;

    child.on('message', msg => (message = msg));
    child.on('error', e => (error = e));
    child.on('close', () => {
      if (message) {
        return resolve(message);
      }
      reject(error || new Error('Forked process closed without error'));
    });
  }).then(result => {
    global.gc();
    return result;
  });<commit_msg>Benchmark: set 'NODE_ENV=production' before running the benchmarks (#2434)<commit_after>// @noflow

'use strict';

const assert = require('assert');
const cp = require('child_process');

// Clocks the time taken to execute a test per cycle (secs).
function clock(count, fn) {
  const start = process.hrtime.bigint();
  for (let i = 0; i < count; ++i) {
    fn();
  }
  return Number(process.hrtime.bigint() - start);
}

if (require.main === module) {
  const modulePath = process.env.BENCHMARK_MODULE_PATH;
  assert(typeof modulePath === 'string');
  assert(process.send);
  const module = require(modulePath);

  clock(7, module.measure); // warm up
  global.gc();
  process.nextTick(() => {
    const memBaseline = process.memoryUsage().heapUsed;
    const clocked = clock(module.count, module.measure);
    process.send({
      name: module.name,
      clocked: clocked / module.count,
      memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
    });
  });
}

function sampleModule(modulePath) {
  return new Promise((resolve, reject) => {
    const env = {
      NODE_ENV: 'production',
      BENCHMARK_MODULE_PATH: modulePath,
    };
    const child = cp.fork(__filename, { env });
    let message;
    let error;

    child.on('message', msg => (message = msg));
    child.on('error', e => (error = e));
    child.on('close', () => {
      if (message) {
        return resolve(message);
      }
      reject(error || new Error('Forked process closed without error'));
    });
  }).then(result => {
    global.gc();
    return result;
  });
{'fraction_non_alphanumeric': 0.16029023746701848, 'fraction_numerical': 0.002638522427440633, 'mean_word_length': 3.2478991596638656, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5973325', 'n_tokens_mistral': 965, 'n_tokens_neox': 950, 'n_words': 241}