Last active
February 21, 2024 18:59
-
-
Save whitehorse0/80ef3574fa8c971eaacf38bd3961cbfb to your computer and use it in GitHub Desktop.
Artillery configuration example Load testing, and allow writing custom logic JS functions to be called at certain points during the execution of a scenario.
This file contains hidden or 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
config: | |
target: "http://localhost:8000" | |
http: | |
timeout: 10 # Responses have to be sent within 10 seconds or the request will be aborted | |
processor: "./processor.js" | |
phases: | |
# Create 100 virtual users every second for 60 seconds | |
- duration: 60 # seconds | |
arrivalRate: 100 # virtual users | |
name: "Load test - user login - arrival rate" | |
payload: | |
# path is relative to the location of the test script | |
path: "credential.csv" | |
fields: | |
- "username" | |
- "password" | |
order: "sequence" | |
plugins: | |
expect: {} | |
scenarios: | |
- name: 'Load test user login & Logout' | |
flow: | |
- post: | |
url: "/login" | |
json: | |
username: "{{ username }}" | |
password: "{{ password }}" | |
beforeRequest: parsePropertyToString | |
capture: | |
- json: "$.token" | |
as: token | |
expect: | |
- statusCode: 200 | |
- post: | |
url: "/logout" | |
headers: | |
Authorization: "Bearer {{ token }}" | |
expect: | |
- statusCode: 200 |
This file contains hidden or 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
username | password | |
---|---|---|
admin | admin123 |
This file contains hidden or 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
const { spawn } = require('child_process') | |
const chalk = require('chalk') | |
const path = require('path') | |
const glob = require('glob-fs')({ gitignore: true }) | |
class ArtilleryTest { | |
constructor () { | |
this.isWin = process.platform === 'win32' | |
this.path = path.join(__dirname, './../../') | |
} | |
run () { | |
this.runDir(this.path) | |
} | |
async runDir (dirPath) { | |
const files = glob.readdirSync(`**/*.test.yml`) | |
files.forEach(file => { | |
let testDir = path.join(dirPath, file) | |
this.startLoadTest(testDir) | |
}) | |
} | |
startLoadTest (file) { | |
if (this.isWin) { | |
this.startWindowTest(file) | |
} else { | |
this.startOtherTest(file) | |
} | |
} | |
getArtillery () { | |
return './node_modules/.bin/artillery' | |
} | |
startWindowTest (file) { | |
let cp = spawn(process.env.comspec, ['/c', 'artillery run -e production ' + file]) | |
this.assingArtillaryOptions(cp) | |
} | |
startOtherTest (file) { | |
let cp = spawn(this.getArtillery(), ['run', '-e', 'production', '-o', './tests/artillery/report.json', file]) | |
this.assingArtillaryOptions(cp) | |
} | |
assingArtillaryOptions (cp) { | |
cp.stdout.on('data', function (data) { | |
console.log(chalk.white.bold(data.toString())) // logging on test process | |
}) | |
cp.stderr.on('data', function (data) { | |
console.log(chalk.redBright.bold(data.toString())) // logging on test fails process | |
}) | |
cp.on('exit', function (code) { | |
console.log(chalk.gray.bold('child process exited with code ' + code.toString())) | |
}) | |
} | |
} | |
const test = new ArtilleryTest() | |
test.run() |
This file contains hidden or 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
// The JS file is expected to be a standard Node.js module: | |
module.exports = { | |
beforeRequestHandler (req, ctx, ee, next) { | |
return next() // Must be called for the scenario to continue | |
}, | |
afterResponseHandler (req, res, ctx, ee, next) { | |
// console.log(`response body : ${res.body}`) | |
return next() // Must be called for the scenario to continue | |
}, | |
query (ctx, events, done) { | |
// ctx.vars['query'] = 'foo' // set the "query" for the virtual user request | |
return done() | |
}, | |
parsePropertyToString (req, ctx, ee, next) { | |
req.json.username = (ctx.vars.username).toString() | |
req.json.password = (ctx.vars.password).toString() | |
return next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment