PHP is slower than it should be
<p>I have a server with Dual Xeon Quad Core L5420 running at 2.5GHz. I've been optimizing my server, and have come to my final bottleneck: PHP.</p>

<p>My very simple PHP script:</p>

<p>./test.php</p>

<pre><code>&lt;?php print_r(posix_getpwuid(posix_getuid()));
</code></pre>

<p>My not-so-scientific-because-they-don't-pay-attention-to-thread-locking-but-scientific-enough-to-give-me-a-reasonable-multithreaded-requests-per-second-result scripts:</p>

<p>./benchmark-php</p>

<pre><code>#!/bin/bash
if [ -z $1 ]; then
  LIMIT=10
else
  LIMIT=$1
fi

if [ -z $2 ]; then
  SCRIPT="index.php"
else
  SCRIPT=$2
fi

START=$(date +%s.%N)
COUNT=0
while (( $COUNT &lt; $LIMIT ))
do
  php $SCRIPT &gt; /dev/null
  COUNT=$(echo "$COUNT + 1" | bc)
done
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
REQS_PER_SEC=$(echo "scale=2; $COUNT / $DIFF" | bc)
echo $REQS_PER_SEC
</code></pre>

<p>./really-benchmark-php</p>

<pre><code>#!/bin/bash
if [ -z $1 ]; then
  LIMIT=10
else
  LIMIT=$1
fi

if [ -z $2 ]; then
  THREADS=16
else
  THREADS=$2
fi

if [ -z $3 ]; then
  SCRIPT="index.php"
else
  SCRIPT=$3
fi

PIDS=""

echo '' &gt; results
for thread in `seq 1 $THREADS`; do
  ./benchmark-php $LIMIT $SCRIPT &gt;&gt; results &amp;
  PIDS="$PIDS $!"
done

for PID in $PIDS; do
  wait $PID
done

RESULTS=`cat results`
MATH="0"
for RESULT in $RESULTS; do
  MATH="$MATH + $RESULT"
done

echo "$MATH" | bc
</code></pre>

<p>The result of running <code>./really-benchmark-php 100 8 test.php</code> is ~137 requests per second.</p>

<p>Running the same script on a sqlite or mysql powered instance of Drupal returns ~1.5 req/s.</p>

<p>I have APC and mem_cache both installed, and I have verified that they're running on defaults. (Yes, APC's enable_cli is on, also.) Does someone know the magic "make PHP execute faster" switch?</p>

<p>I have an alternative configuration setup (FPM/FastCGI) that serves ~140 req/s of the MySQL Drupal install... how could that be possible if PHP itself can't even serve 2 req/s from the command line?</p>

<p>The result of the <code>ab</code> tool feel just as low to me:</p>

<p>static page: <code>ab -n 1000 -c 100 http://x.x.x.x/</code> Requests per second: 683.71</p>

<p>test php: <code>ab -n 100 -c 5 http://x.x.x.x/</code> Requests per second: 41.38</p>

<p>drupal-mysql: <code>ab -n 100 -c 10 http://x.x.x.x/drupal/</code> Requests per second: 0.24</p>

<p>drupal-sqlite: <code>ab -n 100 -c 10 http://x.x.x.x/drupal-test/</code> Requests per second: 4.92</p>
