我有一个调用我们服务的 curl 命令,所以如果我的服务超时,它会返回如下 JSON 响应:
[{"results":{"response":null},"error":{"errorCode":1001,"message":"Service Timeout","status":"FAILURE"}}]
下面是我运行时的curl命令,如果超时我会得到上面的响应
curl --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120";
我在 for 循环中运行上面的 curl 命令 x 次。现在我想通过检查 JSON 响应中的 "message"
来查看有多少调用超时?我的意思是,如果我进行了 100 万次调用,那么有多少次调用超时,超时的百分比是多少?
所以我得到了下面一行调用 curl 命令的循环,但每当我运行它时,它总是给出 1 作为错误的答案。我的意思是我可以看到很多调用超时,但每次它都会给出 1 作为答案。我做错了什么吗?
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'
这是我在运行命令后看到的输出:
% 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
最佳答案
只需使用 -s
标志使 curl 静音:
curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"
或者像这样将它的错误输出重定向到/dev/null
:
curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" 2>/dev/null
此外,您的计数版本不会按预期工作。修复方法如下:
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"
关于linux - 如何通过 grep 响应找出有多少调用超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36095766/