what is best way to improve performance of zend framework? - php

zend framework has many components/services I don't need, it has many includes.
All this I think slow down application.
Do you know how to speed up it? may be remove not used(what is common) components, or combine files to one file?

APC or eAccelerator (APC will be included by default in future releases, so I'd recommend using it, even though raw speed is slightly below of eAccelerator)
Two level cache for configuration, full-page, partial views, queries, model objects:
1st level cache (in memory KV-store, APC store or memcached)
2nd level cache (persistent KV-store in files, SQLite or dedicated KV-store)
RDBMS connection pooling, if avaliable.

Before you start worrying about actively modifying things for more performance, you'll want to check the Performance Guide from the manual. One of the simplest steps you can do is to enable an opcode cache (such as APC) on your server - an Opcode cache alone can give you a 3-4x boost.

Code on disk that isn't being called, doesn't take any time. The only way to see what is slow is to measure it. That said, if you aren't running an opcode-cache such as APC, then you are wasting time.

I agree with Topbit, that you should start with code profiling.
Find what is the problem.
I don't think that the problem is just because of ZF has so many files. It uses autoloading, so only files required at the moment are loaded. You definitely shouldn't split different files contents.
For many perfomance problems, caching is your friend.

you can get a bit of extra speed by optimizing the requirements statements
as stated in the optimizing help topic ... first remove all the requirements
and i also recommend using pear naming and overwriting the autoloader,
function __autoload($class) {
require str_replace('_', '/', $class) . '.php';
}
you can find more details here

Are you being forced to use the Zend Framework? If there is no obligation to use it, then not using it would obviously be the fastest way to speed things up. There are several lightweight PHP frameworks that don't come with all the overhead and bulk of Zend. For instance, Codeigniter, Yii, Symfony, and Kohana are all excellent choices and I know at least that codenigniter and Kohana both support the use of Zend components (for instance:Using Zend with Codeigniter).
Good luck!

Related

How to use I use Zend_Cache_Backend_TwoLevels with tagging?

I'm thinking of using two level cache backend, in a Zend Framework application.
Fast: APC
Slow: File
But I need it to use cache tagging, to make an easy cache clearing.
So is it possible? to use those combinations?
PS. I'm asking this question because I've read:
Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true" argument.
In the official Zend Framework document: Zend Cache APC Backend, so I was wondering how to get use of tags, since it's the most interesting part in caching IMO.
I don't think this is possible. You marked Mikushi's answer correct, however from this page:
http://framework.zend.com/manual/en/zend.cache.backends.html#zend.cache.backends.memcached
Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true" argument.
Well, the documentation is pretty clear on that, APC does not support tagging. If you want to take advantage of the tagging and the two level caching, your might want to use Memcache as fast, then you'll have tagging available.
You could still hack your way in adding support for tagging to the APC BackEnd of Zend, by extending it, but i would not recommend it.
As a general advice, using APC as a main caching system is not a good solution. APC is mainly an OPcode cache system, even with the apc_store, whereas Memcache is a distributed caching system, which is a lot better, and safer.
To give you a "real life" example, at my job, we use APC as a cache only to store parsed configuration files for our websites/application. Everything else is either Redis or Memcache.

Shared vs dedicated php library

I have a server which consists of several Zend Framework application.
I want to know if it is a good idea to upload Zend Library on the server and share it among all the applications instead of uploading it per application.
Does it influence speed if for example multiple applications request a library simultaneously or not.
What is its Pros and Cons?
Thx in advance.
My answer applies in general to shared libraries, as this should not be specific to Zend Library:
PROS of sharing:
Less disk space usage
Possibly less memory usage (depends on many factors, including OS)
Update once, update all (you do not have to update the library for every single app
CONS of sharing:
If you need a particular version of the library for a certain application (for compatibility reasons for example), you cannot do it by sharing the library
Risk of breaking apps by updating to an incompatible library version.
As others have pointed out, there are pros and cons. The big con is that every time you're going to upgrade your library code, you need to test every application, not just the one that needs the upgrade right now.
The big pro is that if you're using an opcode cache like APC (and you should be), you're wasting a fair bit of memory loading identical chunks of library code. Depending on the size of your opcode cache, and how much of the library code actually ever gets run, this could become an issue at some point. If the size of your opcode cache is not big enough to hold everything, you'll end up with a performance hit of some magnitude.
A middle-ground solution is to keep all your libraries some place on the server where they can be shared. Build your apps to use some configuration value for loading.
APP1: config.php
<?PHP
define('ZEND_LIB_PATH','/path/to/ZendFramework-1.9/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());
APP2: config.php
define('ZEND_LIB_PATH','/path/to/ZendFramework-1.10.1/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());
That way if two apps happen to be using the same version, they can share opcode caches for that version, but you're not tied to it.
DISCLAIMER: I haven't actually done this, so you probably want to test the theory before putting it into practice.
If the applications are separate, I would give each one its own library to avoid issues when you need to upgrade a library on one of the apps, but don't want to have to test and update them all with that library version.
Then again, if those applications share code, they should definitely share the libraries too, to avoid even worse problems with the shared code running under different library version on each site.

Using Zend Framework for highload projects

Zend Framework is a good framework but not very fast.
Can you tell whether it's worth using Zend Framework for highload projects, for example, for email marketing service that can inlude about ten or houndred thousand of users?
Is it possible to achive acceptable performance using Zend Framework?
Has anybody such an expirience?
Thank you very much.
For what I have seen, the definitive defense of Zend Framework performance and recommendations for performance optimization comes from Padraic Brady at:
PHP Framework Benchmarks: Entertaining But Ultimately Useless
In particular, note his four recommendations for performance optimization:
Don't use Zend_Application. While Zend_App is great for creating consistent complex bootstraps within a standardised structure, it doesn't come without a significant performance hit to baseline performance. A more direct bootstrap (typical of ZF until Zend_App arrived) is far faster and can also be done without configuration files.
Skip using the ViewRenderer plugin. Without the ViewRenderer, you need to manually configure Zend_View and add render() calls to Controllers. This is actually very simple to do and is fairly fast - fast was never really part of the ViewRenderer's genetics.
Use autoloading. Strip require_once calls from the framework library so unneeded files are ignored. Replace uses of Zend_Loader_Autoloader with a not-so-crazy autoloader function. In fact, pray Zend_Loader is never used - it does a lot of file ops that, to date, have never been explained to me as having any value.
Preload everything (Symfony 2 Preview does!). It buys you some performance cookies and equalises the speed baseline. Using a simple preload script is not that hard.
We've used ZF in a lot of high traffic sites, and we've had no issues so far. We did have to jump through a few low-hanging hoops, though.
Some suggestions:
use Zend_Queue to help with batch mailing
use Zend_Cache whenever possible
Use plugin loader cache
Strip require_once calls in favor of autoloading
Get rid of components you don't want. (as suggested, you would not need MVC stack for CLI / mail)
We chose Sphinx in favor of Zend_Search_Lucene (enormous performance gain)
The bottom line for us has been this: development time is much, much more expensive than hardware. The flexiblity and higher re-use of code completely trumps any minor performance losses we had to deal with. For the most part, the performance overhead was very fixed.
You ask:
Is it possible to achive acceptable performance using Zend Framework? Has anybody such an expirience?
Yes, I have experience with a site with millions of users. But you do need to use techniques to deal with the high load. Caching etc...
A CDN can help a lot. Look into developing with the cloud. Amazon might be a pain to get started with but it helps you scale if need be.
I guess what I'm saying is, the Framework may cost you a bit of performance, but helps make maintenance possible and building it faster (once you get over the learning curve). Then you you have to evaluate what needs to be done to improve performance (although it helps a lot to plan for what will be obvious problems, right from the get go).
I know of several companies that use ZF in high-performance/high-load scenarios. I don't know which ones I can state and which ones I can't, but some of them are media companies who have to handle popular TV shows. Others handle live sporting events. Others are multi-billion dollar companies who need to serve their internal organizations. So, ZF is being used by plenty of companies who run pretty high-load sites. One of our case studies is Fox Interactive (http://framework.zend.com/about/casestudies) and I know of several other customers who use it for high-performance websites.
Zend Framework MVC, out of the box, will be quite fast. My blog comes back in about 100ms without caching and there's a fair amount of stuff that happens on my front page. I could probably drop that down to 50ms with some internal caching (Full page caching could drop it down to single digit ms, but then it's not touching ZF).
Seconding Joe's answer. I've also seen ZF deployed on a few sites handling millions of requests and have yet to encounter a problem. When dealing with that amount of traffic it's a good idea to use other strategies beyond your framework, including but not limited to caching and the use of a CDN.
I've found most frameworks will call or create many class instances per request which I think is what causes people to say that framework X is slow without having any real world experience with it. Any hit you take there can be easily mitigated by using an accelerator and caching.
If you already have a team of devs you've hired, I'd suggest using what they feel most comfortable with and have the most experience with. Best case they'll be able to tune their code for that framework.
A lot of the framework, any framework really, is used for building and managing the project development but the resulting project is 'just' php, html, css etc. the same as any other php web site. So what evidence do you have, that's real timing against other framework and non-framework built sites not anecdotal evidence, that a Zend project site is slow.
Edit -- answers to below --
I don't think the structure that the framework uses will hurt performance. It may be more a question of PHP being acceptable and then how much 'overhead' is added with the site design and the optimisation of loading say JavaScript's etc. I would imagine that using the Yui guidelines of minifying JavaScript and CSS and loading them in the correct order and making sure the PHP code is efficient will help. You can also use other standard things such as DB Caching and Zend Accelerator will speed things up. One thing to be careful of would be the DB connection. The use of an ORM layer might have an impact.
However back to the original question about the framework i think it is similar to asking if using Eclipse or Textmate has an effect on the speed of the resulting site.

High Performance PHP

I am about to launch a CodeIgniter web application. Any recommendations on what I should do to maximize performance of my application? I'm thinking memcached and an accelerator (though I am not sure which one). Suggestions?
To maximize performance, minimizing database requests you must. Denormalize tables before you hack on memory caches. And more importantly run your application through xdebug first, to check out the cachegrind output.
memcached is great, if you need more datatypes Redis is a great alternative too. An Opcode cache like APC could help. PHP 5.3.3's new SAPI (FPM) could help a lot if you don't want to go with the traditional mod_php way.
You can use an accelerator, like APC. Any sort of caching will help if its applied in the right area's.
I would suggest you profile your code runtime using Xdebug and Webgrind in order to find out where its running slowly, and go from there with any optimisations.
You can also do things like move things in your htaccess to the server config and disable htaccess, so that the server isn't wasting time looking in every directory for them.
Performance is a very broad term. As caching is not the only issue. You need to make sure that you have a good database design and structure and that you are querying it in an appropriate and efficient manner.
Also you might want to consider profiling, before you go on to caching, to see if it is indeed faster then regeneration.
Edit:
Have a look at the following benchmarks from AventLabs
you can use both is the simplest answer.

How can I determine CodeIgniter speed?

I am thinking of using a PHP framework called CodeIgniter.
One of the things I am interested in is its speed. I have, however, no way to find out how fast it is, and would rather not simply take the word of their website for it. Does anybody know how I can determine its speed myself, or can someone tell me of a site that can?
Code Igniter also has some built-in benchmarking tools:
http://codeigniter.com/user_guide/general/profiling.html
Yes, the problem is you have to build your application to profile it.
At work we had a couple of projects written outside which we load-tested before putting them on our main boxes. We were quite surprised to find critical performance problems with both; one was written in CakePHP and the other was written using Drupal. I don't think this highlights a problem with any framework or CMS other than the need to do profiling and load-testing on any site which is going to get significant traffic. In both cases it was what the developer had done, rather than the characteristics of the software platform, that caused the problem. For example, there was a recursive function call the developer had created in the Cake project which instantiated the entire Cake object every recursion and this would have taken out the server had it gone live under load.
In my opinion performance should not be a deciding factor in choosing a framework; the objective differences are likely to be marginal and the way you use it is likely to cause far more performance problems than the inherent performance of the framework.
I believe that to scale any PHP application to run under load, you will need an opcode cache and you'll need to write in intelligent content caching using something like memcached or whatever built-in caching your framework supports.
If your site is database-driven I would be very surprised if your bottleneck would be the application framework. "Fast" as in faster development is what I would worry about rather than "fast" as in speedy handling of requests. Significant optimization is better done by caching strategies and optimizing your database access.
Besides database access your own code will be where most of the time for each request is spent (and even that is usually not significant compared to database access), the framework will likely not be affecting the time spent on a request, unless it is really badly written.
It way be better to look for a framework which has good caching support (which Code Igniter may have, I don't know), that will almost always save you more time than the few milliseconds you could shave off the request handling by using a slightly faster framework.
Have a look at the Zend Framework too, it has the benefit of being PHP 5, whereas Code Igniter is still PHP 4, as I understand it. That may be an issue when it comes to speed, but in favor of which framework I don't know. Zend has good caching support and a database profiler that can help you find where your bottlenecks are.
i'd recommend testing it for yourself. use xdebug's profiler to create a cachegrind compatible file and webgrind to visualize the file.
that way you end up with very reliable information.
CodeIgniter is plenty fast for most projects. Some have posted here and if you Google, you will find that it compares favorably to other frameworks with respect to speed.
I would agree with another poster that performance is usually not a big concern when it comes to framework choice. The major frameworks all have sufficient performance for most projects.
I maintain a site that gets slammed a few times a year. Last year the development team rewrote the entire site using Codeigniter and we have had much luck in terms of performance. Additionally, the time it took to perform the rewrite was minimal as this framework is quite easy to work with. CakePHP in my opinion is also a good choice if you find that you don't like Codeigniter.
For CodeIgniter and other PHP frameworks, PHP Quick Profiler is very handy for benchmarking and measuring speed especially for database queries. You must check this out:
php-quick-profiler
It's very easy to install and provides an awesome GUI for examine different benchmarking tests.

Categories