Skip to content

Commit

Permalink
fix(apdex): run apdex logic correctly in workers (#2195)
Browse files Browse the repository at this point in the history
Solves #2193
  • Loading branch information
bernardobridge authored Oct 10, 2023
1 parent 5516ff0 commit 9c51118
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
52 changes: 30 additions & 22 deletions packages/artillery-plugin-apdex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/artillery-plugin-apdex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
10 changes: 10 additions & 0 deletions packages/artillery-plugin-apdex/test/fixtures/processor.js
Original file line number Diff line number Diff line change
@@ -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
};
14 changes: 14 additions & 0 deletions packages/artillery-plugin-apdex/test/fixtures/scenario.yml
Original file line number Diff line number Diff line change
@@ -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: "/"
36 changes: 36 additions & 0 deletions packages/artillery-plugin-apdex/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -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'
);
});

0 comments on commit 9c51118

Please sign in to comment.