8

Is there a way to change the json body in vegeta Post request load tests in vegeta.

I want to send a request with a different parameter in the json body for each of the requests. for example if I have

POST https://endpoint.com/createNew
@/targets/data.json

and data.json looks like

{
   "id": 1234
}

What is the best way to make it so we have different request data for each of the requests in the attack?

Inv3r53
  • 2,929
  • 3
  • 25
  • 37
Sakib
  • 1,503
  • 4
  • 26
  • 39

3 Answers3

12

I needed to do something similiar and decided to use the vegeta lib rather than cli for this which allows me to control the HTTP requests

So you need to write your own function which return a vegeta.Targeter

func NewCustomTargeter() vegeta.Targeter {
    return func(tgt *vegeta.Target) error {
        if tgt == nil {
            return vegeta.ErrNilTarget
        }

        tgt.Method = "POST"

        tgt.URL = "https://endpoint.com/createNew"

        rand := generateFourDigitRandom()

        payload := '{ "id":"`+rand+ `" } `
        tgt.Body = []byte(payload)
        return nil
    }
}

and use this function in the main function like this

    targeter := NewCustomTargeter()
    attacker := vegeta.NewAttacker()
    var metrics vegeta.Metrics
    for res := range attacker.Attack(targeter, rate, duration, "Load Test") {
        metrics.Add(res)
    }
    metrics.Close()
    fmt.Printf("%+v  \n", metrics)
rajeshnair
  • 1,587
  • 16
  • 32
2

On Jul 10, 2018, vegeta#PR300 introduced the -format=json option. Here is the vegeta README description:

The JSON format makes integration with programs that produce targets dynamically easier. Each target is one JSON object in its own line. The method and url fields are required. If present, the body field must be base64 encoded. The generated JSON Schema defines the format in detail.

And their provided example:

jq -ncM '{method: "GET", url: "http://goku", body: "Punch!" | @base64, header: {"Content-Type": ["text/plain"]}}' |
  vegeta attack -format=json -rate=100 | vegeta encode
ynkr
  • 25,946
  • 4
  • 32
  • 30
-7

If you have multiple files under targets folder and would like to use them all in your load test I would suggest the following configuration:

  1. Install Directory Listing Config plugin using JMeter Plugins Manager

    JMeter Directory Listing Config Installation

  2. Configure it to point to your targets folder:

    Directory Listing Config - configuration

  3. In your HTTP Request Sampler use __FileToString() function like:

    ${__FileToString(${filename},,)}
    

    Using file in request body

  4. When you run your test it will pick up the next file from targets directory and use its contents as the request body

    JMeter Directory Listing Config in Action

Dmitri T
  • 159,985
  • 5
  • 83
  • 133