I am building a REST service, i am expecting a lot of requests (even DoS attacks). So thinking about network and cpu consumption which HTTP method would be better for my server (accepting POST or GET)?
I have made some tests... Using a VM Ubuntu 14.04 (1 core) as server, with apache and 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;
Test #1 (using 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
I run the AB test several times, with same results: POST is faster but GET is lighter. So i thought in a second test (something more real).
Test #2 (using 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 with the wget test.... GET is faster:
...
TIME POST: 54.707362313
TIME GET: 53.049255400
Which HTTP method has better performance?
Should i care about it?
Should i expect different results with nginx or nodejs (expess)?
abandwgetmore than anything else here; POST and GET should not have any inherent, significant differences in speed. – Ed Cottrell♦ Nov 14 '14 at 5:18