Categories
PHP

Which php5.5 zend optimizer settings to use?

Hello,

Today I’ve got an email from Steve Corona, since I am subscribed to the book he released, scaling PHP. Pretty nice work his done here! šŸ˜‰

I am copying pasting part of his email since I couldn’t find it anywhere, and I guess more people may be interested in this.

Which php5.5 zend optimizer settings to use?

Instead of letting this knowledge go to waste, I want to share it with you. I had to spend the time figuring out the best REAL WORLD settings for Zend Optimizer.

These settings are straight from my php.ini file from one of my apps that does 117 million HTTP requests per day. I’ll explain what each one does and why it’s important so you can tweak it for your setup.

opcache.revalidate_freqĀ – Basically put, how often (in seconds) should the code cache expire and check if your code has changed. 0 means it checks your PHP code every single request (which adds lots of stat syscalls). Set it to 0 in your development environment. Production doesn’t matter because of the next setting.

opcache.validate_timestampsĀ – When this is enabled, PHP will check the file timestamp per your opcache.revalidate_freq value.

When it’s disabled, opcache.revaliate_freq is ignored and PHP files are NEVER checked for updated code.Ā So, if you modify your code, the changes won’t actually run until you restart or reload PHP (you force a reload with kill -SIGUSR2).

Yes, this is a pain in the ass, but you should use it. Why? While you’re updating or deplying code, new code files can get mixed with old onesā€” the results are unknown. It’s unsafe as hell.

opcache.max_accelerated_filesĀ – Controls how many PHP files, at most, can be held in memory at once. It’s important that your project has LESS FILES than whatever you set this at. My codebase has ~6000 files, so I use the prime number 7963 for max_accelerated_files.

You can run “find . -type f -print | grep php | wc -l” to quickly calculate the number of files in your codebase.

opcache.memory_consumptionĀ – The default is 64MB,Ā I set it to 192MB because I have a ton of code. You can use the functionĀ opcache_get_status()Ā to tell how much memory opcache is consuming and if you need to increase the amount (more on this next week).

opcache.interned_strings_bufferĀ – A pretty neat setting with like 0 documentation. PHP uses a technique calledĀ string interningĀ to improve performanceā€” so, for example, if you have the string “foobar” 1000 times in your code, internally PHP will store 1 immutable variable for this string and just use a pointer to it for the other 999 times you use it. Cool. This setting takes it to the next levelā€” instead of having a pool of these immutable string for each SINGLE php-fpm process, this setting shares it across ALL of your php-fpm processes. It saves memory and improves performance, especially in big applications.

The value is set in megabytes, so set it to “16” for 16MB. The default is low, 4MB.

opcache.fast_shutdownĀ – Another interesting setting with no useful documentation. “Allows for faster shutdown”. Oh okay. Like that helps me. What this actually does is provide a faster mechanism for calling the deconstructors in your code at the end of a single request to speed up the response and recycle php workers so they’re ready for the next incoming request faster. Set it to 1 and turn it on.

in php.ini

opcache.revalidate_freq=0
opcache.validate_timestamps=0 (comment this out in your dev environment)
opcache.max_accelerated_files=7963
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1