php-amqplib vs PHP AMQP Extension

The Problem

While testing Futureproofs API performance using BlackFire I noticed that we had a bottleneck centred around an

fgets

method. It wasn’t part of our code (vague I know, I’ve lost the reference). After a bit of digging I found the culprit in the php-amqlib library; a library we use quite extensively from our API to add messages to RabbitMQ.

Now php-amqlib is a great library, we’ve used it a lot and its never once let us down. But I’m a stickler for performance and the idea that it might be holding back our API is something I felt might be worth investigating…

The rough ‘n’ ready test

We created a custom PHP script that performed 1000 requests against our local API which is running on a Vagrant machine using Ubuntu 14.04, PHP 5.6 on the Slim (2.6.2) framework. The same AMQP message was created in both instances using our custom abstract class and the cURL requests had to go through our Authentication layer, validators etc so the requests represent the differences between each implementation in “real life” scenarios.

For the purposes of this initial test I can’t give you code so you’ll have to take my word for it.

php-amqlib

This took 16.463 seconds
Average response time was: 0.163 seconds.
Max response time was: 0.321 seconds.
Min response time was: 0.134 seconds.

AMQP Extension

This took 6.920 seconds
Average response time was: 0.068 seconds.
Max response time was: 0.313 seconds.
Min response time was: 0.042 seconds.

Wow! So our preliminary tests reveal almost a 2.5x increase in requests when using the extension over the library. Not very scientific, but it indicates further research could be worthwhile.

Building a test environment

In an attempt to create a reusable test environment I created a simple vagrant box and a couple of tests. You can pull the repo down and run the tests yourself.

As you can see from the results below, the pecl-amqp library is blazingly fast, 2.5x faster on the simpler test (one) and 6.5x faster on the more complex test (two).

Experiment One

  • Exchange and Queue Deleted
  • Exchange and Queue Created
  • 10000 published messages
  • Queue/Channel closed
php-amqlib - 10000 iterations took 0.630 seconds
pecl-amqp - 10000 iterations took 0.244 seconds

Experiment Two

  • 1000 iterations
    • Exchange and Queue Deleted
    • Exchange and Queue Created
    • Publish Message
    • Queue/Channel closed
php-amqlib - 1000 iterations took 16.388 seconds
pecl-amqp - 1000 iterations took 104.647 seconds