Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkulhan committed Mar 15, 2015
0 parents commit fd989cb
Show file tree
Hide file tree
Showing 110 changed files with 10,531 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/composer.phar
/composer.lock
/vendor
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm

before_script:
- composer install --dev --prefer-source
- sudo rabbitmqctl add_vhost testvhost
- sudo rabbitmqctl add_user testuser testpassword
- sudo rabbitmqctl set_permissions -p testvhost testuser ".*" ".*" ".*"


script:
- ./vendor/bin/phpuint

services:
- rabbitmq
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015 Jakub Kulhan <jakub.kulhan@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Bunny

> Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library

## License

The MIT license. See `LICENSE` file.
30 changes: 30 additions & 0 deletions benchmark/consumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Bunny;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();
$ch = $c->connect()->channel();

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_queue", "bench_exchange");

$t = null;
$count = 0;

$ch->run(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
if ($t === null) {
$t = microtime(true);
}

if ($msg->content === "quit") {
printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t);
$c->stop();
} else {
++$count;
}

}, "bench_queue", "", false, true);

$c->disconnect();
46 changes: 46 additions & 0 deletions benchmark/consumer_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Bunny;

use Bunny\Async\Client;
use React\Promise;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();

$c->connect()->then(function (Client $c) {
return $c->channel();

})->then(function (Channel $ch) {
return Promise\all([
$ch,
$ch->queueDeclare("bench_queue"),
$ch->exchangeDeclare("bench_exchange"),
$ch->queueBind("bench_queue", "bench_exchange"),
]);

})->then(function ($r) {
/** @var Channel $ch */
$ch = $r[0];

$t = null;
$count = 0;

return $ch->consume(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
if ($t === null) {
$t = microtime(true);
}

if ($msg->content === "quit") {
printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t);
$c->disconnect()->then(function () use ($c){
$c->stop();
});

} else {
++$count;
}
}, "bench_queue", "", false, true);
});

$c->run();
46 changes: 46 additions & 0 deletions benchmark/endian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
define("N", 1000000);

function swapEndian64Strrev($s)
{
return strrev($s);
}

function swapEndian64Concat($s)
{
return $s[7] . $s[6] . $s[5] . $s[4] . $s[3] . $s[2] . $s[1] . $s[0];
}

function swapEndian64Index($s)
{
$rs = "00000000";
$rs[0] = $s[7];
$rs[1] = $s[6];
$rs[2] = $s[5];
$rs[3] = $s[4];
$rs[4] = $s[3];
$rs[5] = $s[2];
$rs[6] = $s[1];
$rs[7] = $s[0];
return $rs;
}

$s = pack("NN", 1, 1);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Strrev($s);
}
var_dump(microtime(true) - $t);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Concat($s);
}
var_dump(microtime(true) - $t);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Index($s);
}
var_dump(microtime(true) - $t);
38 changes: 38 additions & 0 deletions benchmark/producer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Bunny;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();
$c->connect();
$ch = $c->channel();

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_queue", "bench_exchange");

$body = <<<EOT
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza
EOT;

$time = microtime(true);
$max = isset($argv[1]) ? (int) $argv[1] : 1;

for ($i = 0; $i < $max; $i++) {
$ch->publish($body, [], "bench_exchange");
}

echo microtime(true) - $time, "\n";

$ch->publish("quit", [], "bench_exchange");

$c->disconnect();
60 changes: 60 additions & 0 deletions benchmark/producer_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace Bunny;

use Bunny\Async\Client;
use React\Promise;

require_once __DIR__ . "/../vendor/autoload.php";

$c = new Client();

$body = <<<EOT
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza
EOT;

$time = microtime(true);
$max = isset($argv[1]) ? (int) $argv[1] : 1;

$c->connect()->then(function (Client $c) {
return $c->channel();

})->then(function (Channel $ch) {
return Promise\all([
$ch,
$ch->queueDeclare("bench_queue"),
$ch->exchangeDeclare("bench_exchange"),
$ch->queueBind("bench_queue", "bench_exchange"),
]);

})->then(function ($r) use ($body, &$time, &$max) {
/** @var Channel $ch */
$ch = $r[0];

$promises = [];

for ($i = 0; $i < $max; $i++) {
$promises[] = $ch->publish($body, [], "bench_exchange");
}

$promises[] = $ch->publish("quit", [], "bench_exchange");

return Promise\all($promises);

})->then(function () use ($c) {
return $c->disconnect();

})->then(function () use ($c, &$time) {
echo microtime(true) - $time, "\n";
$c->stop();
});

$c->run();
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "bunny/bunny",
"description": "RabbitMQ (or generally AMQP 0.9.1) pure-PHP client library (does not require any ext).",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "Jakub Kulhan",
"email": "jakub.kulhan@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"psr/log": "~1.0",
"react/event-loop": "~0.4",
"react/promise": "~2.2"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
},
"autoload": {
"psr-4": {
"Bunny\\": [
"src/Bunny/",
"test/Bunny"
]
}
}
}
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">

<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php" phpVersion="5.4.0" phpVersionOperator=">=">test</directory>
</testsuite>
</testsuites>

</phpunit>
Loading

0 comments on commit fd989cb

Please sign in to comment.