Config apc in zend framework 2 - php

i need to maximize performance in a zend framework 2 + doctrine 3 project , the application is slow so i've woked in enabling the memcache for some queries , i've also enabled the cache for the config files ... but the application stills slow so searching in the net i found some articles talking about performing zf2 project and mentioning apc as a way to make the application not so slow , so i've installed apc, but i can't find a way to configure how to enable apc in zend framework 2?

APC is a PHP extension, it's not something you need to enable within individual applications unless you're using it for userland caching as well (which I assume you are not, since you mentioned memcache). You can check if it's working by firing up a phpinfo page and seeing if you have an APC section.
If you have PHP 5.5 or above, you don't need (and shouldn't use) APC, as PHP comes with its own (better) opcache.
I'd suggest you benchmark your application using something like XDebug to find out what is causing the slowness. This should give you a better idea what you need to improve.

Related

PHP APC-opecode cache and file based output cache

I am running PHP Version 5.3.5 and developing a website in Codeigniter 2.1.3
I am working now to improve website performance, and I found caching can be used like APC or memcached or Zend OpCache.
In codeigniter I used $this->output->cache(43829); for caching.
Output of actions got cached, and It returns same output on every request.
But I have dynamic pages so this caching will not work. I am looking around how APC can be used for ope-code caching and used to enhance web site performance.
Is APC stores opecode in cache that cane be used by zend engine for execution execution, without regenerating opecode on each request?
Attached is my PHP APC configuration
PHP 5.3 is nearly 2 years past end of life, and APC is not available for current PHP versions. PHP 5.5+ comes with Zend Opcache, which has equivalent functionality. Instead of trying to get your site to work with old technologies, simply upgrading your server to a current (and secure) version of PHP would give you this performance boost without you having to do anything else.
Output of actions got cached, and It returns same output on every request
While its faster than regenerating the page, its still far from optimal. Better to use a caching reverse proxy in such a case.
I am looking around how APC can be used for op[e]-code caching
Your question implies that you think you are not already using op-code caching. I suggest your first port of call is to see if it is already enabled. You can see this from the output of phpinfo() or check function_exists('apc_sma_info');
APC is unlikely to be provisioned for future versions of PHP. ZOP+ is the opcode cache which is currently bundled with PHP 5.4+. It will work with 5.3, but requires some effort. Tim is correct in saying the 5.3 is past its sell-by-date and should be upgraded.
Note that ZOP+ also includes an optimizer (and, rather alarmingly, it seems to have a significant impact on the performance of many frameworks). The performance of ZOP+ is more dependent on sizing the cache than in APC - but even with APC, sizing the cache wrongly will not give good results.

Symfony 2 performance optimisations

We're looking for a PHP framework to work with in future and are currently testing out things with Symfony 2. For this, we've redesigned our API and implemented it as a bundle in Symfony. It turned out that Symfony seems to be very slow - actually far slower than our old (not even well-designed) system.
We tried to optimise the performance by caching the byte code (using APC for this). While we've noticed a huge improvement in performance (before: about 3 seconds to load the API; after: 0.6 seconds in average (still 0.5 seconds slower than our old system without APC)), we're kind of excited - but still not really pleased with the high loading time of such an easy task like getting one result out of an almost empty database.
I don't know, but I could imagine this is due to Symfony autoloading all classes, even when not needed for a specific bundle.
Now, before we deep-six Symfony, we'd like to look out for further optimisations, possibly a way to exclude unneeded components in a specific bundle, as I personally think this would make a big difference.
I'd be thankful for any ideas on how to further improve the performance, experience reports with using Symfony or anything else that could be helpful for us on the lookout for a framework.
Edit:
Some information about the testing environment:
Operating system: Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-38-generic x86_64)
Apache version: Apache/2.2.22 (Ubuntu)
PHP version: 5.3.10-1ubuntu3.13
Considerable PHP extensions: apc
Also, all tests are done on a local copy of our system, so possible network issues can be excluded.
These points can optimise your application performance:
Upgrade PHP. The performance gain between PHP 5.3 and PHP 5.4 is very high. PHP 5.5 would be even better, but it's not supported by all distributions, like Debian 7.
NGINX is faster than Apache and the configuration is easier.
Using PHP-FPM with NGINX is a good combination. You can also run your PHP with HHVM which is in average 2x faster than PHP-FPM, provided you replace Symfony/Assetic with Grunt. Caution: HHVM requires more precaution and testing before deploying safely. You can follow these two articles (in French): JoliCode and Big Brains Company
PHP APC extension is deprecated. I think that XCache, Memcached or Redis are better, and they're also most supported at the moment. For PHP >= 5.5, APCu can be used as a replacement for APC.
Additionally, you can read a few articles which talk about Symfony2 optimisation and provide Twig benchmarks.
PHP articles:
Script high-performance in French
Comparison of PHP 5.3 and PHP 5.6 performance in French
Check this article to apprehend the PHP optimisation in French
Google's recommendations for optimising PHP applications in English
10 best pratices to optimise PHP in English
Symfony2 and Twig articles:
Symfony documentation gives a few tips how to build a performant application in English
Template optimisation in French
Twig include optimisation in French
Use #Cache annotation requests in English
Symfony2: Good practices in French
Limit the usage of unnecessary bundles
Symfony2 Twig performance optimisation in English
If you don't want to use the Twig Engine, you can disable it in French
Caching in Symfony from it's Cookbook - really impressive!
Others optimisations:
Maybe you can use an updated version of Ubuntu.
Personally, I prefer using Debian which is also popular for servers because it's very stable.
Using a cache proxy like Varnish can be a finishing touch.
Varnish requires a developer's implication and maybe a formation. Using NGINX FastCGI Cache to limit FastCGI requests to HHVM, FPM or PHP-NG can resolve speed response.
Did you look this blog post ? http://symfony.com/blog/push-it-to-the-limits-symfony2-for-high-performance-needs
According to your information, i can advise you to try php 5.5 or 5.6 and NGINX with PHP-FPM, it can 40% faster or more.
You can try approach suggested in this article: http://stfalcon.com/en/blog/post/performance-symfony2-doctrine2-orm
The author suggests to:
Download all necessary connections
Update multiple entities by request
Get data as an associative array
Use reference proxies
Don't forget to use Symfony profiler toolbar
The code described is availiable on github: https://github.com/lensky84/performance

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

Zend Framework 2 large page load time

I have installed the Skeleton application of ZF2 (from official GitHub repo), and the first page I see takes 400-700ms to load (default "Welcome to Zend Framework 2" page, with no database connections and without handling anything).
In raw PHP (without frameworks) It'll take a few ms. to load.
Could You explain, what caused such a big delays?
I'm new to ZF, and now deciding, to use or not to use it.
Zend Framework is a heavy php framework which uses a lot of php files. Because php is evaluted on the spot it takes a lot of time every request to evaluate these files. You should use a Opcode cache like apc and many others or if you're using php 5.5 Opcache that is built -in. An Opcode cache makes a copy of these evaluated files and stores these in memory. This gives a huge speed bonus.
Another thing that might be the problem is that if you run this ZF2 application with apache on windows it is much slower then apache on linux
It's hard to know whether this is good or bad without any info on the server you're running this test on. You also implied that this was a stock ZF2 skeleton app, but your screenshot shows what I think is the ZF2 developer toolbar, an add-on module.
I just tested a fresh checkout of the skeleton app on my (admittedly decent spec) dev machine and it loads in 30 ms (PHP 5.5). I would expect to be able to improve that with some simple production-type optimizations (classmap, config caching, superluminal etc.).
Edit: I thought I'd see what I could get this down to with some quick tweaks. I also installed the developer toolbar just to be sure this wasn't slowing things down too much. Result:

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.

Categories