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); |
Would you be able to rerun the benchmark for node with 0.12.0
and io.js 1.5.1
and post the results?
Pretty good internet
UnbrandedTech:~ james$ speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Sai gon Postel Corporation (180.93.246.1)...
Selecting best server based on latency...
Hosted by FPT Telecom (Ho Chi Minh City) [7.56 km]: 4.095 ms
Testing download speed........................................
Download: 40.80 Mbit/s
Testing upload speed..................................................
Upload: 44.87 Mbit/s
Results (http://www.example.com)
NodeJS v5.0.0
[email protected]
UnbrandedTech:~ james$ node benchmark.js
100, 0.788
PHP 5.6.16 (cli)
amphp/artax 2.0
UnbrandedTech:~ james$ php benchmark.php
float(4.8010399341583)
Node is ~83.6% faster?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@raitucarp Ask and you shall receive 😀
Good old fashioned garbage residential internet:
I now get redirected to captchas when trying to query google more than a couple of times in succession (imagine that) ... so instead I re-ran the same benchmark in the code posted above but used the encrypted
https://github.com
as the target URI. Equivalent results:/cc @mrseth