Created
October 19, 2011 00:07
-
-
Save icheishvili/1297123 to your computer and use it in GitHub Desktop.
Using PHP streams to invoke things in parallel over a network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TcpMagic { | |
private $_socket = null; | |
public function __construct($host, $port) { | |
$this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); | |
socket_connect($this->_socket, $host, $port); | |
} | |
public function __destruct() { | |
$this->close(); | |
} | |
public function write($data) { | |
socket_write($this->_socket, $data); | |
} | |
public function read() { | |
$result = ''; | |
$chunk = ''; | |
do { | |
$chunk = socket_read($this->_socket, 8192); | |
$result .= $chunk; | |
} while ($chunk !== ''); | |
return $result; | |
} | |
public function close() { | |
if ($this->_socket) { | |
socket_close($this->_socket); | |
} | |
} | |
} | |
function serial() { | |
$t = new TcpMagic('api.beatport.com', 80); | |
$t->write("GET /catalog/2/tracks HTTP/1.0\r\n\r\n"); | |
echo strlen($t->read()) . PHP_EOL; | |
$r = new TcpMagic('api.beatport.com', 80); | |
$r->write("GET /catalog/2/releases HTTP/1.0\r\n\r\n"); | |
echo strlen($r->read()) . PHP_EOL; | |
$c = new TcpMagic('api.beatport.com', 80); | |
$c->write("GET /catalog/2/charts HTTP/1.0\r\n\r\n"); | |
echo strlen($c->read()) . PHP_EOL; | |
$a = new TcpMagic('api.beatport.com', 80); | |
$a->write("GET /catalog/2/artists HTTP/1.0\r\n\r\n"); | |
echo strlen($a->read()) . PHP_EOL; | |
$l = new TcpMagic('api.beatport.com', 80); | |
$l->write("GET /catalog/2/labels HTTP/1.0\r\n\r\n"); | |
echo strlen($l->read()) . PHP_EOL; | |
$g = new TcpMagic('api.beatport.com', 80); | |
$g->write("GET /catalog/2/genres HTTP/1.0\r\n\r\n"); | |
echo strlen($g->read()) . PHP_EOL; | |
} | |
function parallel() { | |
$t = new TcpMagic('api.beatport.com', 80); | |
$t->write("GET /catalog/2/tracks HTTP/1.0\r\n\r\n"); | |
$r = new TcpMagic('api.beatport.com', 80); | |
$r->write("GET /catalog/2/releases HTTP/1.0\r\n\r\n"); | |
$c = new TcpMagic('api.beatport.com', 80); | |
$c->write("GET /catalog/2/charts HTTP/1.0\r\n\r\n"); | |
$a = new TcpMagic('api.beatport.com', 80); | |
$a->write("GET /catalog/2/artists HTTP/1.0\r\n\r\n"); | |
$l = new TcpMagic('api.beatport.com', 80); | |
$l->write("GET /catalog/2/labels HTTP/1.0\r\n\r\n"); | |
$g = new TcpMagic('api.beatport.com', 80); | |
$g->write("GET /catalog/2/genres HTTP/1.0\r\n\r\n"); | |
echo strlen($t->read()) . PHP_EOL; | |
echo strlen($r->read()) . PHP_EOL; | |
echo strlen($c->read()) . PHP_EOL; | |
echo strlen($a->read()) . PHP_EOL; | |
echo strlen($l->read()) . PHP_EOL; | |
echo strlen($g->read()) . PHP_EOL; | |
} | |
$start = microtime(true); | |
serial(); | |
$end = microtime(true); | |
echo 'Serial invocation took ' . ($end - $start) . ' seconds' . PHP_EOL; | |
$start = microtime(true); | |
parallel(); | |
$end = microtime(true); | |
echo 'Parallel invocation took ' . ($end - $start) . ' seconds' . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment