Thursday, April 7, 2022

[SOLVED] curl code : stop send http request when it reach specific amount?

Issue

target=${1:-http://web1.com}
while true # loop forever, until ctrl+c pressed.
do
    for i in $(seq 10) # perfrom the inner command 10 times.
    do
    curl $target > /dev/null & # send out a curl request, the & indicates not to wait for the response.
    done


    wait # after 100 requests are sent out, wait for their processes to finish before the next iteration.

done

I want to train my HTTP load balancing by giving multiple HTTP requests in one time. I found this code to help me to send 10 sequence HTTP requests to web1.com at one time. However, I want the code to stop when reaches 15000 requests.

so in total, there will be 1500 times to send HTTP requests.

thank you for helping me

update

I decide to use Hey. https://serverfault.com/a/1082007/861352

i use this command to run the request hey -n 100 -q 2 -c 1 http://web1.com -A

which:

-n: stop the request when it reaches 100 request
-q: give a break for 2 (not request /second but query /second) to avoid flooding requests at once
-c: send the request at once 1 request
http://web1.com = my web
-A = accept header, I have an error when removing -A

Solution

this works:

target=${1:-http://web1.com}
limit=1500

count=0
while true # loop forever, until ctrl+c pressed.
do
    for i in $(seq 10) # perfrom the inner command 10 times.
    do
    count=$(expr $count + 1) # increments +1
    curl -sk $target > /dev/null & # send out a curl request, the & indicates not to wait for the response
    done


    wait # after 100 requests are sent out, wait for their processes to finish before the next iteration.

if [ $count -ge $limit ];then
exit 0
fi
done


Answered By - Ron
Answer Checked By - Terry (WPSolving Volunteer)