title: Very slow execution of php scripts on the server
body: <p>This question is related to my other question, <a href="https://stackoverflow.com/questions/48911665/benchmarking-runtime-of-ajax-requests?noredirect=1#comment84829932_48911665">found here</a>.</p>
  
  <p>At first I tought that this is a networking problem, but it seems more and more likely, that this has something to do with my php configuration, and the running of the php files. I've made the following test cases:</p>
  
  <p>I've created a php file, named it <code>test.php</code>, with the following content:</p>
  
  <pre><code> &lt;?php
   echo 'test';
   ?&gt;
  </code></pre>
  
  <p>and created two bash files, with the following contents:</p>
  
  <pre><code>//bash1.sh
  #!/bin/bash
  /usr/bin/php /testFirstByte/test.php
  </code></pre>
  
  <p>and the other with</p>
  
  <pre><code>//bash2.sh
  #!/bin/bash
  echo Test;
  </code></pre>
  
  <p>Then I proceeded to time their run, running each and preceeding it with the 'time' command, ie: <code>time php test.php</code></p>
  
  <p>The results are as follows:</p>
  
  <pre><code> // time php test.php
   test
   real    0m0.548s
   user    0m0.445s
   sys     0m0.101s
  </code></pre>
  
  <p>&nbsp;</p>
  
  <pre><code> // time sh bash2.hs
   Test
   real    0m0.002s
   user    0m0.002s
   sys     0m0.000s
  </code></pre>
  
  <p>&nbsp;</p>
  
  <pre><code> // time sh bash1.hs
   X-Powered-By: PHP/5.5.30
   Content-type: text/html; charset=utf-8
   test
   real    0m0.539s
   user    0m0.429s
   sys     0m0.108s
  </code></pre>
  
  <p>For me it looks like, that whenever I try to run a PHP script, the runtime is increased with at least half a second, for whichever php script I try to run. I can't figure out how to resolve this issue, so any help would be greatly appreaciated!</p>
  
  <p><strong>EDIT 1:</strong> I've made a simple script, I hope this is what you meant by the inside test @Eineki. The script is as follows:</p>
  
  <pre><code>$timer = microtime(true);
  require('test_simple.php');
  $end = microtime(true) - $timer;
  echo "Require test duration was: " . $end . " seconds\n";
  
  $timer = microtime(true);
  exec('php test_simple.php');
  $end = microtime(true) - $timer;
  echo "Exec test duration was: " . $end . " seconds\n";
  </code></pre>
  
  <p>The results were as follows: </p>
  
  <blockquote>
    <p>Require test duration was: 0.00102400779724 seconds<br>
    Exec test duration was: 0.61318397522 seconds</p>
  </blockquote>
  
  <p><strong>EDIT 2:</strong> the list of loaded extensions are as follows:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Core
      [1] =&gt; date
      [2] =&gt; ereg
      [3] =&gt; libxml
      [4] =&gt; openssl
      [5] =&gt; pcre
      [6] =&gt; sqlite3
      [7] =&gt; zlib
      [8] =&gt; bcmath
      [9] =&gt; bz2
      [10] =&gt; calendar
      [11] =&gt; ctype
      [12] =&gt; curl
      [13] =&gt; dom
      [14] =&gt; hash
      [15] =&gt; fileinfo
      [16] =&gt; filter
      [17] =&gt; ftp
      [18] =&gt; gd
      [19] =&gt; gettext
      [20] =&gt; SPL
      [21] =&gt; iconv
      [22] =&gt; session
      [23] =&gt; json
      [24] =&gt; mbstring
      [25] =&gt; mcrypt
      [26] =&gt; standard
      [27] =&gt; mysql
      [28] =&gt; mysqli
      [29] =&gt; mysqlnd
      [30] =&gt; Phar
      [31] =&gt; posix
      [32] =&gt; Reflection
      [33] =&gt; imap
      [34] =&gt; SimpleXML
      [35] =&gt; sockets
      [36] =&gt; exif
      [37] =&gt; tokenizer
      [38] =&gt; xml
      [39] =&gt; xmlreader
      [40] =&gt; xmlwriter
      [41] =&gt; zip
      [42] =&gt; cgi-fcgi
      [43] =&gt; PDO
      [44] =&gt; pdo_sqlite
      [45] =&gt; pdo_mysql
      [46] =&gt; mailparse
      [47] =&gt; Zend OPcache
  )
  </code></pre>
  
  <p>And this is my php version, as of <code>php -v</code>:</p>
  
  <pre><code>PHP 5.5.30 (cgi-fcgi) (built: Dec  3 2015 06:55:27)
  Copyright (c) 1997-2015 The PHP Group
  Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
      with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend    Technologies
  </code></pre>
  
  <p><strong>EDIT 3:</strong> Run an <code>strace</code> as suggested by @voter on the php file, both on the production server (the server in question), and on our development server, where this problem does not occur. Everything that <code>strace</code> outputs is basically 10 times more on the prod server, then on the dev server. Maybe someone familiar with <code>strace</code> can gather a conclusion from this?</p>
  
  <p><strong>EDIT 4:</strong> </p>
  
  <p>Exceprt from <code>vmstat 1</code> command:</p>
  
  <pre><code>procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
   r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
   5  0 3163644 5410932 2522564 13417292    0    1    22    62    1    0 18  6 75  1  0   
   2  0 3163644 5845884 2522568 13406468    0    0     0   916 31787 5966  9  6 85  0  0  
   8  0 3163644 5439468 2522572 13406840    0    0     8   432 50513 5322 12  6 82  0  0  
   4  0 3163644 5750124 2522572 13407624    0    0     4   232 54417 5615  8  7 86  0  0  
   4  0 3163644 5748608 2522576 13407480    0    0     4   760 118206 5736  7  9 83  0  0 
   3  0 3163644 5742648 2522576 13418040    0    0     0   244 68462 6689 10  7 83  0  0  
   4  0 3163644 5671104 2522576 13407620    0    0    40   568 34157 4222  7  5 87  0  0  
   4  0 3163644 5980828 2522580 13401712    0    0    16   524 43754 6391 17  6 77  0  0  
   5  0 3163644 5506988 2522592 13418868    0    0   264   280 59452 5955 16  7 77  0  0  
   5  0 3163644 5577116 2522600 13417800    0    0    32   540 68056 8968 11  6 83  0  0  
   7  0 3163644 4747580 2522612 13451468    0    0    16   376 241800 7107 12 13 75  0  0 
   4  0 3163644 4948548 2522616 13440832    0    0    12   468 354599 5155  7 16 77  0  0 
  </code></pre>
  
  <p><strong>EDIT 5:</strong></p>
  
  <p>The results of a <code>top</code> command on the server:</p>
  
  <pre><code>top - 09:17:58 up 15 days,  1:53,  8 users,  load average: 6.90, 6.22, 5.34
  Tasks: 687 total,   3 running, 683 sleeping,   0 stopped,   1 zombie
  Cpu(s): 15.0%us,  3.4%sy,  0.0%ni, 80.7%id,  0.8%wa,  0.0%hi,  0.1%si,  0.0%st
  Mem:  49390000k total, 43364688k used,  6025312k free,  2697344k buffers
  Swap: 16482300k total,  3495772k used, 12986528k free, 11878096k cached
  </code></pre>