Skip to content

Instantly share code, notes, and snippets.

@rdlowrey
Last active December 3, 2022 15:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdlowrey/54171625334670ccb9f5 to your computer and use it in GitHub Desktop.
Save rdlowrey/54171625334670ccb9f5 to your computer and use it in GitHub Desktop.
PHP vs Node.js scraping
/*
$ 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);
}
});
}
<?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);
@UnbrandedTech
Copy link

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
request@2.67.0

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