What is preload.
Hi everyone, if you are a PHP developer like me then you will probably know that php has just released version 7.4 with some new features. Preload is a new feature added to PHP version 7.4, A feature that is expected to significantly improve the performance of your PHP app.
Preload works as follows:
- We have to customize a script file to specify the files we want to preload.
- The script we just customized above will be executed when the PHP service is started.
- All preloaded files will be stored on RAM and used to process incoming requests.
- When you change a preloaded file, the change will not be applied until you restart the PHP service.
Preload example
Install php7.4-fpm and nginx: You refer here Config php7.4-fpm: Open the file /etc/php/7.4/fpm/php.ini and edit the following config:
1 2 3 4 5 6 7 | ... opcache.preload=/var/www/php74/preload.php # Ở file này chúng ta chỉ định những file nào được preload ... opcache.preload_user=www-data ... ... |
Config nginx: Edit the content of the file / etc / nginx / sites-enabled / default to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | server { listen 80 default_server; listen [::]:80 default_server; root /var/www/php74; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ .php$ { try_files $uri /index.php =404; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_read_timeout 600; include fastcgi_params; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } } |
Create the directory containing the code you want to preload: In my example, I created a directory named php74 in / etc / www /. The php74 directory has the following structure:
1 2 3 4 5 | A.php B.php index.php preload.php |
The contents of the files are as follows:
1 2 3 4 5 6 7 8 9 10 | //A.php <? php class A { public function name () : string { return 'Hello PHP 7.4' ; } } |
1 2 3 4 5 | //B.php <? php require_on('A.php') class B extends A {} |
1 2 3 4 5 6 7 | //index.php <? php require_on('B.php') $ b = new B ; echo $ b- > name () . '<BR>' ; |
Here if you reload nginx and access it from the browser, you will get the error "B does not know the class" Edit the content of the preload.php file to:
1 2 3 4 5 6 7 8 9 | //preload.php <? php $ files = glob ( __DIR__ . '/**.php' ); foreach ( $ files as $ file ) { opcache_compile_file ( $ file ); } |
Next we reload the service php7.4-fpm and nginx. Accessing http: // localhost will get the result:
After restarting php7.4-fpm, as specified in the preload.php file, the php74 directories will be loaded into RAM and will be executed on the next request. But the file has been preloaded, then when you edit again, the new changes will not be applied until you restart the service php7.4-fpm. Now let's try to change the content of the A.php file to
1 2 3 4 5 6 7 8 9 | <?php class A { public function name () : string { return 'I HAVE BEEN CHANGED' ; } } |
After reloading the browser, the browser is still 'Hello PHP 7.4' => our changes in the A.php file have not been applied yet. If we now restart php7.4-fpm, our changes will be applied.
Performance
As mentioned at the beginning, the use of preload will greatly improve the performance of the app. You can refer to the results of benchmarks when using Ben Morel's preload here. According to Ben Morel's share, you can decide to just pre-load "hot class" – classes commonly used in your codebase. Ben Morel's benchmark shows that only about 100 hot classes are downloaded, actually providing better performance than preloading everything. Preloading all classes only increases the performance by 13%, while pre-loading of hot classes increases the efficiency by 17%.
So through the article I introduced and the preload example in PHP version 7.4, I hope my article will bring useful information to you.