问题
I am to load test a scenario where user adds a contact in his addressbook. To do this user must first Log-In to his account.
I have a Nodejs script 'AutoLogin.js' that performs Log-In for the user and a json file 'contact.json' which has necessary configuration and POST request parameters to add contact in addressbook.
Artillery runs 'contact.json' file.
{
"config": {
"target": "target url",
"https": {
"tls": {
"rejectunauthorized": false
}
},
"phases": [
{
"duration": 10,
"arrivalRate": 2
}
]
},
"scenarios": [
{
"flow": [
{
"post": {
"url": "/addContact",
"contactInfo": {
"Name": "Davion",
"Mobile": "9289543654",
"Email": "Davion@gmail.com"
}
}
}
]
}
]
}
Given code sends 2 Post request/sec i.e simulating 20 users adding a contact to addressbook over a duration of 10 seconds.
I need to perform Log-In before this code runs as adding a contact without Log-In is not possible.
Plus I don't want Log-In process to be included in load test. Is there a way I Can run my 'AutoLogin.js' script within 'contact.json' file without including it in load test and then running 'contact.json' using Artillery?
回答1:
If you need to login before every single request:
- In the
config
section of the YAML file, add aprocessor
line with your custom JS specified. Note, your JS is expected to be a standard Node.js module:
"config": {
"processor": "AutoLogin.js"
}
- In the
post
action of your flow, add a line for thebeforeRequest
hook:
"post": {
"url": "/addContact",
"beforeRequest": "functionFromAutoLogin",
"contactInfo": {
"Name": "Davion",
"Mobile": "9289543654",
"Email": "Davion@gmail.com"
}
}
From: https://artillery.io/docs/http-reference/#advanced-writing-custom-logic-in-javascript
See also https://artillery.io/docs/script-reference/#payload-files for using a CSV for the test data.
来源:https://stackoverflow.com/questions/42196637/how-can-i-run-a-node-js-script-in-artillery-i-o-without-including-it-in-load-tes