2011-03-10 22:43:36 -05:00
|
|
|
/**
|
|
|
|
* Common performance testing functionality
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2013 Mike Gerwitz
|
2011-03-10 22:43:36 -05:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2011-03-10 22:43:36 -05:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-03-10 22:43:36 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores start time
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
var start = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes a module from the lib directory
|
|
|
|
*
|
|
|
|
* @param {string} name module name
|
|
|
|
*
|
|
|
|
* @return {Object} module exports
|
|
|
|
*/
|
|
|
|
exports.require = function( name )
|
|
|
|
{
|
|
|
|
return require( '../../lib/' + name );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple wrapper to perform testing and output the result
|
|
|
|
*
|
2011-03-10 23:07:52 -05:00
|
|
|
* The count is not used to call the function multiple times, because that would
|
|
|
|
* greatly impact the test results. Instead, you should pass the number of times
|
|
|
|
* the test was performed in a loop.
|
|
|
|
*
|
|
|
|
* @param {function()} test performance test to perform
|
|
|
|
* @param {number} count number of times the test was performed
|
|
|
|
* @param {string=} desc test description
|
2011-03-10 22:43:36 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-10 23:07:52 -05:00
|
|
|
exports.test = function( test, count, desc )
|
2011-03-10 22:43:36 -05:00
|
|
|
{
|
|
|
|
exports.start();
|
|
|
|
test();
|
2011-03-10 23:07:52 -05:00
|
|
|
exports.report( count, desc );
|
2011-03-10 22:43:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the timer
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.start = function()
|
|
|
|
{
|
|
|
|
start = ( new Date() ).getTime();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the time elapsed, followed by the description (if available)
|
|
|
|
*
|
2011-03-10 23:07:52 -05:00
|
|
|
* @param {number} count number of times the test was performed
|
|
|
|
* @param {string=} desc test description
|
2011-03-10 22:43:36 -05:00
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-03-10 23:07:52 -05:00
|
|
|
exports.report = function( count, desc )
|
2011-03-10 22:43:36 -05:00
|
|
|
{
|
2011-03-10 23:07:52 -05:00
|
|
|
count = +count;
|
|
|
|
desc = desc || '';
|
2011-03-10 22:43:36 -05:00
|
|
|
|
|
|
|
var end = ( new Date() ).getTime(),
|
2011-03-10 23:07:52 -05:00
|
|
|
total = ( ( end - start ) / 1000 ).toFixed( 3 ),
|
2011-03-11 19:14:10 -05:00
|
|
|
pers = ( total / count ).toFixed( 10 )
|
2011-03-10 22:43:36 -05:00
|
|
|
;
|
|
|
|
|
2011-03-10 23:07:52 -05:00
|
|
|
console.log( total + "s (x" + count + " = " + pers + "s each)" +
|
2011-03-10 22:43:36 -05:00
|
|
|
( ( desc ) ? ( ': ' + desc ) : '' )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|