TruAPI sample scripts
The following are examples of TruAPI scripts.
'use strict'; exports = module.exports = function (vuser) { /* init action */ vuser.init('Base Vuser init', function (svc, done) { svc.logger.info('Vuser %s init', vuser.getVuserId()); done(); }); /* main action */ vuser.action('Base Vuser action', function (svc, done) { svc.logger.info('Vuser %s running', vuser.getVuserId()); svc.transaction.start('tran1'); svc.transaction.end('tran1', svc.transaction.PASS); svc.transaction.start('tran2'); function test() { svc.transaction.end('tran2', svc.transaction.PASS); done(); } setTimeout(test, 500); }); /* end action */ vuser.end('Base Vuser end', function (svc, done) { svc.logger.info('Vuser %s end', vuser.getVuserId()); done(); }); };
'use strict'; var tunnel = require('tunnel'), url = require('url'); exports = module.exports = function (vuser) { var requestOptions; vuser.init('simple HTTP request tests', function(svc, done) { var proxy; svc.logger.info('Init http test %s', vuser.getVuserId()); requestOptions = { hostname: 'nodejs.org', port: 80, path: '/' }; if (process.env.http_proxy) { /* update the http_proxy setting */ proxy = url.parse(process.env.http_proxy); svc.logger.info('set http proxy %s:%s', proxy.hostname, proxy.port); requestOptions.agent = tunnel.httpOverHttp({ maxSockets: 100, proxy: { host: proxy.hostname, port: proxy.port } }); } done(); }); vuser.action('simple HTTP request tests', function (svc, done) { svc.logger.info('Start http test %s', vuser.getVuserId()); /* send request to server */ svc.transaction.start('httpTest'); function sendRequest() { svc.http.request(requestOptions, function (res) { svc.logger.info('http response statusCode = %d', res.statusCode); res.on('end', function() { svc.transaction.end('httpTest', svc.transaction.PASS); done(); }).on('error', function() { svc.transaction.end('httpTest', svc.transaction.FAIL); done(); }); }).on('error', function (err) { svc.logger.error('http error %s', err.toString()); svc.transaction.end('httpTest', svc.transaction.FAIL); done(); }).end(); } svc.transaction.thinkTime('httpTest', 1000 * 2, function () { sendRequest(); }); }); };
* Test name: request_test * Script name: request_test.js */ 'use strict'; exports = module.exports = function (vuser) { var vuserId, requestOptions; /* prepare test data */ vuserId = vuser.getVuserId(); /* init action */ vuser.init('Vuser init action', function (svc, done) { svc.logger.info('Vuser %s init', vuserId); requestOptions = { url: 'http://nodejs.org' }; if (process.env.http_proxy) { requestOptions.proxy = process.env.http_proxy; } done(); }); /* main action */ vuser.action('Vuser main action', function (svc, done) { svc.logger.info('Vuser %s running', vuserId); /* send request to server */ function sendRequest() { svc.request(requestOptions, function (err, res, body) { if (err) { svc.logger.error('request error %s', err.toString()); svc.transaction.end('requestTest', svc.transaction.FAIL); done(); return; } /* close the transaction */ svc.transaction.end('requestTest', svc.transaction.PASS); done(); }); } svc.transaction.start('requestTest'); svc.transaction.thinkTime('requestTest', 1000 * 5, function () { sendRequest(); }); }); };
/* Test name: urls_test * Script name: urls_test.js */ 'use strict'; var path = require('path'); exports = module.exports = function (vuser) { var vuserId, proxy, urlListFile, urlList; /* prepare test data */ vuserId = vuser.getVuserId(); urlListFile = 'urls.json'; urlList = []; proxy = process.env.http_proxy ? process.env.http_proxy : undefined; /* init action */ vuser.init('Vuser init action', function (svc, done) { svc.logger.info('Vuser %s init', vuserId); /* load url list */ urlListFile = path.resolve(__dirname, urlListFile); svc.logger.info('load url list from %s', urlListFile); try { urlList = require(urlListFile); } catch (err) { svc.logger.error('Cannot load url list from %s', urlListFile); } done(); }); /* test URL item */ function testUrlItem(svc, urlItem, callback) { var reqOpts; callback = callback || function () {}; urlItem = urlItem || {}; reqOpts = { url: urlItem.url, proxy: proxy }; svc.logger.info('Testing URL %s', urlItem.url); svc.request(reqOpts, function (err, res, body) { if (err) { svc.logger.error('request error %s', err.toString()); } callback(); }); } /* main action */ vuser.action('Vuser main action', function (svc, done) { var idx, len; idx = 0; len = urlList.length; svc.logger.info('Test Url list length is %d', len); if (urlList.length <= 0) { svc.logger.error('An invalid Url list.'); done(); return; } function onCallback(err) { if (err) { svc.logger.error('Error: %s', err.toString()); } idx++; if (idx < len) { /* test the next url */ svc.thinkTime(1000 * 3, function () { testUrlItem(svc, urlList[idx], onCallback); }); } else { done(); } } /* test the first url */ svc.thinkTime(1000 * 3, function () { testUrlItem(svc, urlList[idx], onCallback); }); }); };