Permalink
Browse files

new benchmark scripts.

  • Loading branch information...
videlalvaro committed Nov 14, 2011
1 parent af92d95 commit 31278bda4c2a73dd5058f36bf0a28a6856c3fead
Showing with 112 additions and 0 deletions.
  1. +9 −0 benchmark/config.php
  2. +47 −0 benchmark/consumer.php
  3. +56 −0 benchmark/producer.php
View
@@ -0,0 +1,9 @@
+<?php
+define('HOST', 'localhost');
+define('PORT', 5672);
+define('USER', 'guest');
+define('PASS', 'guest');
+define('VHOST', '/');
+
+//If this is enabled you can see AMQP output on the CLI
+define('AMQP_DEBUG', false);
View
@@ -0,0 +1,47 @@
+<?php
+
+require_once(__DIR__ . '/../amqp.inc');
+include(__DIR__ . '/config.php');
+
+$exchange = 'bench_exchange';
+$queue = 'bench_queue';
+$consumer_tag = '';
+
+$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
+$ch = $conn->channel();
+
+$ch->queue_declare($queue, false, false, false, false);
+$ch->exchange_declare($exchange, 'direct', false, false, false);
+$ch->queue_bind($queue, $exchange);
+
+class Consumer
+{
+ protected $msgCount = 0;
+ protected $startTime = null;
+
+ public function process_message($msg)
+ {
+ if($this->startTime === null) {
+ $this->startTime = microtime(true);
+ }
+
+ if ($msg->body == 'quit') {
+ echo sprintf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $this->msgCount, microtime(true) - $this->startTime);
+ die;
+ }
+ $this->msgCount++;
+ }
+}
+
+$ch->basic_consume($queue, '', false, true, false, false, array(new Consumer(), 'process_message'));
+
+function shutdown($ch, $conn){
+ $ch->close();
+ $conn->close();
+}
+register_shutdown_function('shutdown', $ch, $conn);
+
+while(count($ch->callbacks)) {
+ $ch->wait();
+}
+?>
View
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * Usage:
+ * php producer.php 10000
+ * The integer arguments tells the script how many messages to publish.
+ */
+
+require_once(__DIR__ . '/../amqp.inc');
+include(__DIR__ . '/config.php');
+
+$exchange = 'bench_exchange';
+$queue = 'bench_queue';
+
+$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
+$ch = $conn->channel();
+
+$ch->queue_declare($queue, false, false, false, false);
+
+$ch->exchange_declare($exchange, 'direct', false, false, false);
+$ch->exchange_declare('control', 'fanout', false, false, false);
+
+$ch->queue_bind($queue, $exchange);
+
+
+$msg_body = <<<EOT
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
+abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza
+EOT;
+
+$msg = new AMQPMessage($msg_body);
+
+$time = microtime(true);
+
+$max = (int) $argv[1];
+
+// Publishes $max messages using $msg_body as the content.
+for ($i = 0; $i < $max; $i++) {
+ $ch->basic_publish($msg, $exchange);
+}
+
+echo microtime(true) - $time, "\n";
+
+$ch->basic_publish(new AMQPMessage('quit'), $exchange);
+
+$ch->close();
+$conn->close();
+?>

0 comments on commit 31278bd

Please sign in to comment.