我正在構建一個REST服務,我期待着很多請求(甚至是DoS攻擊)。所以想想網絡和CPU的消耗哪個HTTP方法會更好我的服務器(接受POST或GET)?獲取和發佈性能
我已經取得了一些測試...使用VM的Ubuntu 14.04(1個核心)的服務器,與Apache和PHP
get.php:
$s = "G: ";
foreach ($_GET as $key => $val){
$s .= $val . ", ";
}
echo $s;
post.php中:
$s = "P: ";
foreach ($_POST as $key => $val){
$s .= $val . ", ";
}
echo $s;
試驗#1(用ab):
ab -n 10000 'http://10.0.0.112/get.php?key1=val1&key2=val2&key3=val3'
Concurrency Level: 1
Time taken for tests: 9.080 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2080000 bytes
HTML transferred: 210000 bytes
Requests per second: 1101.33 [#/sec] (mean)
Time per request: 0.908 [ms] (mean)
Time per request: 0.908 [ms] (mean, across all concurrent requests)
Transfer rate: 223.71 [Kbytes/sec] received
ab -n 10000 -p post.data -T application/x-www-form-urlencoded 'http://10.0.0.112/post.php'
Concurrency Level: 1
Time taken for tests: 9.526 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2090000 bytes
Total body sent: 1860000
HTML transferred: 220000 bytes
Requests per second: 1049.72 [#/sec] (mean)
Time per request: 0.953 [ms] (mean)
Time per request: 0.953 [ms] (mean, across all concurrent requests)
Transfer rate: 214.25 [Kbytes/sec] received
我多次運行AB測試,結果相同:POST更快,但GET更輕。所以我想在第二個測試(更真實的東西)。
測試#2(使用wget):
TIME_POST=0
TIME_GET=0
X1=100
X2=10
function fpost {
START=$(date +%s.%N)
i=0
while [ $i -lt $X1 ]
do
wget -q -O out.file 'http://10.0.0.112/post.php' --post-data 'key1=val1&key2=val2&key3=val3'
rm -rf out.file
i=$[$i+1]
done
END=$(date +%s.%N)
T=$(echo "$END - $START" | bc)
echo "POST: $T"
TIME_POST=$(echo "$TIME_POST + $T" | bc)
}
function fget {
START=$(date +%s.%N)
i=0
while [ $i -lt $X1 ]
do
wget -q -O out.file 'http://10.0.0.112/get.php?key1=val1&key2=val2&key3=val3'
rm -rf out.file
i=$[$i+1]
done
END=$(date +%s.%N)
T=$(echo "$END - $START" | bc)
echo "GET: $T"
TIME_GET=$(echo "$TIME_GET + $T" | bc)
}
j=0
while [ $j -lt $X2 ]
do
echo "#"$[$j+1]
fpost
fget
echo
j=$[$j+1]
done
echo "TIME POST: $TIME_POST"
echo "TIME GET: $TIME_GET"
MMMMM與wget的測試....得到的是速度更快:
...
TIME POST: 54.707362313
TIME GET: 53.049255400
的HTTP方法具有更好的性能?
我應該關心它嗎?
我應該期望與nginx或nodejs(expess)有不同的結果嗎?
到目前爲止的答案都很好,但你的問題是關於速度。你可能在這裏測試'ab'和'wget'的速度比什麼都多; POST和GET不應該有任何固有的,顯着的速度差異。 –