xcache doesn't speed up my php application - php

I have a php application which provides restful service,I want to speed it by xcache,but the situation is not as imagined.
My php application bases on yii 1.1,and the function I test does heavy db query and calculation.The elapsed time if not use xcache is 600ms(300ms db query) and things are on the whole the same if use xcache.
I can see xcache is really working by accessing xcache admin page.
Xcache can save time by avoiding creating reduplicated opcode,theoretically it can speed my application,but it doesn't here,so can someone explain?

You should try enable opcache in php ini that is now bundled in as part of php core. a better alternative would be to try and optermise the heavy query

Profile your application. You can't expect a significant speedup if the bottleneck is IO or some poorly optimized DB queries.

Opcache is now the standard, and it's highly recommended to use it.
It's also seemless, you don't need to do a thing (other than installing it) as Opcache manages all the setting and getting of Op Code caches for you transparently.
Alternatively, you could attempt to diagnose your query by looking at database indexes, using EXPLAIN (if you're using MySQL) and caching the results.

I think the reason is that compilation time only the small part of the whole.I'll test it and put the result here later.

Related

Understanding OPCache behavior

Even tho the website is behind CloudFlare CDN, we decided to use OPCache to reduce the load on server as there are a maximum (peak) of ~400 active users per second (most of the time it's 50-100 u/s).
But most pages have some different data for each user, for example user's dashboard overview, most of the data is same but it has some different numbers for each user that needs to be up to date.
My Questions are:
Is it wise to use OPCache for such a website?
How will it handle pages with unique queries? will it take more RAM (caching multiple pages for each user) than running without OPCache?
Will it affect performance on pages such as Signup/Login etc?
I read that all PHP applications should use OPCache, is that correct?
P.S. The website is running on PHP 7.3.4
Yes it will gain performance improvement
If your RAM usage increased and you're concerned with it, you can fine tune memory consumption via opcache.memory_consumption
Affect meaning they will be faster
Yes, there really is no reason to not use OPCache other than edge cases (e.g., when your app's source code changes at a very fast rate or testing).
TLDR: Production code should always have OPCached enabled.
OPcache only caches the internal opcode representation of a PHP script, not its output. The queries performed or the content displayed by the page has no impact on how OPcache will behave.
This caching will improve the performance of all PHP web pages. As such, it should always be enabled on production sites.
Each PHP script is being compiled at runtime,
It takes time to transform the human readable code into a code that can be understood by the machine.
OpCache is a bytecode cache engine , it will compile the script to bytecode script only once - so you will save time - then the precompiled script is being stored in memory, which should lead to performance boosts in your PHP applications.
What i think you are missing is that the opcache DOES NOT cache the result of the script , but it is just compile the script .
Note this method is not good if the php script it self is changing for each user
or if it is loaded from -let's say for example- a database
As general view for this approach
Php caching whatever it was APC cache or opcache or else is amazing strategy this should increase php performance by 50% in general
As it act as follows
When php script excuted there's this involve three major steps
1-parse of the script
2-compile of the script
3-output
APC cache or else act as intermediary as it saves php script in the compiled form so php will start in the output stage directly and this will not affect queries but it will increase its speed as query for MySQL or else not just the statement of SELECT or whatever but it involves execution of extensions like PDO so it will become faster
You can classify extension as follows
APC or Xcache for php 5
In new php version use opcache
All of them have the same principle
Some developers like APC other like opcache
As example X-cart the popular shopping platform use xcache .

using both Opcache and Xcache

I'm just wondering is it stupid to use both Xcache 3 with Zend Opcache at the same time to cache PHP files? I know that both do almost the same job, but not sure if that would make any difference on the performance and speed.
I want to speed up my php page load so that visitors don't need to wait long.
any thoughts on that?
To answer your question: Yes, you should not run xcache and Zend Opcache at the same time. If you do, you'll get undefined behaviours, most notably "cannot redeclare class XYZ" fatal errors. That happened to me after a systems upgrade, where the packet maintainer activated Zend Opcache along the already existing xcache installation.
As for the matter of which of both to use for opcaching, that depends on your specific code - I'd recommend setting up a test environment and firing up the Apache Benchmark or a similar tool to check the answer times.
On a default wordpress installation, I was able to get a speedup (uncached vs xcache) of about 5-7x, which is quite significant. If you really need more, you'll need to check out the other possibilities already mentioned in the comments like
using a loadbalancer and multiple application servers
using memcache or memcached to cache database queries and other load heavy operations
switching to another database system like a NoSQL system (be careful of the consequences)
changing your architecture to a static site with webservices providing interactive content

is it possible to use php accelerators with zend framework

is it possible to use accelerators such as Memcache, APC, etc on a PHP+ZendFramework application and still have it work without Zend Framework going kaputt
Yes you can use PHP accelerators such as APC, eAccelerator which cache the PHP byte code (http://en.wikipedia.org/wiki/List_of_PHP_accelerators). Those type of cache just keep your "PHP scripts" in memory. It won't directly affect Zend Framework.
You can also use Memcache by using Zend_Cache (http://framework.zend.com/manual/en/zend.cache.introduction.html). Memcache is generally use to store raw data such as DB result, Page result.
I use APC under nginx+php-fpm in production with an ZF app and it works great.
(You can use it under apache as well).
It does provide a big boost, but you might want to setup a cron job to clear the apc cache once a day, after a while in certain conditions the memory gets fragmented and the performance drops a bit.

should I add a php APC to my server

A friend has recommended that I install php APC, claiming it will help php run faster and use less memory
sounds promising but I'm a little nervous about adding it to my VPS server
I have one small app that I've built using codeigniter, and several sites that use the popular slideshowpro photo gallery software
could install this break any of the back end code on my sites?
I'm no high tech server guy, but should I give this a try?
Depends entirely on your situation.
Is your site unresponsive or slow at the moment? Is this definitely due to the PHP scripts and not any other data sources such as a database or remote API?
If you answered yes to the above, then installing one of the many PHP accelerators out there would be a good shout. As for using less memory, that's largely dependent on your apache/lightppd/nginx config and php.ini variables.
Most PHP accelerators work by converting the (to be) interpreted PHP code into opcode. This is then stored in memory (RAM) for fast access. If you haven't already implemented file-based caching in CodeIgniter then the benefits of installing a PHP accelerator would be noticeable. If you haven't, then I suggest you do that first before moving straight over to (wasting?) spending time trying to install APC manually.
If your site is currently performing well and you're not too confident in your *nix skills then I suggest you try implementing CodeIgniter caching first rather than try messing with what is an already working VPS.
My personal preference is PHP eAccelerator.
Should installing a PHP cache engine not improve your site's performance then I suggest you look at what other factors influence your application. As stated above, these could be: database or API to name a few.
Hope this helps.
APC is basically a cache engine that stores your compiled php scripts on a temp location on your server. Meaning that these do not have to be interpreted every time someone calls your sccript. It is a PHP extension can can safely be turned ON or OFF and it does not affect your actual code. So... do not fear!
When a php script is processed, there is a compilation phase, where php converts the source code of the php files into "opcodes". APC simply caches the result of this compilation phase, so it should be safe to turn on.
That said, when making such changes to production code it is always wise to run a regression test to ensure no new issues have been introduced.

Is using PHP accelerators such as MMCache or Zend Accelerator making PHP faster?

Does anybody have experience working with PHP accelerators such as MMCache or Zend Accelerator? I'd like to know if using either of these makes PHP comparable to faster web-technologies. Also, are there trade offs for using these?
Note that Zend Optimizer and MMCache (or similar applications) are totally different things. While Zend Optimizer tries to optimize the program opcode MMCache will cache the scripts in memory and reuse the precompiled code.
I did some benchmarks some time ago and you can find the results in my blog (in German though). The basic results:
Zend Optimizer alone didn't help at all. Actually my scripts were slower than without optimizer.
When it comes to caches:
* fastest: eAccelerator
* XCache
* APC
And: You DO want to install a opcode cache!
For example:
alt text http://blogs.interdose.com/dominik/wp-content/uploads/2008/04/opcode_wordpress.png
This is the duration it took to call the wordpress homepage 10.000 times.
Edit: BTW, eAccelerator contains an optimizer itself.
MMCache has been deprecated. I recommend either http://pecl.php.net/package/APC or http://xcache.lighttpd.net/, both of which also give you variable storage (like Memcache).
Both are interesting and will provide speed boost since they compile source code into binary representation which is then executed by the PHP engine.
Any huge web site running with PHP (Facebook for example) is running some sort of opcode cache system like MMCache.
The problem is that they are not very easy to set up depending on your system.
Depending on how much of your PHP code is actually executed and how long that execution takes they can be a really big win. It certainly isn't going to hurt, but the gain you see will very much depend on where your time is currently spent.
btw mmcache has been rolled into a different project now, I forget the name but Google will tell you.
I use APC on my production servers and it works pretty well out of the box. Compile it and add it to PHP and there isn't much tweaking left to do for it. I check it every once in a while just to review stats but since I use MVC a lot all of the main files (routers, controllers, etc) rarely change on a day-to-day basis so that code stays compiled and runs pretty efficiently.
currently we use apc, free and was just a simple plug and play on our live servers. Provided a huge performance increase for our site, especially as the project size increased. I also have the apc.stat disabled so it doesn't check if the code has been updated, so whenever we need to update the code on the live site we restart apache.
I use APC, and can attest that it can dramatically reduce the CPU and I/O load on an app server if you maintain a high cache-hit rate. It not only saves you from having to compile, it can save you from having to read the php files from disk at all. (i.e. the bytecodes are served directly from main memory, so it's super fast) It lowers the speed to render a single page, and increases the requests per second your server can handle.
If you use RedHat or CentOS, installing APC is super simple:
yum install php-devel httpd-devel php-pear
pecl install apc
echo "extension=apc.so" > /etc/php.d/apc.ini
# if you're using SELinux:
chcon "system_u:object_r:textrel_shlib_t" /usr/lib/php/modules/apc.so
/etc/init.d/httpd restart
You asked about downsides. The only downside is that it requires some memory. The default on APC is 30MB, but it can be adjusted, and the cost of a little bit of memory more than pays for itself with the increased speed and response rate.
BlaM's testing included all the DB calls made by WordPress. When you're making fewer DB calls, you'll see the performance gain of opcode caches be even more dramatic.
I used Zend Accelerator a little back in the day (2004-ish). It certainly gave some significant performance wins on code it could work with, but unfortunately the system I was using was designed to quite often dynamically load code and then eval it, which Zend Accelerator couldn't do much with at the time (and I'd guess still can't).
On the down side, we certainly saw some caching issues (where the code would be changes, but the compiled version sync with the change for one reason or another). I imagine those problems have likely been ironed out by now.
Anyway, I don't have any hard comparison numbers, and certainly didn't write the same system in different environments for comparison, but for the vast majority of systems, PHP isn't going to kill you performance wise.
Have you checked out Phalanger? It compiles PHP to .NET code. Here are some benchmarks which show that it can dramatically improve performance.

Categories