diff --git a/packages/artillery-plugin-apdex/index.js b/packages/artillery-plugin-apdex/index.js index c6688c9016..cdc94df780 100644 --- a/packages/artillery-plugin-apdex/index.js +++ b/packages/artillery-plugin-apdex/index.js @@ -15,34 +15,42 @@ class ApdexPlugin { constructor(script, _events) { this.script = script; - const t = - script.config.apdex?.threshold || - script.config.plugins.apdex?.threshold || - 500; + if ( + global.artillery && + Number(global.artillery.version.slice(0, 1)) > 1 && + typeof process.env.LOCAL_WORKER_ID !== 'undefined' + ) { + const t = + script.config.apdex?.threshold || + script.config.plugins.apdex?.threshold || + 500; - if (!script.config.processor) { - script.config.processor = {}; - } + if (!script.config.processor) { + script.config.processor = {}; + } - script.scenarios.forEach(function (scenario) { - scenario.afterResponse = [].concat(scenario.afterResponse || []); - scenario.afterResponse.push('apdexAfterResponse'); - }); + script.scenarios.forEach(function (scenario) { + scenario.afterResponse = [].concat(scenario.afterResponse || []); + scenario.afterResponse.push('apdexAfterResponse'); + }); - function apdexAfterResponse(req, res, userContext, events, done) { - const total = res.timings.phases.total; - if (total <= t) { - events.emit('counter', METRICS.satisfied, 1); - } else if (total <= 4 * t) { - events.emit('counter', METRICS.tolerated, 1); - } else { - events.emit('counter', METRICS.frustrated, 1); + function apdexAfterResponse(req, res, userContext, events, done) { + const total = res.timings.phases.total; + if (total <= t) { + events.emit('counter', METRICS.satisfied, 1); + } else if (total <= 4 * t) { + events.emit('counter', METRICS.tolerated, 1); + } else { + events.emit('counter', METRICS.frustrated, 1); + } + + return done(); } - return done(); - } + script.config.processor.apdexAfterResponse = apdexAfterResponse; - script.config.processor.apdexAfterResponse = apdexAfterResponse; + return; + } global.artillery.ext({ ext: 'beforeExit', diff --git a/packages/artillery-plugin-apdex/package.json b/packages/artillery-plugin-apdex/package.json index fadc180e29..8e76818253 100644 --- a/packages/artillery-plugin-apdex/package.json +++ b/packages/artillery-plugin-apdex/package.json @@ -4,7 +4,7 @@ "description": "Calculate and report Apdex scores", "main": "index.js", "scripts": { - "test": "exit 0" + "test": "export ARTILLERY_TELEMETRY_DEFAULTS='{\"source\":\"test-suite\"}' && tap ./test/*.spec.js --timeout 300 --no-coverage --color" }, "keywords": [], "author": "", diff --git a/packages/artillery-plugin-apdex/test/fixtures/processor.js b/packages/artillery-plugin-apdex/test/fixtures/processor.js new file mode 100644 index 0000000000..4832c8aef5 --- /dev/null +++ b/packages/artillery-plugin-apdex/test/fixtures/processor.js @@ -0,0 +1,10 @@ +function myAfterResponseHandler(req, res, context, ee, next) { + //Change your function name and add your logic here. + //For more information, check: https://docs.art/http-reference#function-signatures + console.log('After Response Handler still working'); + next(); +} + +module.exports = { + myAfterResponseHandler +}; diff --git a/packages/artillery-plugin-apdex/test/fixtures/scenario.yml b/packages/artillery-plugin-apdex/test/fixtures/scenario.yml new file mode 100644 index 0000000000..c85cb18ad0 --- /dev/null +++ b/packages/artillery-plugin-apdex/test/fixtures/scenario.yml @@ -0,0 +1,14 @@ +config: + target: "http://asciiart.artillery.io:8080" + processor: "./processor.js" + phases: + - duration: 5 + arrivalRate: 1 + name: "Phase 1" + +scenarios: + - name: apdexPluginTest + flow: + - get: + afterResponse: myAfterResponseHandler + url: "/" \ No newline at end of file diff --git a/packages/artillery-plugin-apdex/test/index.spec.js b/packages/artillery-plugin-apdex/test/index.spec.js new file mode 100644 index 0000000000..6398d2708b --- /dev/null +++ b/packages/artillery-plugin-apdex/test/index.spec.js @@ -0,0 +1,36 @@ +const { test, afterEach } = require('tap'); +const { $ } = require('zx'); + +test('apdex plugin works when other after response hooks are set', async (t) => { + //Arrange: Plugin overrides + const override = JSON.stringify({ + config: { + plugins: { apdex: {} }, + apdex: { + threshold: 100 + } + } + }); + + //Act: run the test + const output = + await $`../artillery/bin/run run ./test/fixtures/scenario.yml --overrides ${override}`; + + const apdexRegex = + /Apdex score: (\d(?:\.\d+)?) \((unacceptable|poor|fair|good|excellent)\)/; + + const apdexTest = apdexRegex.test(output.stdout); + const afterResponseOccurrences = ( + output.stdout.match( + new RegExp(/After Response Handler still working/, 'g') + ) || [] + ).length; + + // Assert + t.ok(apdexTest, 'Console did not include Apdex score'); + t.equal( + afterResponseOccurrences, + 5, + 'After Response Handler did not run five times' + ); +});