Created
November 17, 2016 21:59
-
-
Save hassy/a330223fb4b3d2c2f7ee24e1274cefdd to your computer and use it in GitHub Desktop.
Custom Artillery functions to print the latency distribution for a single HTTP request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Custom Artillery functions to print the latency distribution for a single HTTP request | |
// | |
// Example usage: (in your scenario) | |
// - get: | |
// url: "/some/endpoint" | |
// beforeRequest: ["recordStartTime"] | |
// afterResponse: ["logLatencyDelta"] | |
// name: "Optional name for the endpoint for the report" | |
module.exports = { | |
recordStartTime: recordStartTime, | |
logLatencyDelta: logLatencyDelta | |
}; | |
/** | |
* Pre-request hook to set requestLatency variable to current time | |
*/ | |
function recordStartTime(req, ctx, events, done) { | |
ctx.vars['requestLatency'] = Date.now(); | |
return done(); | |
} | |
/** | |
* Post-response hook to calculate and log how long the request took | |
*/ | |
function logLatencyDelta(req, res, ctx, events, done) { | |
if (!ctx.vars.requestLatency) { | |
return done(); | |
} | |
let delta = Date.now() - ctx.vars['requestLatency']; | |
events.emit('customStat', { stat: req.name + ' latency', value: delta }); | |
return done(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment