Skip to content

Instantly share code, notes, and snippets.

@m6w6
Created August 20, 2014 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m6w6/e3de44ef1a8260ecebde to your computer and use it in GitHub Desktop.
Save m6w6/e3de44ef1a8260ecebde to your computer and use it in GitHub Desktop.
artax 100xgoogle
<?php
require "vendor/autoload.php";
require "vendor/rdlowrey/after/lib/functions.php";
$promises = (new Artax\Client)->requestMulti(array_fill(0, 100, "http://www.google.com"));
$responses = After\all($promises)->wait();
foreach ($responses as $response) {
printf(
"HTTP/%s %d %s\n",
$response->getProtocol(),
$response->getStatus(),
$response->getReason()
);
}
@rdlowrey
Copy link

Yes, so I've broken the underlying After\all() dependency function. I'll fix it today. In the meantime you can manually workaround it for benchmarking purposes by doing the things that After\all() is supposed to hide from the user like so:

<?php
require __DIR__ . '/vendor/autoload.php';
require "vendor/rdlowrey/after/lib/functions.php";

define('REQ_COUNT', 100);
$count = 0;
$reactor = new Alert\NativeReactor;
$client = new Artax\Client($reactor);
$promises = $client->requestMulti(array_fill(0, REQ_COUNT, "http://www.google.com"));
$onComplete = function($error, $response) use (&$count, $reactor) {
    if ($error) {
        echo $error->getMessage();
    } else {
        printf(
            "HTTP/%s %d %s\n",
            $response->getProtocol(),
            $response->getStatus(),
            $response->getReason()
        );
    }
    if (++$count >= REQ_COUNT) {
        $reactor->stop();
    }
};

foreach ($promises as $promise) {
    $promise->when($onComplete);
}

$reactor->run();

The by-ref $count variable will increment in the $onComplete callback and stop the event loop when the last response resolves.

@m6w6
Copy link
Author

m6w6 commented Aug 21, 2014

After a composer update, I still get the same result?

installed:
bagder/ca-bundle dev-master origin/master
daverandom/addr v0.2.4 Asynchronous DNS resolver using Alert
daverandom/libdns v0.2.1 DNS protocol implementation written in pure PHP
rdlowrey/acesync v0.2.4 Non-blocking TCP socket connection/encryption tools
rdlowrey/after v0.2.1 Simple PHP concurrency primitives
rdlowrey/alert v0.10.2 Event loops and stream IO reactors for non-blocking PHP applications

█ mike@smugmug:~/src/artax.git<git=master...origin/master>$ php google100.php
HTTP/1.1 200 OK

@rdlowrey
Copy link

Yeah those are still the old version numbers. I'm something of a composer noob so I can't say exactly what the problem is, but re-cloning the repo and installing via composer fixed it for me. Your test script just worked fine on my end. I'm putting the commands here so you can copy/paste for convenience:

git clone https://github.com/rdlowrey/Artax.git
cd Artax
composer.phar install

One other note: the functions.php include in line 4 of your original snippet is no longer necessary as I fixed the relevant composer file to add it to the autoloader as well.

Please let me know if it still gives you problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment