Last active
December 3, 2022 15:07
-
-
Save rdlowrey/54171625334670ccb9f5 to your computer and use it in GitHub Desktop.
PHP vs Node.js scraping
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
/* | |
$ npm install request | |
$ node bench.js | |
*/ | |
var request = require('request'); | |
var url = 'http://www.google.com'; | |
var total_requests = 100; | |
var i; | |
var counter = 0; | |
var start = +new Date(); | |
for (i=0; i<total_requests; i++) { | |
request(url, function (error, response, body) { | |
if (error) { | |
console.log("error :("); | |
} else { | |
console.log(response.statusCode); | |
} | |
if (++counter == total_requests) { | |
var end = +new Date(); | |
console.log(i +", "+(end-start)/1000); | |
} | |
}); | |
} |
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 | |
/* | |
$ git clone https://github.com/amphp/artax.git | |
$ cd artax | |
$ composer install | |
$ php bench.php | |
*/ | |
require __DIR__ . '/vendor/autoload.php'; | |
use Amp\Artax\Client; | |
const URI = 'http://www.google.com'; | |
const MAX_SOCKETS = 5; // <-- Same as node's default (fairness!) | |
const TOTAL_REQUESTS = 100; | |
function onUpdate($data, $i) { | |
if ($data[0] === Amp\Artax\Notify::RESPONSE) { | |
echo "{$i} | ", $data[1]->getStatus(), "\n"; | |
} | |
} | |
$startedAt = microtime(1); | |
$client = new Client; | |
$client->setOption(Client::OP_HOST_CONNECTION_LIMIT, MAX_SOCKETS); | |
for ($i=0; $i<TOTAL_REQUESTS; $i++) { | |
$promises[] = $promise = $client->request(URI); | |
$promise->watch(function($data) use ($i) { onUpdate($data, $i); }); | |
} | |
$responses = Amp\wait(Amp\all($promises)); | |
var_dump(microtime(1) - $startedAt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty good internet
Results (http://www.example.com)
NodeJS v5.0.0
[email protected]
PHP 5.6.16 (cli)
amphp/artax 2.0
Node is ~83.6% faster?