artilleryio / artillery

The complete load testing platform. Everything you need for production-grade load tests. Serverless & distributed. Load test with Playwright. Load test HTTP APIs, GraphQL, WebSocket, and more. Use any Node.js module.
https://www.artillery.io
Mozilla Public License 2.0
8.06k stars 512 forks source link

custom function setJSONBody could not be found. #2086

Open artieghosh99 opened 1 year ago

artieghosh99 commented 1 year ago

Hi, thanks in advance for the help.

I'm trying to conduct a load test against a simple web service that processes image files. When I run the test on my local machine I see the following error: "WARNING: custom function setJSONBody could not be found.". It's totally possible that I've done something wrong, but I've tried and failed to find other documentation on this.

My config file:

config:
  target: https://localhost:7178
  tls: {
      'rejectUnauthorized': false
    }
  phases:
    - duration: 20
      arrivalRate: 1
      rampTo: 2
      name: Warm up phase
    - duration: 60
      arrivalRate: 2
      name: Ramp up load
  processor: './send50mbTiff.js'
scenarios:
  - flow:
      - loop:
        - post:
            url: '/test'
            beforeRequest: 'setJSONBody'
            afterRequest: 'logHeaders'
        count: 1

my processor file:

module.exports = {
    setJSONBody: setJSONBody,
    logHeaders: logHeaders,
  }

  const fs = require('fs');

  function setJSONBody(requestParams, context, ee, next) {
    const formData = chooseRandomFile();
    requestParams.formData = Object.assign({}, requestParams.formData, formData);
    return next(); 
  }

  ///<summary>
  // this function chooses a random file from the repository of files between 0 and 50 mb
  // and serves that file to the JSONBody above.
  ///</summary>
  function chooseRandomFile() {

    let rand = Math.floor(Math.random() * 120);
    var files = fs.readdirSync('C:\Projects\TiffMergerUtility\TiffMergerUtility\bin\Release\net6.0\MergedTiffs')
    let randomFile = files[Math.floor(Math.random() * files.length)];
    return randomFile;

  }

  function logHeaders(requestParams, response, context, ee, next) {
    console.log(response.headers);
    return next();
  }
hassy commented 1 year ago

hi @artieghosh99 👋 The custom JS looks good. Can you check to make sure that the file is called send50mbTiff.js and that it's in the same directory as the Artillery YAML script? Is the other function (logHeaders) being called successfully?

artieghosh99 commented 1 year ago

Thanks for the response - the file is named correctly, but logHeaders is also not being called. If i change the passed file name in the script, it throws the right error telling me that file can't be found.

bernardobridge commented 1 year ago

Hey @artieghosh99 👋 , one thing I noticed is you have a typo in your script:

afterRequest is not a valid hook, it should be afterResponse. Can you try changing that and seeing if the logHeaders then gets called?

This is likely not the source of the original issue, but it may help get ahead with the debugging :).