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