<commit_before>// wwBenchmark.js
// ==============

/* eslint no-eval: 0 */

'use strict';

const benchmark = require('benchmark');

function startBenchmark (baselineCode, testCode, fnc) {
  // TODO:
  // firm time cutoff regardless of whether test finishes.

  // default options:
  benchmark.options.maxTime = 0.05; // seems to be the minimum for test cycles - 5.

  self.baselineCode = baselineCode.replace(/use strict/g, '');
  self.testCode = testCode.replace(/use strict/g, '');
  self.fnc = fnc;

  const stockTest = {
    name: 'stock test',
    setup () { try { eval(self.baselineCode); } catch (err) { console.error(err); } },
    fn () { eval(fnc); }
  };
  const userTest = {
    name: 'user test',
    setup () { try { eval(testCode); } catch (err) { console.error(err); } },
    fn () { eval(fnc); }
  };

  const suite = benchmark.Suite();

  let results = null;

  return new Promise(resolve => {
    suite
    .add(stockTest)
    .add(userTest)
    .on('cycle', e => console.log(String(e.target)))
    .on('complete', function () { results = this.filter('fastest').map('name');
      console.log(`Fastest is ${results}`);
    })
    .run({ async: true });
    resolve(results);
  });
}

self.onmessage = e => {
  const stockCode = e.data[0][0];
  const userCode = e.data[0][1];
  const benchmarkFnCall = e.data[1];

  const result = startBenchmark(stockCode, userCode, benchmarkFnCall);
  /*
  let timeUsed = Number.POSITIVE_INFINITY;
  try {
    eval(code);
    const start = performance.now();
    eval(benchmarkFnCall);
    const finish = performance.now();
    timeUsed = finish - start;
  }
  catch (err) {
    console.error(err);
  }
 */

  // self.postMessage(result);
};
<commit_msg>tweak benchmarking and return data to main thread<commit_after>// wwBenchmark.js
// ==============

/* eslint no-eval: 0 */

'use strict';

const benchmark = require('benchmark');

function startBenchmark (baselineCode, testCode, fnc) {
  // TODO:
  // firm time cutoff regardless of whether test finishes.

  // default options:
  benchmark.options.maxTime = 0.05; // seems to be the minimum for test cycles - 5.
  // benchmark.options.maxTime = Number.NEGATIVE_INFINITY;
  benchmark.options.minSamples = 4;
  // benchmark.options.initCount = 10;

  self.baselineCode = baselineCode.replace(/use strict/g, '');
  self.testCode = testCode.replace(/use strict/g, '');
  self.fnc = fnc;

  const stockTest = {
    name: 'stock test',
    setup () { try { eval(self.baselineCode); } catch (err) { console.error(err); } },
    fn () { eval(fnc); }
  };
  const userTest = {
    name: '<NAME>',
    setup () { try { eval(testCode); } catch (err) { console.error(err); } },
    fn () { eval(fnc); }
  };

  const suite = benchmark.Suite();

  // let results = null;
  const results = {
    fastest: null,
    testData: []
  };

  return new Promise(resolve => {
    suite
    .add(stockTest)
    .add(userTest)
    .on('cycle', e => results.testData.push(String(e.target)))
    .on('complete', function () { results.fastest = this.filter('fastest').map('name');
      resolve(results);
      // console.log(`Fastest is ${results}`);
    })
    .run({ async: true });
  });
}

self.onmessage = e => {
  const stockCode = e.data[0][0];
  const userCode = e.data[0][1];
  const benchmarkFnCall = e.data[1];

  startBenchmark(stockCode, userCode, benchmarkFnCall)
    .then(result => {
      self.postMessage(result);
    });
  /*
  let timeUsed = Number.POSITIVE_INFINITY;
  try {
    eval(code);
    const start = performance.now();
    eval(benchmarkFnCall);
    const finish = performance.now();
    timeUsed = finish - start;
  }
  catch (err) {
    console.error(err);
  }
 */

 // self.postMessage(result);
};

{'fraction_non_alphanumeric': 0.17442799461641992, 'fraction_numerical': 0.006191117092866756, 'mean_word_length': 3.043525571273123, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, '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': '6979627', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1237, 'n_words': 281}