user1950349
user1950349

Reputation: 5146

How to grep response to figure out how many calls got timed out?

I have a curl command which is calling one of our service so if my service timesout, it returns JSON response like below:

[{"results":{"response":null},"error":{"errorCode":1001,"message":"Service Timeout","status":"FAILURE"}}]

Below is my curl command when I run, I will get above response if there is any timeout

curl --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120";

I am running my above curl command in a for loop for x number of times. Now I want to see how many calls got timed out by checking "message" in the JSON response? I mean if I made 1 million calls then how many calls got timedout and what was the percentage of timeout?

So I got below one line for loop which calls curl command but whenever I run it always gives 1 as an answer which is wrong. I mean I can see lot of calls getting timedout but every time it gives 1 as an answer. Is there anything wrong I am doing?

for ((i=1;i<=1000000;i++)); do
curl --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"
done \
| grep -wcoE '"errorCode":1001'

This is what I see as an output after running the command:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   226  100   226    0     0  12798      0 --:--:-- --:--:-- --:--:-- 17384
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   226  100   226    0     0   4591      0 --:--:-- --:--:-- --:--:--  7290
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   226  100   226    0     0   6318      0 --:--:-- --:--:-- --:--:--  8370
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   226  100   226    0     0   5252      0 --:--:-- --:--:-- --:--:--  7793
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   226  100   226    0     0   6139      0 --:--:-- --:--:-- --:--:--  8071
1

Upvotes: 0

Views: 95

Answers (1)

unconditional
unconditional

Reputation: 7666

Just make curl silent with the -s flag:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"

Or redirect its error output to /dev/null like so:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" 2>/dev/null

Also, your version of counting won't work as expected. Here's how you could fix it:

timedout_count=0
for ((i=1;i<=1000000;i++)); do
    curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" | 
        grep -q '"errorCode":1001' && timedout_count=$(( timedout_count + 1 ))
done 

echo "Timed out $timedout_count times" 

Upvotes: 2

Related Questions