0

I’m upgrading some system from PHP 5.5 (installed directly on AWS EC2), to PHP 7.4 in Docker (running on AWS EC2 using Docker Compose). The performance for image resizing using ImageMagick has decreased a lot.

Test script

To diagnose the issue, I’ve made the following simple script, with a 5MB JPEG as input:

<?php

$image_name = "imgbench.jpeg";
$image_size = 4000;
$image_quality = 50;

$image = new Imagick();
$image->readImage($image_name);
$image->stripImage();
$image->resizeImage($image_size, $image_size, Imagick::FILTER_LANCZOS, 1, true);
$image->setImageCompressionQuality($image_quality);
$image->setInterlaceScheme(Imagick::INTERLACE_JPEG);
$image_blob = $image->getImageBlob();

Measurements

From my measurements, it seems that the resizeImage() and getImageBlob() calls take the majority of execution time, as expected. I’ve run this script 5 times on the old and new system (through nginx with php-fpm), and found the following average execution times:

  • Old system (PHP 5.5): 2.5 seconds average
  • New system (PHP 7.4 in Docker): 14.8 seconds average

In other words, the PHP 7.4 setup is 6 times slower than the old PHP 5.5 setup! (The times were measured from within the PHP script, so nginx/php-fpm shouldn’t have played a role.)

For other kinds of requests, the new setup if performing fine (slightly faster than the old one, as may be expected from PHP 7). Both systems use the same type of EC2 instances, and no explicit resource limitations are setup for the Docker container in the new system. Therefore, I wouldn’t expect this resizing to be slower than before.

Does anyone have an idea what could cause this, and more importantly, how the performance can be improved in the new system?

System usage

The old system was in use while running the test script, but it had plenty of CPU/memory available. While running the test script, its CPU usage went to >300% (according to top).

The new system was unused while running the test script, so it also had plenty of CPU/memory available. However, here the CPU usage only went up to 150–180%, according to top and docker stats.

Setup

Old system

On the old system (using x64 Amazon Linux AMI version 1), PHP was set up as follows:

sudo yum install php55-pecl-imagick ...

And it shows the following info:

$ php -i
phpinfo()
PHP Version => 5.5.38

(...)

imagick

imagick module => enabled
imagick module version => 3.4.4
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
Imagick compiled with ImageMagick version => ImageMagick 6.7.8-9 2016-06-22 Q16 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.7.8-9 2016-06-22 Q16 http://www.imagemagick.org

New system

The new system uses Docker (sudo yum install docker on x64 Amazon Linux 2) with the following Dockerfile for the PHP container:

FROM php:7.4-fpm
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions imagick

(The same performance issue was present when installing the PHP extension imagick and libmagickwand-dev manually, without mlocati/php-extension-installer.)

This gives the following info:

$ docker-compose exec php php -i
phpinfo()
PHP Version => 7.4.13

(...)

imagick

imagick module => enabled
imagick module version => 3.4.4
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Jonathan
  • 401
  • 2
  • 4
  • 18
  • Are you 100% certain you used the same instance type? Which instance type is it? – Michael Hampton Dec 07 '20 at 15:53
  • @MichaelHampton Yes, positive! I’m using `t3.xlarge` with 4 vCPUs and 16GB memory (and unlimited CPU usage/credits), so that should be plenty… Actually, the old setup used to have smaller instances before (`t3.medium`), and even that performed a lot better than the new one. – Jonathan Dec 07 '20 at 15:56
  • I just tried everything again on a new `t3.medium` instance with the [Amazon Linux AMI](https://aws.amazon.com/marketplace/pp/Amazon-Web-Services-Amazon-Linux-AMI-HVM-64-bit/B00CIYTQTC) using php-cli (so without nginx/php-fpm). Simple `sudo yum install docker php55-cli php55-common php55-pecl-imagick`; then `php imgbench.php` for PHP 5, and `docker build` + `docker run -it php imgbench.php` for PHP 7 (using the scripts/Dockerfile from above). The results were similar to above. **Interestingly, PHP 7.0 in Docker did not have this problem, while all newer versions did (I tested 7.0 to 7.4).** – Jonathan Dec 07 '20 at 16:49
  • Cont.: I also tested PHP 7.0 to 7.2 without Docker (newer versions were now available through yum), and they all had good performance. So somehow it seems that PHP 7.1 to 7.4 in Docker suffer from this problem. I’ll be opening an issue for the Docker PHP image. – Jonathan Dec 07 '20 at 16:59
  • Issue reported: https://github.com/docker-library/php/issues/1100 – Jonathan Dec 07 '20 at 17:36

1 Answers1

1

I was able to work around the problem by using the php:alpine images instead.

The exact cause is not yet clear, see the following issue for more details: https://github.com/docker-library/php/issues/1100

Jonathan
  • 401
  • 2
  • 4
  • 18