<commit_before>/*!
 * B - Benchmarks for Node.js.
 *
 * <NAME> <<EMAIL>>
 * MIT License.
 */

/**
 * Dependencies.
 */
var EventEmitter = require('events').EventEmitter;

/**
 * Benchmark constructor.
 *
 * @param {String} name
 * @param {Object} reporter
 * @api public
 */
function Benchmark(name, reporter) {
  this._name = name;
  this._reporter = reporter;
}

/**
 * Inherit from `EventEmitter`.
 */
Benchmark.prototype.__proto__ = EventEmitter.prototype

/**
 * Set a reporter.
 *
 * @type {Object} reporter
 * @api public
 */
Benchmark.prototype.reporter = function(reporter) {
  this._reporter = reporter;
  return this;
};

/**
 * Delegate report to the reporter.
 *
 * @api private
 */
Benchmark.prototype.report = function() {
  this._reporter.report(this._name, this.result(), this._iterations);
  this.emit('done')
};

/**
 * Run the benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api public
 */
Benchmark.prototype.run = function(iterations, fn) {
  if (fn.length < 2) {
    this.sync(iterations, fn);
  } else {
    this.async(iterations, fn);
  }
};

/**
 * Start the timer.
 *
 * @api private
 */
Benchmark.prototype.start = function() {
  this.emit('start')
  this._start = process.hrtime()
};

/**
 * Stop the timer.
 *
 * @api private
 */
Benchmark.prototype.stop = function() {
  this._end = process.hrtime(this._start)
  this.emit('stop')
};

/**
 * Calculate the benchmark result.
 *
 * @returns {Number}
 * @api privagte
 */
Benchmark.prototype.result = function() {
  return this._end[0]*1000000000 + this._end[1]
};

/**
 * Run synchronous benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api private
 */
Benchmark.prototype.sync = function(iterations, fn) {
  this._iterations = iterations
  this.start();
  while (iterations-- > 0) fn(iterations);
  this.stop();
  this.report();
};

/**
 * Run asynchronous benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api private
 */
Benchmark.prototype.async = function(iterations, fn) {
  this._iterations = iterations
  var self = this
  var i = iterations
  if (iterations <= 0) done()
 
  this.start();

  while (--i >= 0) fn(i, done)  
  
  function done () {
    if (--iterations <= 0) {
      self.stop();
      self.report();
    }
  }
}

/**
 * Expose `Benchmark`.
 */
module.exports = Benchmark;<commit_msg>made iteration counter more predictable<commit_after>/*!
 * B - Benchmarks for Node.js.
 *
 * <NAME> <<EMAIL>>
 * MIT License.
 */

/**
 * Dependencies.
 */
var EventEmitter = require('events').EventEmitter;

/**
 * Benchmark constructor.
 *
 * @param {String} name
 * @param {Object} reporter
 * @api public
 */
function Benchmark(name, reporter) {
  this._name = name;
  this._reporter = reporter;
}

/**
 * Inherit from `EventEmitter`.
 */
Benchmark.prototype.__proto__ = EventEmitter.prototype

/**
 * Set a reporter.
 *
 * @type {Object} reporter
 * @api public
 */
Benchmark.prototype.reporter = function(reporter) {
  this._reporter = reporter;
  return this;
};

/**
 * Delegate report to the reporter.
 *
 * @api private
 */
Benchmark.prototype.report = function() {
  this._reporter.report(this._name, this.result(), this._iterations);
  this.emit('done')
};

/**
 * Run the benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api public
 */
Benchmark.prototype.run = function(iterations, fn) {
  if (fn.length < 2) {
    this.sync(iterations, fn);
  } else {
    this.async(iterations, fn);
  }
};

/**
 * Start the timer.
 *
 * @api private
 */
Benchmark.prototype.start = function() {
  this.emit('start')
  this._start = process.hrtime()
};

/**
 * Stop the timer.
 *
 * @api private
 */
Benchmark.prototype.stop = function() {
  this._end = process.hrtime(this._start)
  this.emit('stop')
};

/**
 * Calculate the benchmark result.
 *
 * @returns {Number}
 * @api privagte
 */
Benchmark.prototype.result = function() {
  return this._end[0]*1000000000 + this._end[1]
};

/**
 * Run synchronous benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api private
 */
Benchmark.prototype.sync = function(iterations, fn) {
  this._iterations = iterations
  this.start();
  for (var i = 1; i < iterations; i++) fn(i);
  this.stop();
  this.report();
};

/**
 * Run asynchronous benchmark.
 *
 * @param {Number} iterations
 * @param {Function} function to be benchmarked
 * @api private
 */
Benchmark.prototype.async = function(iterations, fn) {
  this._iterations = iterations
  var self = this
  if (iterations <= 0) done()
 
  this.start();

  for (var i = 1; i <= this._iterations; i++) fn(i, done)  
  
  function done () {
    if (--iterations <= 0) {
      self.stop();
      self.report();
    }
  }
}

/**
 * Expose `Benchmark`.
 */
module.exports = Benchmark;
{'fraction_non_alphanumeric': 0.16520139030873032, 'fraction_numerical': 0.006951543651604989, 'mean_word_length': 3.2613240418118465, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2591614', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1636, 'n_words': 392}