Added UNIX socket and test for it
This commit is contained in:
+73
-35
@@ -1,43 +1,15 @@
|
||||
<?php
|
||||
|
||||
class Socket
|
||||
abstract class Socket
|
||||
{
|
||||
const RECEIVE_TIMEOUT = 10;
|
||||
|
||||
private $sock;
|
||||
private $throw_exception_on_timeout;
|
||||
private $connected;
|
||||
private $host;
|
||||
private $port;
|
||||
|
||||
public function __construct($ip='', $port='', $throw_exception_on_timeout = false, $auto_connect = true)
|
||||
{
|
||||
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
if ($this->sock === FALSE)
|
||||
{
|
||||
throw new Exception("Error creating socket: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = false;
|
||||
$this->host = $ip;
|
||||
$this->port = $port;
|
||||
if ($auto_connect)
|
||||
{
|
||||
$this->connect();
|
||||
}
|
||||
$this->throw_exception_on_timeout = $throw_exception_on_timeout;
|
||||
}
|
||||
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connected) return;
|
||||
// тушим ошибки, иначе вывалится с PHP Error вместо exception
|
||||
if (@socket_connect($this->sock, $this->host, $this->port) === FALSE)
|
||||
{
|
||||
throw new Exception("Error connecting: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = true;
|
||||
}
|
||||
protected $sock;
|
||||
protected $throw_exception_on_timeout;
|
||||
protected $connected;
|
||||
|
||||
abstract public function connect();
|
||||
|
||||
/**
|
||||
* Закрытие соединения
|
||||
*/
|
||||
@@ -51,7 +23,7 @@ class Socket
|
||||
}
|
||||
|
||||
/*
|
||||
* Пропихивает строку в сокет. Бросит exception если случилась лажа.
|
||||
* Пропихивает строку в сокет.
|
||||
*/
|
||||
|
||||
public function iterativeSend($s)
|
||||
@@ -156,4 +128,70 @@ class Socket
|
||||
return $this->iterativeReceive($len, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class IPSocket extends Socket
|
||||
{
|
||||
private $host;
|
||||
private $port;
|
||||
|
||||
public function __construct($ip='', $port='', $throw_exception_on_timeout = false, $auto_connect = true)
|
||||
{
|
||||
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
if ($this->sock === FALSE)
|
||||
{
|
||||
throw new Exception("Error creating socket: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = false;
|
||||
$this->host = $ip;
|
||||
$this->port = $port;
|
||||
if ($auto_connect)
|
||||
{
|
||||
$this->connect();
|
||||
}
|
||||
$this->throw_exception_on_timeout = $throw_exception_on_timeout;
|
||||
}
|
||||
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connected) return;
|
||||
// тушим ошибки, иначе вывалится с PHP Error вместо exception
|
||||
if (@socket_connect($this->sock, $this->host, $this->port) === FALSE)
|
||||
{
|
||||
throw new Exception("Error connecting: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
class UnixSocket extends Socket
|
||||
{
|
||||
private $path;
|
||||
|
||||
public function __construct($path='', $throw_exception_on_timeout = false, $auto_connect = true)
|
||||
{
|
||||
$this->sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
||||
if ($this->sock === FALSE)
|
||||
{
|
||||
throw new Exception("Error creating socket: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = false;
|
||||
$this->path = $path;
|
||||
if ($auto_connect)
|
||||
{
|
||||
$this->connect();
|
||||
}
|
||||
$this->throw_exception_on_timeout = $throw_exception_on_timeout;
|
||||
}
|
||||
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connected) return;
|
||||
// тушим ошибки, иначе вывалится с PHP Error вместо exception
|
||||
if (@socket_connect($this->sock, $this->path) === FALSE)
|
||||
{
|
||||
throw new Exception("Error connecting: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||
}
|
||||
$this->connected = true;
|
||||
}
|
||||
}
|
||||
+38
-1
@@ -4,6 +4,9 @@ require_once('Socket.php');
|
||||
|
||||
function makeRequest($size)
|
||||
{
|
||||
$result = '';
|
||||
for ($i = 0; $i < $size; $i++) $result .= chr(rand() % 256);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function testSocket($amount, $reconnect, $size)
|
||||
@@ -14,7 +17,30 @@ function testSocket($amount, $reconnect, $size)
|
||||
{
|
||||
if ($s === null)
|
||||
{
|
||||
$s = new Socket('127.0.0.1', 19876);
|
||||
$s = new IPSocket('127.0.0.1', 19876);
|
||||
$s->connect();
|
||||
$current = 0;
|
||||
}
|
||||
$m = makeRequest($size);
|
||||
$s->sendMessage($m);
|
||||
$r = $s->receiveMessage();
|
||||
$current++;
|
||||
if ($current >= $reconnect)
|
||||
{
|
||||
$s = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testUnixSocket($amount, $reconnect, $size)
|
||||
{
|
||||
$s = null;
|
||||
$current = 0;
|
||||
for ($i = 0; $i < $amount; $i++)
|
||||
{
|
||||
if ($s === null)
|
||||
{
|
||||
$s = new UnixSocket('/tmp/ipc_test_socket');
|
||||
$s->connect();
|
||||
$current = 0;
|
||||
}
|
||||
@@ -35,9 +61,20 @@ $request_size = $argv[3];
|
||||
|
||||
print('Testing '.$requests_amount.' requests, reconnect after '.
|
||||
$requests_per_connect.' size '.$request_size."\n");
|
||||
|
||||
|
||||
print("Testing IP socket:\n");
|
||||
$start = microtime(true);
|
||||
testSocket($requests_amount, $requests_per_connect, $request_size);
|
||||
$finish = microtime(true);
|
||||
|
||||
$elapsed = $finish - $start;
|
||||
print('Total: '.round($elapsed, 3).' seconds, '.round($requests_amount / $elapsed, 3).' RPS'."\n");
|
||||
|
||||
print("Testing Unix socket:\n");
|
||||
$start = microtime(true);
|
||||
testUnixSocket($requests_amount, $requests_per_connect, $request_size);
|
||||
$finish = microtime(true);
|
||||
|
||||
$elapsed = $finish - $start;
|
||||
print('Total: '.round($elapsed, 3).' seconds, '.round($requests_amount / $elapsed, 3).' RPS'."\n");
|
||||
|
||||
Reference in New Issue
Block a user