Re: improve speed of PHP answers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



1-2s? that sounds insane and (to me at least) entirely unacceptable, though
not something that i haven't seen before when i was speeding up our
wordpress...

>From some significant experience of speeding up things, including php
applications, frameworks, various other applications, servers, platforms,
code, cars, bikes, etc, often times php is not the issue, though people tend
to want to believe it, there are many places where typical frameworks can be
speed up, now while most of what the others have already said is true, here
are some more ideas and concepts.

One, while sorting and limiting, and improving mysql queries is a great way
to increase application performance, dont forget that sql is not a
programming language and as such certain tasks should not be done in
queries, any complex computations for example would execute faster in php
then they would in mysql (tested).

Two, optimising php code. If you can, make sure you optimise your code, bad
code takes a lot longer to run. Make sure when writing code you know
strengths and weaknesses of the language, using recursion for example, while
a lot of times provides a very elegant solution programmatically, in php, it
runs a lot slower then using in-line loops, as such, for performance, one
should avoid them (unless you are planning to use a php compiler, such as
hiphop-php)

Three, cache, it costs a little ram, but the results speak for themselves,
opcode caching for php speeds up consequent executions of your code, data
caching speeds up getting data. It is often faster to make two dozen calls
to cache then it is to make a single call to a database (not always, but
very, very often). Browser-side caching makes files that dont change on your
computer, not have to be re-downloaded every request.

Four, look at your http requests when you are loading the page, especially
themed, look at the dozen+ of requests that are made to the server. Each
takes time to satisfy, think of a way to speed that up...

Lastly, os, you want to optimize your OS, you want to choose an os with a
good network stack, example, on the same hardware, windows xp (and server
2k3) will top out your transfers at 9 Gb/s while Linux at 12, fact, that i
have personally tested (in lab conditions and in real life). You want to use
something that is not pre-bloated for you, a slim, minimal install of linux
works wonders. Next, apache, if you can, use multi-threaded apache (apache
mpm), threading model is many times superior in performance to the regular,
forking apache. Compiling your php on your platform will gain you a little
performance, but, contrary to popular belief, not a ton, however compiling
your own kernel can give you quite a bit more performance. While your
distribution fails to catch up to what's happening in Linux, running the
latest kernel with Mike Galbraith's patch will increase performance at high
loads on your server, and quite significantly too, look at the scheduler
patch, its brilliant...

So to explain that above is not just words, i will take you through my
latest adventure of wordpress on an EC2 instance.

My goal was to increase performance of a wordpress site running on an ec2
instance (2 cores, 4 gb ram); centos (basic install no gui, mysql), apache,
php (mysql not on the same server), custom theme, custom plugins...

I didnt want to modify wordpress, i glimpsed through the plugins changing
some obvious slow-downs, but that gave me little to no performance. One more
thing i didnt want to do was make this box unmanageable, by custom compiling
stuff, so custom kernel was out, as was apache mpm :(

So what happened:
Before anything i came to a realisation that i could use memory, but cpu was
a much-needed resource that i needed to minimise the use of...

First, installed xcache, php opcode caching, this speed up the scripts
execution, but did little for overall performance
Installed memcached, and caching extension for wordpress
Thought for a while, and i wasnt satisfied with page load times,
particularly sending the same files over and over again
Installed nginx, to act as a front-facing server, with apache/php on proxy
through. nginx is a lot lighter, threaded and a lot faster at serving static
content: javacript, css, images
Setup a php script to serve files with caching back-end, i.e. nginx -> php
-> caching ?-> disk
Setup nginx to rewrite the caching portion of headers for these files so
after initial transfer, they wouldn't need to be downloaded any more
Looking at the requests, a lot of css/js files were being requested by
wordpress, found minify, which combines js/css files and serves them as one.
Minify is pretty awesome, it was a significant part of decreasing page-load
speeds by cutting requests down by more than 1/2.

So, the results of the above (which could be improved on, but are a good
starting point) note the hardware has NOT changed

stack was modified from apache -> php -> mysql to nginx ?-> (apache -> php
-> xcache -> memcache ?-> fs) -> apache -> php -> xcache -> memcache ?->
mysql
performance went from 12 requests/sec @ 100% cpu utilisation to 1600
requests/sec @ ~2% cpu utilisation
page load times went from 1.2-1.5s to 0.125 - 0.3s
interestingly memory utilisation went from hogging over 800meg at full load,
to about 64 meg pretty constant, but invariant of the load.

The same content is served, the page looks exactly the same, nobody had to
really change any code (there was a change in one of our framework modules
that one of the plug-ins calls, but it was small enough (5 lines)), minus
the wait... a lot of it :)




Hopefully this gives you ideas that work out, if not, well, I tried anyway,


~Alex

This work as well as my thinking in general, is licensed under a Creative
Commons Attribution-Noncommercial 3.0 License.


On Thu, Feb 10, 2011 at 1:49 AM, Ashley Sheridan
<ash@xxxxxxxxxxxxxxxxxxxx>wrote:

> On Wed, 2011-02-09 at 18:57 +0100, Alain Roger wrote:
>
> > it's a php component for joomla so it is written in PHP.
> > it access to DB as also the whole site root structure to backup database
> > structure , data as also web site folders and files.
> > due to slow freeing ressources (php pointers, memory, andso on...) the
> > backup process fails each time...
> >
> > A.
> >
> > On Wed, Feb 9, 2011 at 6:47 PM, Alexis Antonakis <admin@xxxxxxxxxxxxxxx
> >wrote:
> >
> > > But what is the webpage trying to do?
> > > Is it straight html? Are you accessing data from a database? What
> exactly?
> > >
> > >
> > > On 09/02/11 10:45, Alain Roger wrote:
> > >
> > >> yes i understand however even Linux has bugs and issues...
> > >> i'm currently installing Fedora 14 as web server and joomla has big
> issues
> > >> on fedora 14 with right permissions and writable configuration.php
> file...
> > >> everybody raises issue about that and till now none of their solution
> > >> worked... :-(
> > >>
> > >> basically web page needs between 1 to 2 s to load, but as it is a
> testing
> > >> computer it is not a huge problem... main problem is that component
> for
> > >> joomla as akeeba backup fails to backup web site while under Windows
> XP it
> > >> worked perfectly...
> > >>
> > >> A.
> > >>
> > >> On Wed, Feb 9, 2011 at 6:41 PM, Alexis Antonakis<
> admin@xxxxxxxxxxxxxxx
> > >> >wrote:
> > >>
> > >>  The Operating System :)
> > >>>
> > >>> It could be a million and one things...how long does it currently
> take to
> > >>> load a page and what is that page trying to do?
> > >>>
> > >>> Alexis
> > >>>
> > >>>
> > >>>
> > >>>
> > >>> On 09/02/11 10:36, Alain Roger wrote:
> > >>>
> > >>>  Hi,
> > >>>>
> > >>>> i have apache/PHP server installed on Windows 7.
> > >>>> my computer is a quad-core CPU with 6 GB RAM and i would like to
> speed
> > >>>> up
> > >>>> PHP answer to requests.
> > >>>> how can i do that ?
> > >>>> which parameter should i tune ?
> > >>>> thx.
> > >>>>
> > >>>>
> > >>>>
> > >>
> > >>
> >
> >
>
> Surely there are already plenty of backup modules for Joomla?
>
> If you're writing it as a module for that CMS, don't forget that you're
> inheriting the whole memory footprint of Joomla at the same time. I
> recently ran into such a problem on a project written on the CodeIgniter
> framework. Now CodeIgniter isn't that heavy on resources, but even it
> struggled with the default memory available to it.
>
> If you're trying to speed up any PHP script, generally you need to step
> through it and see what code you can remove entirely, or alter to have
> less of an impact. For example, PHP code that grabs a set of results
> from the DB and then filters that down is far slower and uses more
> memory than if you left the filtering in MySQL.
>
> Also, look at freeing up resources when you don't need them. Avoid large
> global variables (if they are global the garbage collection can't work
> until the script has ended).
>
> Perhaps split the task into several smaller ones that are more
> manageable at once for the server.
>
> Finally, you can increase the memory that can be allocated to a PHP
> script, but address this last, as the other factors are more important
> in my opinion.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux