Socket
Socket
Sign inDemoInstall

loadtest

Package Overview
Dependencies
Maintainers
1
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loadtest - npm Package Compare versions

Comparing version 0.0.4 to 0.1.0

lib/testserver.js

5

index.js

@@ -10,4 +10,5 @@ 'use strict';

// requires
var Log = require('log');
var loadtest = require('./lib/loadtest.js');
var loadserver = require('./lib/loadserver.js');
var testserver = require('./lib/testserver.js');

@@ -22,3 +23,3 @@ // globals

exports.loadTest = loadtest.loadTest;
exports.startServer = loadserver.startServer;
exports.startServer = testserver.startServer;

172

lib/loadtest.js

@@ -24,3 +24,4 @@ 'use strict';

var DEFAULT_OPTIONS = {
concurrency: 100,
noAgent: true,
concurrency: 1,
requestsPerSecond: 1,

@@ -32,6 +33,13 @@ };

* A client for an HTTP connection.
* Params is an object which may have:
* Params is an object which must contain:
* - url: destination URL.
* - requestsPerSecond: how many requests to create from this client.
* - maxRequests: stop sending requests after this global limit is reached.
* - noAgent: if true, do not use connection keep-alive.
* Params may have:
* - method: HTTP method (default GET).
* - payload: contents of request (default empty).
* - maxRequests: stop sending requests after this global limit is reached
* (default unlimited).
* - maxSeconds: stop receiving requests after this number of seconds
* (default unlimited).
* - noAgent: if true, do not use connection keep-alive (default false).
*/

@@ -59,3 +67,3 @@ function HttpClient(params)

var interval = Math.round(1000 / params.requestsPerSecond);
timer = new timing.HighResolutionTimer(interval, makeRequest);
timer = new timing.HighResolutionTimer(interval, self.makeRequest);
}

@@ -66,3 +74,3 @@

*/
function makeRequest()
self.makeRequest = function(callback)
{

@@ -72,2 +80,6 @@ requests += 1;

{
if (callback)
{
callback(null);
}
return timer.stop();

@@ -79,9 +91,53 @@ }

{
options.noAgent = false;
options.agent = false;
}
var get = http.get(options, getConnect(id));
get.on('error', function(error)
if (params.method)
{
options.method = params.method;
}
var message;
if (params.payload)
{
var message;
if (typeof params.payload == 'string')
{
message = params.payload;
}
else if (typeof params.payload == 'object')
{
message = JSON.stringify(params.payload);
}
else
{
log.error('Unrecognized payload: %s', typeof params.payload);
}
options.headers = {
'Content-Length': message.length,
};
}
var request = http.request(options, getConnect(id, function(error, result)
{
if (error)
{
log.error('Connection %s failed: %s', id, error);
}
else
{
log.debug('Connection %s ended', id);
}
latency.end(id);
if (callback)
{
callback(error, result);
}
}));
if (message)
{
request.write(message);
}
request.on('error', function(error)
{
log.error('Error: %s', error.message);
});
request.end();
}

@@ -92,3 +148,3 @@

*/
function getConnect(id)
function getConnect(id, callback)
{

@@ -105,4 +161,3 @@ return function(connection)

{
log.error('Connection %s failed: %s', id, error);
latency.end(id);
callback('Connection ' + id + ' failed: ' + error);

@@ -112,4 +167,7 @@ });

{
log.debug('Connection %s ended', id);
latency.end(id);
if (connection.statusCode >= 300)
{
return callback('Status code ' + connection.statusCode);
}
callback(null, false);
});

@@ -231,3 +289,5 @@ };

* - requestsPerSecond: how many requests per second per client.
* - noAction: if true, then do not use connection keep-alive.
* - maxRequests: how many requests to send
* - maxSeconds: how long to run the tests.
* - noAgent: if true, then do not use connection keep-alive.
* An optional callback will be called if/when the test finishes.

@@ -286,10 +346,14 @@ */

*/
function help()
function help(args)
{
console.log('Usage: %s %s [options] [concurrency] [req/s] URL');
console.log('Usage: %s %s [options] URL', args[0], args[1]);
console.log(' where URL can be a regular HTTP or websocket URL.');
console.log(' If you want to add an index to the URL, place a $index in it.');
console.log(' Options:');
console.log(' --noagent: send Connection: close');
console.log(' --agent: send Connection: keep-alive (default)');
console.log('Options are:');
console.log(' -n requests Number of requests to perform');
console.log(' -c concurrency Number of multiple requests to make');
console.log(' -t timelimit Seconds to max. wait for responses');
console.log(' --rps Requests per second for each client');
console.log(' --noagent Do not use http agent (default)');
console.log(' --agent Use http agent (Connection: keep-alive)');
console.log(' --index param Replace the value of param with an index in the URL');
}

@@ -309,37 +373,28 @@

}
var argument = args[0];
if (parseInt(argument))
if (!args[0].startsWith('-'))
{
if (numbersParsed == 0)
{
options.concurrency = parseInt(argument);
}
else if (numbersParsed == 1)
{
options.requestsPerSecond = parseInt(argument);
}
else
{
log.error('Too many numeric arguments');
return false;
}
args.splice(0, 1);
numbersParsed += 1;
return true;
}
if (!argument.startsWith('--'))
{
return false;
}
if (argument == '--noagent')
// consume the argument
var argument = nextArgument(args);
switch(argument)
{
options.noAgent = true;
args.splice(0, 1);
return true;
case '-n':
options.maxRequests = nextArgument(args);
return true;
case '-c':
options.concurrency = nextArgument(args);
return true;
case '-t':
options.maxSeconds = nextArgument(args);
return true;
case '--rps':
options.requestsPerSecond = nextArgument(args);
return true;
case '--noagent':
return true;
case '--agent':
options.noAgent = false;
return true;
}
if (argument == '--agent')
{
args.splice(0, 1);
return true;
}
console.error('Unknown option %s', argument);

@@ -350,6 +405,15 @@ return false;

/**
* Get the next argument and remove it from the list.
*/
function nextArgument(args)
{
return args.splice(0, 1)[0];
}
/**
* Process command line arguments.
*/
function processArgs(args)
function processArgs(originalArgs)
{
var args = originalArgs.slice(2);
var options = DEFAULT_OPTIONS;

@@ -369,3 +433,3 @@ while (parseArgument(args, options))

}
return help();
return help(originalArgs);
}

@@ -379,4 +443,4 @@ options.url = args[0];

{
processArgs(process.argv.slice(2));
processArgs(process.argv);
}

@@ -9,3 +9,3 @@ 'use strict';

// requires
var async = require('async');
var testing = require('testing');
var Log = require('log');

@@ -45,23 +45,8 @@

{
if (!'pepito'.startsWith('pe'))
{
return callback('Failed to match using startsWith()')
}
if ('pepito'.startsWith('po'))
{
return callback('Invalid match using startsWith()')
}
if (!'pepito'.endsWith('to'))
{
return callback('Failed to match using endsWith()')
}
if ('pepito'.startsWith('po'))
{
return callback('Invalid match using endsWith()')
}
if ('pepito'.replaceAll('p', 'c') != 'cecito')
{
return callback('Invalid replaceAll().');
}
callback(null, true);
testing.assert('pepito'.startsWith('pe'), 'Failed to match using startsWith()', callback);
testing.assert(!'pepito'.startsWith('po'), 'Invalid match using startsWith()', callback);
testing.assert('pepito'.endsWith('to'), 'Failed to match using endsWith()', callback);
testing.assert(!'pepito'.startsWith('po'), 'Invalid match using endsWith()', callback);
testing.assertEquals('pepito'.replaceAll('p', 'c'), 'cecito', 'Invalid replaceAll().', callback);
testing.success(callback);
}

@@ -121,11 +106,5 @@

exports.overwriteObject(first, second);
if (exports.countProperties(second) != 3)
{
return callback('Overwritten should have three properties');
}
if (second.b != 'b')
{
return callback('Property in second should be replaced with first');
}
callback(null, true);
testing.assertEquals(exports.countProperties(second), 3, 'Overwritten should have three properties', callback);
testing.assertEquals(second.b, 'b', 'Property in second should be replaced with first', callback);
testing.success(callback);
}

@@ -142,3 +121,3 @@

};
async.series(tests, callback);
testing.run(tests, callback);
}

@@ -149,12 +128,4 @@

{
exports.test(function(error, result)
{
if (error)
{
log.error('Tests failed: %s', error);
return;
}
log.info('Tests succesful');
});
exports.test(testing.show);
}

@@ -11,5 +11,6 @@ 'use strict';

var loadtest = require('./loadtest.js');
var loadserver = require('./loadserver.js');
var testserver = require('./testserver.js');
var prototypes = require('./prototypes.js');
var testing = require('testing');
var Log = require('log');
var prototypes = require('./prototypes.js');

@@ -33,7 +34,8 @@ // globals

requestsPerSecond: 10,
method: 'POST',
payload: {
hi: 'there',
},
};
loadtest.loadTest(options, function(error, result)
{
callback(error, result);
});
loadtest.loadTest(options, callback);
}

@@ -46,3 +48,3 @@

