Skip to content

Commit

Permalink
Moved benchmark-related utility functions to separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
weekens committed Feb 1, 2019
1 parent d0d074b commit 5c04899
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 151 deletions.
13 changes: 7 additions & 6 deletions benchmarks/abstract-throughput-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
'use strict';

let common = require('../lib/utils/common.js');
let CumulativeAverage = require('./cumulative-average.js');
let bmUtils = require('./utils/benchmark-utils.js');
let CumulativeAverage = require('./utils/cumulative-average.js');
let P = require('bluebird');
let _ = require('underscore');
let tooBusy = require('toobusy-js');
Expand Down Expand Up @@ -50,7 +51,7 @@ class AbstractThroughputBenchmark {
_run(options) {
options = options || {};

let runTime = options.runTime || common.millisecondTime(1, 'minutes');
let runTime = options.runTime || bmUtils.millisecondTime(1, 'minutes');
let logInterval = 1000; // Display interval in milliseconds.
let concurrencyLevel = options.concurrencyLevel || 64;
let startTime = _.now();
Expand Down Expand Up @@ -93,7 +94,7 @@ class AbstractThroughputBenchmark {
let result = () => {
let avgMemUsage = common.transformObject(
avgMemStats,
value => common.humanReadableNumber(value.getAndReset(), { bytes: true }));
value => bmUtils.humanReadableNumber(value.getAndReset(), { bytes: true }));

let result0 = curResult;

Expand Down Expand Up @@ -176,7 +177,7 @@ class AbstractThroughputBenchmark {
run(options) {
options = options || {};

let testTime = options.testTime || common.millisecondTime(1, 'minutes');
let testTime = options.testTime || bmUtils.millisecondTime(1, 'minutes');

return P.resolve()
.then(() => this.setUp())
Expand All @@ -199,8 +200,8 @@ class AbstractThroughputBenchmark {
runWithWarmUp(options) {
options = options || {};

let warmUpTime = options.warmUpTime || common.millisecondTime(30, 'seconds');
let testTime = options.testTime || common.millisecondTime(1, 'minutes');
let warmUpTime = options.warmUpTime || bmUtils.millisecondTime(30, 'seconds');
let testTime = options.testTime || bmUtils.millisecondTime(1, 'minutes');

console.log(`Warming-up for ${warmUpTime / 1000} seconds...`);

Expand Down
153 changes: 153 additions & 0 deletions benchmarks/utils/benchmark-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2016-2018 Untu, Inc.
* This code is licensed under Eclipse Public License - v 1.0.
* The full license text can be found in LICENSE.txt file and
* on the Eclipse official site (https://www.eclipse.org/legal/epl-v10.html).
*/

'use strict';

/**
* Time unit multipliers for getting millisecond time.
*/
const timeUnitMultipliers = {
/**
* Milliseconds multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
milliseconds: function() {
return 1;
},

/**
* Seconds multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
seconds: function() {
return 1000;
},

/**
* Minutes multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
minutes: function() {
return this.seconds() * 60;
},

/**
* Hours multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
hours: function() {
return this.minutes() * 60;
},

/**
* Days multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
days: function() {
return this.hours() * 24;
},

/**
* Weeks multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
weeks: function() {
return this.days() * 7;
},

/**
* Months multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
months: function() {
return this.days() * 31;
},

/**
* Years multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
years: function() {
return this.days() * 365;
}
};

/**
* Converts a number to human-readable form, with order-of-magnitude
* postfix.
*
* Examples: 1000 => 1.0 K; 1500 => 1.5 K; 1000000 => 1.0 M; 1500000 => 1.5 M.
*
* @param {Number|String} n Number to convert.
* @param {Object} [options] Function options.
* - {Boolean} bytes Bytes mode flag. In bytes mode, 1 K = 1024 instead of 1000. False by default.
* - {Number} precision Floating point precision for the resulting number, i.e. how many digits after decimal point.
* - {String} separator Separator label. Default ' '.
* - {Boolean} pair Return result as array of number and unit.
* numbers will be present in the resulting floating point number. 3 by default.
* @returns {String|Array} Human-readable string representation of a number, or input, if it is not supported.
*/
exports.humanReadableNumber = function(n, options) {
let n0 = n;
options = options || {};
options.separator = typeof options.separator === 'string' ? options.separator : ' ';

if (typeof n0 == 'string') {
if (!n0.match(/^[\d]+.?[\d]*$/)) {
return n0;
}

n0 = parseFloat(n);
}

let units = 'KMGTPEZYXWVU';
let thresh = options.bytes ? 1024 : 1000;

// Not Number or NaN.
// Skip non-positive numbers.
if (typeof n0 != 'number' || n0 != +n0 || n0 <= 0 || n0 < thresh) {
return options.pair ? [n, ''] : n;
}

let u = -1;

do {
n0 /= thresh;

u += 1;
}
while (n0 >= thresh);

let resultNumber = parseFloat(n0.toPrecision(options.precision || 3));

return options.pair ? [resultNumber, units.charAt(u)] : resultNumber + options.separator + units.charAt(u);
};

/**
* Converts an input time value from a specified time unit to milliseconds.
*
* @param {Number|String} timeValue Input time value.
* @param {String} timeUnit Time unit string ('minutes', 'hours', 'weeks', 'days', etc.).
* @returns {Number} Time value in milliseconds.
*/
exports.millisecondTime = function(timeValue, timeUnit) {
timeValue = parseInt(timeValue, 10);

let multi = timeUnitMultipliers[timeUnit];

if (!multi) throw new Error('Unknown time unit: ' + timeUnit);

return timeValue * multi.call(timeUnitMultipliers);
};
File renamed without changes.
145 changes: 0 additions & 145 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,6 @@

let _ = require('underscore');

/**
* Time unit multipliers for getting millisecond time.
*/
const timeUnitMultipliers = {
/**
* Milliseconds multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
milliseconds: function() {
return 1;
},

/**
* Seconds multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
seconds: function() {
return 1000;
},

/**
* Minutes multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
minutes: function() {
return this.seconds() * 60;
},

/**
* Hours multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
hours: function() {
return this.minutes() * 60;
},

/**
* Days multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
days: function() {
return this.hours() * 24;
},

/**
* Weeks multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
weeks: function() {
return this.days() * 7;
},

/**
* Months multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
months: function() {
return this.days() * 31;
},

/**
* Years multiplier.
*
* @returns {Number} Multiplier for milliseconds.
*/
years: function() {
return this.days() * 365;
}
};

/**
* Checks if a given input is a plain JS object.
*
Expand Down Expand Up @@ -280,57 +203,6 @@ exports.transformObjectRecursive = function(obj, iter, context, options) {
}, context);
};

/**
* Converts a number to human-readable form, with order-of-magnitude
* postfix.
*
* Examples: 1000 => 1.0 K; 1500 => 1.5 K; 1000000 => 1.0 M; 1500000 => 1.5 M.
*
* @param {Number|String} n Number to convert.
* @param {Object} [options] Function options.
* - {Boolean} bytes Bytes mode flag. In bytes mode, 1 K = 1024 instead of 1000. False by default.
* - {Number} precision Floating point precision for the resulting number, i.e. how many digits after decimal point.
* - {String} separator Separator label. Default ' '.
* - {Boolean} pair Return result as array of number and unit.
* numbers will be present in the resulting floating point number. 3 by default.
* @returns {String|Array} Human-readable string representation of a number, or input, if it is not supported.
*/
exports.humanReadableNumber = function(n, options) {
let n0 = n;
options = options || {};
options.separator = typeof options.separator === 'string' ? options.separator : ' ';

if (typeof n0 == 'string') {
if (!n0.match(/^[\d]+.?[\d]*$/)) {
return n0;
}

n0 = parseFloat(n);
}

let units = 'KMGTPEZYXWVU';
let thresh = options.bytes ? 1024 : 1000;

// Not Number or NaN.
// Skip non-positive numbers.
if (typeof n0 != 'number' || n0 != +n0 || n0 <= 0 || n0 < thresh) {
return options.pair ? [n, ''] : n;
}

let u = -1;

do {
n0 /= thresh;

u += 1;
}
while (n0 >= thresh);

let resultNumber = parseFloat(n0.toPrecision(options.precision || 3));

return options.pair ? [resultNumber, units.charAt(u)] : resultNumber + options.separator + units.charAt(u);
};

/**
* Generates a key-value pair object.
*
Expand All @@ -345,23 +217,6 @@ exports.pair = function(key, value) {
return o;
};

/**
* Converts an input time value from a specified time unit to milliseconds.
*
* @param {Number|String} timeValue Input time value.
* @param {String} timeUnit Time unit string ('minutes', 'hours', 'weeks', 'days', etc.).
* @returns {Number} Time value in milliseconds.
*/
exports.millisecondTime = function(timeValue, timeUnit) {
timeValue = parseInt(timeValue, 10);

let multi = timeUnitMultipliers[timeUnit];

if (!multi) throw new Error('Unknown time unit: ' + timeUnit);

return timeValue * multi.call(timeUnitMultipliers);
};

/**
* Wraps a given operation into a try-catch block, invoking callback with error
* if it was thrown.
Expand Down

0 comments on commit 5c04899

Please sign in to comment.