Skip to content

Instantly share code, notes, and snippets.

@vibbow
Created November 18, 2019 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vibbow/8909ffc9990023129562e8971fe30f7d to your computer and use it in GitHub Desktop.
Save vibbow/8909ffc9990023129562e8971fe30f7d to your computer and use it in GitHub Desktop.
<?php
define('HOST', '192.168.163.111');
define('PORT', '80');
$test1_low = 0;
$test1_high = 0;
$test2_low = 0;
$test2_high = 0;
for ($i = 0; $i < 1000; $i++) {
// Test 1
$begin = microtime(true);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, HOST, PORT);
$end = microtime(true);
socket_close($socket);
unset($socket);
$cost1 = ($end - $begin) * 1000;
$cost1 < 5 ? $test1_low++ : $test1_high++;
// Test 2
$begin = microtime(true);
$fp = stream_socket_client("tcp://" . HOST . ':' . PORT);
$end = microtime(true);
fclose($fp);
unset($fp);
$cost2 = ($end - $begin) * 1000;
$cost2 < 5 ? $test2_low++ : $test2_high++;
// Summary
echo "{$i} - {$cost1} | {$cost2}" . PHP_EOL;
usleep(100);
}
echo "{$test1_low} | {$test1_high} | {$test2_low} | {$test2_high}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment