Replies: 1 comment 2 replies
-
Hi @rizzyDev , It's posting the same file because you are reading the directory at every request, and in your loop you exit after reading the first result. config:
target: "http://yourtarget"
processor: "./functions.js"
phases:
- duration: 1
arrivalRate: 1
before:
flow:
- function: "readDir"
scenarios:
- name: "Sending files"
flow:
- loop:
- think: 1
- post:
url: "/"
beforeRequest: "readFile"
whileTrue: "gotFile" function readDir(context, events, next) {
// get the files list once
context.vars.files = fs.readdirSync("./files");
return next();
}
function readFile(requestParams, context, events, next) {
// get one file and remove it from the list
const fileName = context.vars.files.pop();
requestParams.body = fs.readFileSync(`./files/${fileName}`)
return next();
}
function gotFile(context, next) {
// continue if we still have files to send
return next(context.vars.files.length > 0)
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My custom function is able to read all the files synchronously without any issues and its as simple as below:
Function (requestParams, context, ee, next){
let dir = .\somewhere:
(fs.readdirSync(dir).forEach(file => {
requestParams.body = fs.readFileSync(
${dir}${file}
).return next()
}
When i try to use it beforeRequest: "function" attrib using loops, it always ends up posting the same file over and over. Is there a way i can loop through all the files and send it in the request body, untill the length of dir is reached?
I am able to post a single file without any troubles. However i need to post all the files in the dir sequentially. I noticed whileTrue option but unable to use it for some reason.
Note: I am able achieve this use case to send to my api using request module of node.js, which posts all the files in the dir.
Please your suggestions would be helpful.
Beta Was this translation helpful? Give feedback.
All reactions