Skip to content

Commit

Permalink
feat(artillery): add ability to specify scenario to run by name
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed Oct 3, 2023
1 parent 9215c2f commit 406fc49
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/artillery/lib/cmds/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ RunCommand.flags = {
}),
key: Flags.string({
description: 'API key for Artillery Cloud'
}),
name: Flags.string({
description: 'Name of the test to run',
char: 'n'
})
};

Expand Down Expand Up @@ -207,7 +211,8 @@ RunCommand.runCommandImplementation = async function (flags, argv, args) {
scriptPath: args.script,
// TODO: This should be an array of files, like inputFiles above
absoluteScriptPath: path.resolve(process.cwd(), args.script),
plugins: []
plugins: [],
scenarioName: flags.name
};

// Set "name" tag if not set explicitly
Expand Down
26 changes: 26 additions & 0 deletions packages/artillery/test/cli/command-run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ tap.test('Environment specified with -e should be used', async (t) => {
t.ok(exitCode === 0 && !output.stdout.includes('ECONNREFUSED'));
});

tap.test('Can specify scenario to run by name', async (t) => {
const reportFile = 'report-with-scenario-by-name.json';
const reportFilePath = await getRootPath(reportFile);

const [exitCode, output] = await execute([
'run',
'-n',
'Test Scenario 2',
'-o',
`${reportFilePath}`,
'test/scripts/scenario-named/scenario.yml'
]);
console.log(output.stdout);

// Here if the right environment is not picked up, we'll get ECONNREFUSED errors in the report
t.ok(
exitCode === 0 && output.stdout.includes('Successfully running scenario 2')
);
const json = JSON.parse(fs.readFileSync(reportFilePath, 'utf8'));

t.ok(
deleteFile(reportFilePath) &&
json.aggregate.counters['vusers.created_by_name.Test Scenario 2'] === 6
);
});

tap.test('Run a script with one payload command line', async (t) => {
const [, output] = await execute([
'run',
Expand Down
13 changes: 13 additions & 0 deletions packages/artillery/test/scripts/scenario-named/scenario.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
config:
target: http://asciiart.artillery.io:8080
phases:
- duration: 3
arrivalRate: 2

scenarios:
- name: Test Scenario 1
flow:
- log: "Successfully running scenario 1"
- name: Test Scenario 2
flow:
- log: "Successfully running scenario 2"
33 changes: 30 additions & 3 deletions packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ function run(script, ee, options, runState, contextVars) {
if (runState.pendingScenarios >= spec.maxVusers) {
metrics.counter('vusers.skipped', 1);
} else {
scenarioContext = runScenario(script, metrics, runState, contextVars);
scenarioContext = runScenario(
script,
metrics,
runState,
contextVars,
options
);
}
});
phaser.on('phaseStarted', function (spec) {
Expand Down Expand Up @@ -242,7 +248,7 @@ function run(script, ee, options, runState, contextVars) {
phaser.run();
}

function runScenario(script, metrics, runState, contextVars) {
function runScenario(script, metrics, runState, contextVars, options) {
const start = process.hrtime();

//
Expand All @@ -263,7 +269,7 @@ function runScenario(script, metrics, runState, contextVars) {
const w = engineUtil.template(scenario.weight, {
vars: variableValues
});
scenario.weight = isNaN(parseInt(w)) ? 0 : parseInt(w);
scenario.weight = isNaN(parseInt(w)) ? 0 : parseInt(w); //eslint-disable-line radix
debug(
`scenario ${scenario.name} weight has been set to ${scenario.weight}`
);
Expand Down Expand Up @@ -307,8 +313,29 @@ function runScenario(script, metrics, runState, contextVars) {
);
}

//default to randomly picked scenario
let i = runState.picker()[0];

if (options.scenarioName) {
let foundIndex;
const foundScenario = script.scenarios.filter((scenario, index) => {
foundIndex = index;
return new RegExp(options.scenarioName).test(scenario.name);
});

if (!foundScenario) {
debug(
`scenario ${options.scenarioName} not found in script. Choosing random scenario instead.`
);
} else if (foundScenario.length > 1) {
debug(
`multiple scenarios found for ${options.scenarioName}. Choosing random scenario instead.`
);
} else {
debug(`scenario ${options.scenarioName} found in script. running it!`);
i = foundIndex;
}
}
debug(
'picking scenario %s (%s) weight = %s',
i,
Expand Down

0 comments on commit 406fc49

Please sign in to comment.