While troubleshooting to find the reason for slow database connections from a PHP application (running in a Kubernetes container environment), the PHP version itself was tested as well. Would another PHP version improve performance? We're about to find out.
A simple PHP script (mysqltest.php) was created. It basically connects to a given MySQL/MariaDB server using the connection credentials at the begin and runs in a loop for 10'000 cycles. During each loop run, a basic SELECT NOW() query is executed on the database server.
<?php
$user = "user";
$password = "password";
$host = "mysqlserver";
try {
$conn = new PDO("mysql:host=$host", $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$queries = 10000;
$startTime = microtime(true);
for ($i = 0; $i < $queries; $i++) {
$result = $conn->query("SELECT NOW()")->fetchAll();
}
$elapsedTime = microtime(true) - $startTime;
$elapsedTime = round($elapsedTime, 6);
$averageTime = number_format($elapsedTime / $queries, 10);
echo "Time for $queries queries: $elapsedTime \n";
echo "Average time per query: $averageTime\n";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
At the end, the script returns the total run time needed to execute the 10k queries and an average time per query (thanks to Stefan Stefanov who created the script).
A new container with Ubuntu 16.04 (Xenial) was started (using the image ubuntu:xenial) and the following packages were installed:
root@xenial-6586845d5f-wr4hf:/# apt-get update && apt-get install php7.0-cli php7.0-mysql
Then the script was executed twice (with a pause of 2 min):
root@xenial-6586845d5f-wr4hf:~# php mysqltest.php && sleep 120 && php mysqltest.php
Time for 10000 queries: 17.596321
Average time per query: 0.0017596321
Time for 10000 queries: 18.602951
Average time per query: 0.0018602951
A new container with Ubuntu 18.04 (Bionic) was started (using the image ubuntu:bionic) and the following packages were installed:
root@bionic-f74f657c4-xqbv6:/# apt-get update && apt-get install php7.2-cli php7.2-mysql
Then the script was executed twice (with a pause of 2 min):
root@wislog-f74f657c4-xqbv6:~# php mysqltest.php && sleep 120 && php mysqltest.php
Time for 10000 queries: 16.110461
Average time per query: 0.0016110461
Time for 10000 queries: 16.35995
Average time per query: 0.0016359950
A new container with Ubuntu 20.04 (Focal) was started (using the image ubuntu:focal) and the following packages were installed:
root@focal-c46b7b945-b8lvr:/# apt-get update && apt-get install php7.4-cli php7.4-mysql
Then the script was executed twice (with a pause of 2 min):
root@focal-c46b7b945-b8lvr:~# php mysqltest.php && sleep 120 && php mysqltest.php
Time for 10000 queries: 14.528925
Average time per query: 0.0014528925
Time for 10000 queries: 16.701757
Average time per query: 0.0016701757
Having now tested the currently active Ubuntu LTS releases and their native PHP version, a side by side comparison reveals a major performance jump from PHP 7.0 to 7.2 (yes, 10% is pretty good!) and then another, minor, performance improvement from PHP 7.2 to 7.4.
Version | Try #1 |
Try #2 |
Avg Runtime |
Improv. % |
PHP 7.0 |
17.596321 | 18.602951 | 18.099636 | 0 |
PHP 7.2 |
16.110461 | 16.35995 | 16.2352055 | 10.30 vs. PHP 7.0 |
PHP 7.4 |
14.528925 | 16.701757 | 15.615341 | 13.73 vs. PHP 7.0 |
No comments yet.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Office PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder