Curl Command Send Asynchronous Request

Solution 1:

I also couldn't get xargs -P working when I needed to do something similar, and I ended up using &.

From man bash:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. These are referred to as asynchronous commands.

So something like this, maybe?

#!/bin/bash

Body='{
    "event_id": "12",
    "metric_name": "API",
    "value" : "1",
    "dimensions": {  
    },
    "timestamp_ms": 1615552313
}';

function run() {
    for i in $(seq 1 10); do
        echo "running task $i"
        curl --location --request POST '10.33.137.98:8080' --header 'Content-Type: text/plain' --data-raw "$Body" &
    done 

    wait
}

time run

Explanations:

  • time is used to measure total execution time, see man time
  • wait ensures that all the background jobs finish before continuing, otherwise the time would just measure how long to launch all the requests, not how it took to get a response
  • parallel might also work instead of &

To calculate statistics, you could output the time of each request, using a second time call in the for loop, and then process it using your favourite analysis tools.