Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leakage + upgrade packages with vulnerabilities + support for running requests one time before start of test #482

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ lib-cov
artillery_report*
coverage/*
.vagrant/
.vscode
.vscode
.history
281 changes: 159 additions & 122 deletions core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const Engines = {
socketio: {}
};

let contextVars;

JSCK.Draft4 = JSCK.draft4;

const schema = new JSCK.Draft4(require('./schemas/artillery_test_script.json'));
Expand Down Expand Up @@ -124,132 +126,136 @@ function runner(script, payload, options, callback) {
}
);

//
// load plugins:
//
let runnerPlugins = [];
let requirePaths = [];

let pro = null;
if (tryResolve('artillery-pro')) {
pro = require('artillery-pro');
requirePaths = requirePaths.concat(pro.getPluginPath());
} else {
debug('Artillery Pro is not installed.');
}
return handleBeforeRequests(script, runnableScript, runnerEngines)
.then(function(){
//
// load plugins:
//
let runnerPlugins = [];
let requirePaths = [];

let pro = null;
if (tryResolve('artillery-pro')) {
pro = require('artillery-pro');
requirePaths = requirePaths.concat(pro.getPluginPath());
} else {
debug('Artillery Pro is not installed.');
}

requirePaths.push('');
requirePaths.push('');

if (process.env.ARTILLERY_PLUGIN_PATH) {
requirePaths = requirePaths.concat(process.env.ARTILLERY_PLUGIN_PATH.split(':'));
}

debug('require paths: ', requirePaths);
if (process.env.ARTILLERY_PLUGIN_PATH) {
requirePaths = requirePaths.concat(process.env.ARTILLERY_PLUGIN_PATH.split(':'));
}

runnableScript.config.plugins = runnableScript.config.plugins || {};
debug('require paths: ', requirePaths);

if (process.env.ARTILLERY_PLUGINS) {
let additionalPlugins = {};
try {
additionalPlugins = JSON.parse(process.env.ARTILLERY_PLUGINS);
} catch (ignoreErr) {
debug(ignoreErr);
}
runnableScript.config.plugins = Object.assign(
runnableScript.config.plugins,
additionalPlugins);
}
runnableScript.config.plugins = runnableScript.config.plugins || {};

_.each(runnableScript.config.plugins, function tryToLoadPlugin(pluginConfig, pluginName) {
let pluginConfigScope = pluginConfig.scope || runnableScript.config.pluginsScope;
let pluginPrefix = pluginConfigScope ? pluginConfigScope : 'artillery-plugin-';
let requireString = pluginPrefix + pluginName;
let Plugin, plugin;

requirePaths.forEach(function(rp) {
try {
Plugin = require(path.join(rp, requireString));
if (typeof Plugin === 'function') {
// Plugin interface v1
plugin = new Plugin(runnableScript.config, ee);
plugin.__name = pluginName;
} else if (typeof Plugin === 'object' && typeof Plugin.Plugin === 'function') {
// Plugin interface 2+
plugin = new Plugin.Plugin(runnableScript, ee, options);
plugin.__name = pluginName;
if (process.env.ARTILLERY_PLUGINS) {
let additionalPlugins = {};
try {
additionalPlugins = JSON.parse(process.env.ARTILLERY_PLUGINS);
} catch (ignoreErr) {
debug(ignoreErr);
}
runnableScript.config.plugins = Object.assign(
runnableScript.config.plugins,
additionalPlugins);
}
} catch (err) {
debug(err);
}
});

if (!Plugin || !plugin) {
console.log(
'WARNING: plugin %s specified but module %s could not be loaded',
pluginName,
requireString);
warnings.plugins[pluginName] = {
message: 'Could not load'
};
} else {
debug('Plugin %s loaded from %s', pluginName, requireString);
runnerPlugins.push(plugin);
}
});

const promise = new Promise(function(resolve, reject) {
ee.run = function() {
let runState = {
pendingScenarios: 0,
pendingRequests: 0,
compiledScenarios: null,
scenarioEvents: null,
picker: undefined,
plugins: runnerPlugins,
engines: runnerEngines
};
debug('run() with: %j', runnableScript);
run(runnableScript, ee, opts, runState);
};

ee.stop = function (done) {
// allow plugins to cleanup
A.eachSeries(
runnerPlugins,
function(plugin, next) {
if (plugin.cleanup) {
plugin.cleanup(function(err) {
if (err) {
_.each(runnableScript.config.plugins, function tryToLoadPlugin(pluginConfig, pluginName) {
let pluginConfigScope = pluginConfig.scope || runnableScript.config.pluginsScope;
let pluginPrefix = pluginConfigScope ? pluginConfigScope : 'artillery-plugin-';
let requireString = pluginPrefix + pluginName;
let Plugin, plugin;

requirePaths.forEach(function(rp) {
try {
Plugin = require(path.join(rp, requireString));
if (typeof Plugin === 'function') {
// Plugin interface v1
plugin = new Plugin(runnableScript.config, ee);
plugin.__name = pluginName;
} else if (typeof Plugin === 'object' && typeof Plugin.Plugin === 'function') {
// Plugin interface 2+
plugin = new Plugin.Plugin(runnableScript, ee, options);
plugin.__name = pluginName;
}
} catch (err) {
debug(err);
}
return next();
}
});
} else {
return next();
}
},
function(err) {
return done(err);
});
};

// FIXME: Warnings should be returned from this function instead along with
// the event emitter. That will be a breaking change.
ee.warnings = warnings;
if (!Plugin || !plugin) {
console.log(
'WARNING: plugin %s specified but module %s could not be loaded',
pluginName,
requireString);
warnings.plugins[pluginName] = {
message: 'Could not load'
};
} else {
debug('Plugin %s loaded from %s', pluginName, requireString);
runnerPlugins.push(plugin);
}
});

resolve(ee);
});
const promise = new Promise(function(resolve, reject) {
ee.run = function() {
let runState = {
pendingScenarios: 0,
pendingRequests: 0,
compiledScenarios: null,
scenarioEvents: null,
picker: undefined,
plugins: runnerPlugins,
engines: runnerEngines
};
debug('run() with: %j', runnableScript);
run(runnableScript, ee, opts, runState);
};

ee.stop = function (done) {
// allow plugins to cleanup
A.eachSeries(
runnerPlugins,
function(plugin, next) {
if (plugin.cleanup) {
plugin.cleanup(function(err) {
if (err) {
debug(err);
}
return next();
});
} else {
return next();
}
},
function(err) {
return done(err);
});
};

// FIXME: Warnings should be returned from this function instead along with
// the event emitter. That will be a breaking change.
ee.warnings = warnings;

resolve(ee);
});

if (callback && typeof callback === 'function') {
promise.then(callback.bind(null, null), callback);
}
if (callback && typeof callback === 'function') {
promise.then(callback.bind(null, null), callback);
}

return promise;
return promise;
});
}

function run(script, ee, options, runState) {
let intermediate = Stats.create();
let aggregate = [];
let intermediate = Stats.create();
let aggregate = [];

let phaser = createPhaser(script.config.phases);
phaser.on('arrival', function() {
Expand Down Expand Up @@ -392,19 +398,28 @@ function runScenario(script, intermediate, runState) {
* Create initial context for a scenario.
*/
function createContext(script) {
const INITIAL_CONTEXT = {
vars: {
target: script.config.target,
$environment: script._environment,
$processEnvironment: process.env
},
funcs: {
$randomNumber: $randomNumber,
$randomString: $randomString,
$template: input => engineUtil.template(input, { vars: result.vars })
}
};
let result = _.cloneDeep(INITIAL_CONTEXT);
let initialContext = {};
if (contextVars) {
initialContext = {
vars: contextVars
};
} else {
initialContext = {
vars: {
target: script.config.target,
$environment: script._environment,
$processEnvironment: process.env
}
};
}
initialContext.funcs = {
$randomNumber: $randomNumber,
$randomString: $randomString,
$template: input => engineUtil.template(input, { vars: result.vars })

};

let result = _.cloneDeep(initialContext);

//
// variables from payloads
Expand Down Expand Up @@ -450,3 +465,25 @@ function $randomNumber(min, max) {
function $randomString(length) {
return Math.random().toString(36).substr(2, length);
}

function handleBeforeRequests(script, runnableScript, runnerEngines) {
return new Promise(function(resolve, reject){
if (script.before) {
let name = runnableScript.before.engine || 'http';
let engine = runnerEngines.find((e) => e.__name === name);
let beforeTestScenario = engine.createScenario(runnableScript.before);
let beforeTestContext = createContext(script);
beforeTestScenario(beforeTestContext, function(err, context) {
if (err) {
debug(err);
return reject(err);
} else {
contextVars = context.vars;
return resolve();
}
});
} else {
return resolve();
}
});
}
12 changes: 12 additions & 0 deletions core/lib/schemas/artillery_test_script.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
}
}
},
"before": {
"type": "object",
"properties": {
"flow": {
"type": "array",
"items": {
"type": "object"
}
}
},
"required": ["flow"]
},
"scenarios": {
"type": "array"
}
Expand Down
7 changes: 1 addition & 6 deletions core/lib/stats2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ function create() {
function combine(statsObjects) {
let result = create();
L.each(statsObjects, function(stats) {
L.each(stats._entries, function(entry) {
result._entries.push(entry);
});
L.each(stats._latencies, function(latency) {
result._latencies.push(latency);
});
Expand Down Expand Up @@ -169,9 +166,7 @@ Stats.prototype.report = function() {
result.scenariosCompleted = this._completedScenarios;
result.requestsCompleted = this._completedRequests;

let latencies = L.map(this._entries, (e) => {
return e[2];
});
let latencies = this._latencies;

result.latency = {
min: round(L.min(latencies) / 1e6, 1),
Expand Down
Loading