{
var server = loadserver.startServer(PORT, function(error)
var server = testserver.startServer(PORT, function(error)
{

@@ -76,16 +78,5 @@ if (error)

{
var run = false;
integrationTest(function(error, result)
{
run = true;
callback(error, result);
});
// give it time
setTimeout(function()
{
if (!run)
{
callback('Sample run did not fire');
}
}, 2000);
testing.run({
integration: integrationTest,
}, 2000, callback);
}

@@ -96,11 +87,4 @@

{
exports.test(function(error, result)
{
if (error)
{
return log.error('Tests failed: %s', error);
}
log.info('Tests passed');
});
exports.test(testing.show);
}

@@ -10,3 +10,3 @@ 'use strict';

// requires
var async = require('async');
var testing = require('testing');
var util = require('util');

@@ -168,16 +168,7 @@ var Log = require('log');

var firstId = latency.start();
if (!firstId)
{
return callback('Invalid first latency id');
}
testing.assert(firstId, 'Invalid first latency id', callback);
var secondId = latency.start();
if (!secondId)
{
return callback('Invalid second latency id');
}
if (firstId == secondId)
{
return callback('Repeated latency ids');
}
callback(null, true);
testing.assert(secondId, 'Invalid second latency id', callback);
testing.assert(firstId != secondId, 'Repeated latency ids', callback);
testing.success(callback);
}

@@ -190,10 +181,5 @@

{
var measured = false;
var options = {
maxRequests: 10,
callback: function(error, result)
{
measured = true;
callback(error, result);
},
callback: callback,
};

@@ -206,10 +192,2 @@ var latency = new exports.Latency(options);

}
// give it time
setTimeout(function()
{
if (!measured)
{
callback('Latency did not call back in 10 ms');
}
}, 10);
}

@@ -240,3 +218,3 @@

}
callback(delayMs);
callback();
counter ++;

@@ -275,17 +253,4 @@ var diff = (Date.now() - start) - counter * delayMs;

{
var fired = false;
var timer = new exports.HighResolutionTimer(10, function()
{
fired = true;
callback(null, 'Timer fired');
});
var timer = new exports.HighResolutionTimer(10, callback);
timer.stop();
// give it time
setTimeout(function()
{
if (!fired)
{
callback('Timer did not fire in 20 ms');
}
}, 20);
}

@@ -303,3 +268,3 @@

};
async.series(tests, callback);
testing.run(tests, callback);
}

@@ -310,12 +275,4 @@

{
exports.test(function(error, result)
{
if (error)
{
log.error('Tests failed: %s', error);
return;
}
log.info('Tests succesful');
});
exports.test(testing.show);
}
{
"name": "loadtest",
"version": "0.0.4",
"version": "0.1.0",
"description": "Load test scripts.",

@@ -16,3 +16,3 @@ "homepage": "http://milliearth.org/",

"microtime": "*",
"async": "*",
"testing": "*",
"log": "*"

@@ -24,2 +24,6 @@ },

},
"bin": {
"loadtest": "lib/loadtest.js",
"testserver": "lib/testserver.js"
},
"scripts": {

@@ -26,0 +30,0 @@ "test": "node test.js"

@@ -16,7 +16,7 @@ # loadtest

$ node lib/loadtest.js [URL] or [websocket URL]
$ loadtest [URL] or [websocket URL]
To get online help, run loadtest.js without parameters:
To get online help, run loadtest without parameters:
$ node lib/loadtest.js
$ loadtest

@@ -27,3 +27,3 @@ ### Advanced Usage

$ node lib/loadtest.js [concurrency [request per second]] ...
$ loadtest [-n requests] [-c concurrency] ...

@@ -46,3 +46,3 @@ #### Concurrency

$ node lib/loadserver.js [port]
$ testserver [port]

@@ -101,4 +101,4 @@ It will show the number of requests received per second, the latency in answering requests and the headers for selected requests.

var loadserver = require('loadserver');
loadserver.startServer(8000);
var testserver = require('testserver');
testserver.startServer(8000);

@@ -105,0 +105,0 @@ ### Complete Sample

@@ -13,3 +13,3 @@ 'use strict';

var util = require('util');
var async = require('async');
var testing = require('testing');
var Log = require('log');

@@ -26,3 +26,2 @@

{
var run = false;
var tests = {

@@ -33,15 +32,3 @@ prototypes: prototypes.test,

};
async.series(tests, function(error, result)
{
run = true;
callback(error, result);
});
// give it time
setTimeout(function()
{
if (!run)
{
callback('Package tests did not call back');
}
}, 2200);
testing.run(tests, 2200, callback);
}

@@ -52,13 +39,4 @@

{
exports.test(function(error, result)
{
if (error)
{
log.error('Failure in tests: %s', error);
process.exit(1);
return;
}
log.info('All tests successful: %s', util.inspect(result, true, 10, true));
});
exports.test(testing.show);
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc