Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vegeta Load Testing: different body for each POST request in the attack

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?

like image 879
Sakib Avatar asked Apr 17 '17 00:04

Sakib


People also ask

What is payload in performance testing?

The header identifies the source and destination of the packet, and is only used in the transmission process, while the actual data is referred to as the payload. Payload testing ensures that users receive data correctly on their next-generation networks.

What is Vegeta tool?

Vegeta is an HTTP load testing tool written in Go that can be used as a command in a command-line interface or as a library. The program tests how an HTTP-based application behaves when multiple users access it at the same time by generating a background load of GET requests.


2 Answers

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)
like image 173
rajeshnair Avatar answered Oct 13 '22 00:10

rajeshnair


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
like image 28
ynkr Avatar answered Oct 12 '22 23:10

ynkr