PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: sockets blocking vs. not

Normal sockets in PHP are blocking. Does using a select manually improve things for just a single socket? Note this test depends on external things, and so is fairly inaccurate.

Run this test again Return to test menu

Historical Results

Based on (4) saved test runs, The test labeled "blocking" was (on average) the faster by 0.0111 seconds, (1.540% faster)

blocking 100%
non-blocking 98.46%

The blocking test took 0.7118 seconds.
The non-blocking test took 0.7229 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 186,863 loops for a total of 3,737,265 runs.

  • Line execution difference (0.000003) milliseconds.
  • Avg difference (0.000) milliseconds per 186,863 loops.
  • Total difference 11.13 milliseconds for 3,737,265 loops

The iteration variablity for Code 1 was (0.0000) milliseconds and Code 2 was (0.0000) milliseconds. The lower and the closer together there values are the more accurate the results are.

Code

The first test, "blocking", was:

$fp = fsockopen('127.0.0.1', 80, $errno, $error);
fwrite($fp,
	'GET / HTTP/1.1' . "\r\n" .
	'Host: localhost' . "\r\n\r\n");

$GLOBALS['dummy2'] = fread($fp, 100);
fclose($fp);

The second test, "non-blocking", was:

$fp = fsockopen('127.0.0.1', 80, $errno, $error);
stream_set_blocking($fp, false);

$r = array($fp);
$w = array($fp);
$e = array();

if (stream_select($e, $w, $e, 2))
	fwrite($fp,
	'GET / HTTP/1.1' . "\r\n" .
	'Host: localhost' . "\r\n\r\n");

if (stream_select($r, $e, $e, 2))
	$GLOBALS['dummy2'] = fread($fp, 100);

fclose($fp);

Running: Linux (x86_64:1 GB) PHP (7.2.15-1+ubuntu16.04.1+deb.sury.org+1)