Skip to content

Instantly share code, notes, and snippets.

@septembermd
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save septembermd/9952015 to your computer and use it in GitHub Desktop.
Save septembermd/9952015 to your computer and use it in GitHub Desktop.
<?php
/**
* Test running a console command in threads (PHP pthread extension) vs loop vs &&
*/
class WorkerThreads extends Thread
{
private $workerId;
public $data;
public function __construct($id)
{
$this->workerId = $id;
}
public function run()
{
// sleep(1);
echo "[{$this->workerId}] Running" . PHP_EOL;
$this->data = shell_exec('phantomjs yslow.js -i basic example.com');
echo "[{$this->workerId}] Done" . PHP_EOL;
}
}
// Worker pool
$workers = [];
echo "Threads:" . PHP_EOL;
// Initialize and start the threads
foreach (range(0, 5) as $i) {
$workers[$i] = new WorkerThreads($i);
$workers[$i]->start();
}
// Let the threads come back
$time = microtime(true);
foreach (range(0, 5) as $i) {
$workers[$i]->join();
echo "[{$i}] Data: " . $workers[$i]->data . PHP_EOL;
}
$time = microtime(true) - $time;
echo "time: " . $time . PHP_EOL;
echo "Foreach: " . PHP_EOL;
$time = microtime(true);
foreach (range(0, 5) as $i) {
echo "[{$i}] ";
echo shell_exec('phantomjs yslow.js -i basic example.com');
}
$time = microtime(true) - $time;
echo "time: " . $time . PHP_EOL;
echo "&&: " . PHP_EOL;
$cmd = "phantomjs yslow.js -i basic example.com";
$time = microtime(true);
foreach (range(0, 5) as $i) {
$cmd .= " && phantomjs yslow.js -i basic example.com";
}
echo shell_exec($cmd);
$time = microtime(true) - $time;
echo "time: " . $time . PHP_EOL;
echo "ALL DONE" . PHP_EOL;
/*
Result:
$ php threads.php
Threads:
[0] Running
[1] Running
[2] Running
[3] Running
[4] Running
[5] Running
[1] Done
[0] Done
[0] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":333}
[1] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":324}
[2] Done
[2] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":327}
[3] Done
[3] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":321}
[5] Done
[4] Done
[4] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":305}
[5] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":317}
time: 5.6576030254364
Foreach:
[0] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":285}
[1] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":291}
[2] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":300}
[3] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":300}
[4] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":286}
[5] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":290}
time: 17.812369823456
&&:
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":306}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":303}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":289}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":286}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":311}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":291}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":293}
time: 20.895822048187
ALL DONE
*/
/*
Unexpected result in browser (Chrome)
[5] Running
[5] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
Content-type: text/html
[0] Running
[0] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
Content-type: text/html
[1] Running
[1] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
Content-type: text/html
[2] Running
[2] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
Content-type: text/html
[4] Running
[4] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
Content-type: text/html
[3] Running
[3] Done
HTTP/1.1 200 OK
Host: localhost:8088
Connection: close
X-Powered-By: PHP/5.5.8
Content-type: text/html
Threads:
[0] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":314}
[1] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":361}
[2] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":373}
[3] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":358}
[4] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":298}
[5] Data: {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":312}
time: 5.7645590305328
Foreach:
[0] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":304}
[1] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":294}
[2] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":916}
[3] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":295}
[4] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":332}
[5] {"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":297}
time: 19.387635946274
&&:
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":292}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":308}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":291}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":290}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":286}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":283}
{"v":"3.1.8","w":2540,"o":98,"u":"http%3A%2F%2Fexample.com%2F","r":2,"i":"ydefault","lt":475}
time: 21.387857913971
ALL DONE
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment