PDA

View Full Version : PHP Really Slow Response Times



matthewjumps
August 19th, 2008, 10:54 AM
I'm using PHP+MySQL to build a CMS. Its going pretty well. I have one problem...

The CMS uses mod_rewrite to change www.site.com/section/page (http://www.site.com/section/page) into GET vars. Sometimes, I have a folder like www.site.com/xml (http://www.site.com/xml) - this contains an index.php, and then different pages get included depending on the URL, eg

www.site.com/xml/newsfeed (http://www.site.com/xml/newsfeed)

includes the newsfeed.php file in the xml folder. This is fine. But I have a flash file that simultaneously requests 4 files from the xml folder, which basically calls index.php 4 times with different included pages, all using the CMS to generate the content.

I seem to get really bad performance sometimes. Sometimes it will load straight away, but then it seems if i reload the page it will take 30+ seconds sometimes. Its weird, in firebug the net monitoring shows the requests, it usually loads a couple of the xml/filename files, but i dont see the other 2 even tho the first couple are done...

Also, i put some speed tester code in the php's and they all say they take something like 0.02 seconds to generate...it just seems to take ages to get back to the browser or something.

Each call to xml/file opens the index.php in /xml, which then looks in the cache folder to see if xml/file has been cached yet, if so it echos its contents, else it connects to the database, generates the content, creates the cached file, then echos it out

Any ideas why the bottlenecks are occuring?

The site is http://mamajazz.com.au/

simplistik
August 19th, 2008, 12:37 PM
sounds like a server problem, have you tried it on another server?

matthewjumps
August 19th, 2008, 12:59 PM
No I havent, I currently dont have access to one...but, I'm using MediaTemple, never had problems before...

So you dont think its to do with the simultaneous requests to the same index.php with multiple simultaneous connects to the db or something???

It is weird that the speed tests report really low generation times though...

simplistik
August 19th, 2008, 01:25 PM
It's not the simultaneous connections, at least it shouldn't be. I see the lag. And when I hit your xml files directly one at a time they lag as well, IF they even load which they only did about 10% of the time. So it seems as if there is something the matter either your mod_rewrite rules or your xml documents. I know it's not the xml directory cause when I call the gallery script it calls just fine.

matthewjumps
August 19th, 2008, 01:28 PM
AHA! .htaccess!!!!

Forgot the [L] at the end of the last rule! Uploaded now, trying it out...hope thats it

simplistik
August 19th, 2008, 01:34 PM
Still slow, what's your rule look like? Also, maybe try [L,QSA]

matthewjumps
August 19th, 2008, 01:36 PM
odd it seems to be fixed for me??? Ill restart my browser and try again, and try IE too...

The rule is



RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&page=$3 [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&page=$4 [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&section4=$4&page=$5 [L]

matthewjumps
August 19th, 2008, 01:41 PM
It really seems fine in both IE and firefox for me. I emptied my cache for both, loaded the site, reloaded, shift-reloaded ... all worked perfectly...

Btw to browse to individual xml files you should use

xml/newsfeed not xml/newsfeed.php

simplistik
August 19th, 2008, 01:42 PM
I still get big lag issues, it works fine when you close the browser out and then open it up, but after a few requests/page refreshes it bombs

simplistik
August 19th, 2008, 01:43 PM
Btw to browse to individual xml files you should use

xml/newsfeed not xml/newsfeed.php
right, that's what was being used, cause that's what shows up in the activity/net monitors

matthewjumps
August 19th, 2008, 01:44 PM
Damn, I made an edit and it seems to be slow again. Poo.

It seems to have something to do with me logging in to admin mode, or editing something...

matthewjumps
August 19th, 2008, 01:45 PM
Oh ok maybe its not the edits, maybe just the refreshes....

what does [L,QSA] mean?

simplistik
August 19th, 2008, 01:45 PM
My one other suggest is to put the "last" [L] flag at the end of each of your rules, it should optimize the processing instead of trying to process all those rules at the same time. [QSA] appends the query string to the new url instead of replacing it you don't need it tho after seeing your rules

matthewjumps
August 19th, 2008, 01:47 PM
The fact that it dies after a few refreshes - if its not mod_rewrite, what else could it be?

could it be some unclosed connection to a database or some unclosed link to a dir/file - something like that???

Oh and do you mean i should do


RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&page=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&page=$4 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&section4=$4&page=$5 [L]

?

matthewjumps
August 19th, 2008, 01:58 PM
Oh and simplistik - thanks for the help so far, much appreciated :)

simplistik
August 19th, 2008, 03:11 PM
The fact that it dies after a few refreshes - if its not mod_rewrite, what else could it be?

could it be some unclosed connection to a database or some unclosed link to a dir/file - something like that???

Oh and do you mean i should do


RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&page=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&page=$4 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&section2=$2&section3=$3&section4=$4&page=$5 [L]

?

yea that's what i mean, the way the [L] property works is it'll stop once a rule is met so basically you have rules 1, 2, 3, 4 if you use the [L] property if rule 1 isn't met for your specific call it moves to rule 2, if rule 2 meets your conditions it'll stop there, but if you don't use the [L] even after rule 2 is met, it'll still go on further to process rules 3 and 4 when it doesn't need to.

matthewjumps
August 19th, 2008, 03:18 PM
ah i see. will i need to reorder the rules so the last is checked for first?

matthewjumps
August 19th, 2008, 03:23 PM
Well, i changed it to [L] , no reordering, and ive been refreshing a lot...and it seems better. now to log in and edit something...

edit: wait, no, its doing it still...it just took a fair few refreshes...

most of the time the offending xml/file load time is around 30 seconds...wha's happening :(

and it's like, once it messes up, if i refresh the page, it keeps happening. almost as if something is stuck, perhaps in a loop, on the server, and it has to wait till the loop is killed...or something?

simplistik
August 19th, 2008, 03:30 PM
Well, technically if your rewrite rules are literally/exaclty what you've shown above, I don't think that apache processes them any differently.

matthewjumps
August 19th, 2008, 03:31 PM
OK - so i guess thats not the problem then? is there a way i can check for a loop in my rewrite rules?

matthewjumps
August 19th, 2008, 11:49 PM
matthewBumps ;)

matthewjumps
August 20th, 2008, 04:37 AM
Hmmm someone mentioned it could be to do with PHP SESSIONS...

As long as a php session is opened, no other scripts are allowed to open the same session (to avoid threading issues with the session data), and PHP will block on any call, even an implicit one, to open that session. Using sessions can cause slow loading behavior if you're including lots of php-served files into the same parent page. In some, rare, cases with web services calling eachother I've even seen php grind to a halt completely until the request timed out.
This is what im doing - using sessions for lots of php created pages using the same parent php page...

Any ideas how to get around this? I need the session open on each one so i can write to it throughout the script execution, so what i was doing was

session_start();
function sessionCloseFunc(){
session_write_close();
}
register_shutdown_function(sessionCloseFunc);
Is there a way to have 'shared' sessions for a parent page? or something like that???