title: Get vs Post Performance
body: <p>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)?</p>
  
  <p>I have made some tests... Using a VM Ubuntu 14.04 (1 core) as server, with apache and php</p>
  
  <p><strong>get.php:</strong></p>
  
  <pre><code>$s = "G: ";
  foreach ( $_GET as $key =&gt; $val){
      $s .= $val . ", ";
  }
  echo $s;
  </code></pre>
  
  <p><strong>post.php:</strong></p>
  
  <pre><code>$s = "P: ";
  foreach ( $_POST as $key =&gt; $val){
      $s .= $val . ", ";
  }
  echo $s;
  </code></pre>
  
  <p>Test #1 (using ab): </p>
  
  <pre><code>ab -n 10000 'http://10.0.0.112/get.php?key1=val1&amp;key2=val2&amp;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
  </code></pre>
  
  <p>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).</p>
  
  <p>Test #2 (using wget):</p>
  
  <pre><code>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&amp;key2=val2&amp;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&amp;key2=val2&amp;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"
  </code></pre>
  
  <p>mmmmm with the wget test.... GET is faster:</p>
  
  <pre><code>...
  TIME POST: 54.707362313
  TIME GET: 53.049255400
  </code></pre>
  
  <p>Which HTTP method has better performance?</p>
  
  <p>Should i care about it?</p>
  
  <p>Should i expect different results with nginx or nodejs (expess)? </p>