Score:1

Curl Command Send Asynchronous Request

sx flag

I using this curl request for sending data to API :

curl --location --request POST 'MY_IP' \
--header 'Content-Type: text/plain' \
--data-raw ' [{
    "event_id": "123",
}]    
'

I want to check the API performance by sending request asynchronously and measure the time take I tried to use xargs -P 10 which say send 10 request parallel but not able to do so ? Any help and how I can measure time ?

My Code :

#!/bin/bash

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

seq 1 2 | xargs -n1 -P10 curl --location --request POST '10.33.137.98:8080' --header 'Content-Type: text/plain' --data-raw "$Body"

But how to calculate time like P95 , P90 etc ?

Score:1
cn flag

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.

Score:0
ar flag

Make a bash function. Call it from GNU Parallel. Use --joblog to see timing.

doit() {
    curl --location --request POST 'MY_IP' \
    --header 'Content-Type: text/plain' \
    --data-raw ' [{
        "event_id": "123",
    }]    
    '
}
export -f doit
seq 10000 | parallel -j 10 --joblog my.log doit

Now look at my.log.

GNU Parallel has an overhead of 1-10 ms per job, so if your curls are really short, you could do more than one:

seq 10000 | parallel -j 10 --joblog my.log 'doit;doit;doit;doit;doit;doit;doit;doit;doit;doit'
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.