Phil Sturgeon wrote this article about benchmarking.
What I am concerned with is this technology called ReactPHP.
I have been to their GitHub, but still cannot wrap my mind around it without an example.
Thankfully, I can use Phil's repo.
At line 12, Phil created a loop with ReactPHP.
https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L12
Then he started to use the loop at line 24
https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L24
My questions are:
- what is the purpose of this
$loop
? - It says on the ReactPHP, it is a simple web server. Does that mean it replaces nginx or Apache?
- When is a good time to use ReactPHP or any similar technology? When is NOT a good time to use it?
I am trying to learn about this technology, so if I have worded this question badly, do let me know so I can rewrite this.
EDIT:
Since I am getting feedback that the question is too broad, let me narrow my question and make it refer to a code example.
Taking the code sample from https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L12,
which I have duplicated below. What does $loop
do?
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$factory = new React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);
echo "Page number, Time taken";
for ($page = 1; $page <= $total_page; $page++) {
$loop->addTimer(0.001, function($timer) use ($client, $page) {
$buffer = '';
$request = $client->request('GET', 'http://fantasy.premierleague.com/my-leagues/303/standings/?ls-page='.$page);
$request->on('response', function($response) use (&$buffer) {
$response->on('data', function($data) use (&$buffer) {
$buffer .= $data;
});
});
$request->on('end', function() use (&$buffer, $page) {
\phpQuery::newDocument($buffer);
foreach (pq('.ismStandingsTable tr') as $data) {
foreach (pq('td', $data) as $key => $val) {
if ($key == 2) {
// print pq($val)->text();
}
}
}
$time_end = microtime(true);
$execution_time = $time_end - $GLOBALS['time_start'];
echo ("\n".$page.", ".$execution_time);
});
$request->end();
});
}
$loop->run();