Long Time to First Byte value for an empty php file
Asked Answered
M

4

18

I had such question some months ago. Now to isolate the problem I tried a new approach. I put an empty file to my server.

File name is "foobar.php". Its content is as follows:

<?php
echo "hello world";

But when I try to enter to example.com/foobar.php, I get

DNS 203 mseconds
Connect 3.33 seconds
Send 0 miliseconds
Time to First Byte 17.35 seconds
Receive 1 miliseconds Total load time 20.88 seconds

Then I put another file called "foobar.txt". Its content is as follows:

hello world<br/>

Loading time of foobar.txt is approximeately 0.2 seconds.

This website is inside a shared hosting so I can't get a root Linux access. I'm trying to find out what makes my site slow.

  • When I get these results I have 60 visitors in my site. And they send AJAX requests when they are active. When they are active they send an AJAX request nearly every 3 seconds.
  • Generally my website has 5-20 requests per second.
  • My hosting provider says that there occurs no CPU overload, it is very low generally.
  • I asked hosting company for Apache limits. I get this values for the whole shared server:

MaxClients 300
MaxRequestsPerChild 4000
ThreadsPerChild 25

  • example.com/mybigpage.php and example.com/foobar.php pages are opened nearly at the same time.
  • If page has txt, jpeg or other extensions they are opened instantly. If extension is php it opens very slow.
  • CakePHP stores session files inside "/httpdocs/app/tmp/sessions" folder. Session files are deleted after two hours of creation. Now there is 3653 files inside that folder. Oldest file is created 2,5 hours ago.
  • In my configuration, PHP handler is Apache module mod_php

New Edit: I talked with my hosting company. And told them that "foobar.php" is opened nearly in 20 seconds. Although that file has no code at all. They told me that they put "foobar.php" to other websites that we use same server. I also tried "othersite.com/foobar.php". It opened instantly. But "mysite.com/foobar.php" opened nearly in 15 seconds. What would make this behaviour? We use same PHP configuration with other sites, but they open instantly.. Can it be because of my .htaccess rules? Or other thing?

New Edit2: My provider told me that there exists no "apd.so" file inside the server. So it seems like I can't use APD.

What should I look for to find the bottleneck?
What would limit my site?


Additional data: from the phpinfo, I get this:

'./configure' '--prefix=/usr/local/lsws/lsphp5' '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib64' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.dd' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-libexpat-dir=/usr/lib64' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-shmop' '--enable-calendar' '--with-libxml-dir=/usr' '--with-mysql' '--with-mysqli' '--with-gd' '--enable-dom' '--disable-dba' '--without-unixODBC' '--enable-xmlreader' '--enable-xmlwriter' '--with-mcrypt' '--enable-mbstring' '--with-litespeed' '--enable-soap' '--with-xsl' '--with-pdo-mysql' '--with-pdo-sqlite' '--enable-sqlite-utf8' '--with-pspell' '--with-sqlite=shared' '--with-xmlrpc' '--with-mhash' '--enable-pdo' '--with-imap' '--with-imap-ssl' '--without-suhosin' '--with-tidy' '--enable-zip' '--enable-inline-optimization' '--enable-gd-native-ttf' '--enable-bcmath'

Myriammyriameter answered 22/12, 2012 at 12:4 Comment(6)
You might want to look at the xdebug profiler, gives you a nice visual overview of which part takes timeHelpless
@MichelFeldheim Should running xDebug in my local computer would give an opinion? Because my hosting company doesn't permit me to use xDebug.Myriammyriameter
Yes, running xdebug on your local machine is sufficient to determine time consumers.Helpless
put print_r(get_included_files()); in your "empty" php file - if you are not infact loading an empty file - it will tell you which files you've loaded first. if you don't have app/webroot/ as your domain's doc root - you have to consider whatever mod_rewrite rules you've added.Holofernes
@Holofernes I wrote and got this result: Array ( [0] => /var/www/vhosts/example.com/httpdocs/app/webroot/foobar.php )Myriammyriameter
@Myriammyriameter then it's not related to the framework - you have something generally wrong with your apache setup or some other aspect of your domain's setup.Holofernes
S
7

Seems clear to be a PHP problem, since Apache has no problem serving static files. Have you tried installing APD from PECL?

Using a PHP profiler like APD will show you whether the bottleneck is in PHP and, if so, where it is. For example, is the slowness in the framework that you're using? Or perhaps just a rogue extension?

Paraphrasing from the official manual:

With APD, you just add an instruction at the entry point:

<?php
apd_set_pprof_trace();
?>

APD will dump profiling information to apd.dumpdir/pprof_pid.ext.

Then, pprofp will consume your dump files and tell you what methods are chewing up the response time:

bash-2.05b$ pprofp -R /tmp/pprof.22141.0

Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time  = 0.00
Total User Time    = 0.00


Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 preg_replace
14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 str_replace

If none of the delay you see shows up in the profile, it would suggest it's a systemwide PHP configuration problem (perhaps a rogue or misconfigured extension). But I would guess it's something in the framework.

Shetler answered 26/12, 2012 at 1:31 Comment(3)
It seems like "php_apd.so" wasn't loaded in hosting company's configuration. I edited question and added extension list.Myriammyriameter
Seems clear to be a PHP problem - disappointing that this answer has been awarded a bonus starting out with a now-obvious false statement.Holofernes
The problem could be in the Apache relying the response as well... having the same problem now, the php script run for 0.2sec but the response takes 0.65sec. static content retun in 0.02sec.Chemurgy
B
4

You write that at the time when this problem occurs, you have several clients doing AJAX requests every 3 seconds. This makes it likely that all the PHP workers available at your server are blocked by these AJAX requests. Your web server receives your request for /foobar.php and then has to wait until a PHP worker is free to process your request.

So among the possible solutions for your problem are (without you being more specific what the AJAX is needed for, I need to keep this general):

  • have the AJAX requests go to a static file
  • ensure that PHP really closes the connection when the (AJAX) request is finished, for example with a header("Connection: close"); (though this might not be enough, check the comments on the PHP manual about connection handling)
  • reduce the number of AJAX requests made (to a number worked out in cooperation with your provider)

In general you will need your provider's help with solving this. You did not write which method your provider uses for serving PHP, for example with fpm there is a setting called process.max that can limit your web servers ability to process so many PHP files in parallel.

Blunger answered 30/12, 2012 at 14:22 Comment(4)
I already asked this: MaxClients 300, MaxRequestsPerChild 4000, ThreadsPerChild 25.. I would be happy if you can tell me which addtional parameters should I ask to my provider for more information about this problem..Myriammyriameter
-Making AJAX request is obligatory for me. It is very important for my site to make users easily solve their issues. This requests must point to a PHP function. -"You did not write which method your provider uses for serving PHP" What are the options for this? If you can tell me the alternatives then I can ask to my provider. Do you mean this. -I use jQuery for AJAX requests. So I suppose that jQuery closes the connection after getting the AJAX response.Myriammyriameter
Yes, I mean something like that. the MaxClients only applies if mod_php is used. If they use fpm then the setting I mentioned above applies.Blunger
Thank you. Provider told me that PHP handler is Apache module. So what would I ask more? MaxProcessCount or any other more?Myriammyriameter
C
3

Without cooperation of you provider you have no chance to find out what is wrong.

I would suggest it is something like disk io problems of session.

Another intresting point is that your provider use: http://www.litespeedtech.com/php-litespeed-sapi.html Never heard about that bevore.

If your provider would to what mjk advised you, you should simply change your provider. It seams to be that they dont have theyr ther under control.

Chamonix answered 29/12, 2012 at 13:37 Comment(8)
Thank you for reply. I couldn't understand the last sentence. You mean if provider wouldn't give permission?Myriammyriameter
You would need root access to the server to do what user MJK (post above me) told you. But a shard hoster will noz grant that to you. But you can ask then to do that. But if they dont linke to debug your problem and only give you the easy answer that the cpu only idle. I would advice you to change the provider.Chamonix
What would provider to for MJK's solution? What should I tell them to do? regarding to this They should show "apd.so" directory, set "dump directory path" and Restart Apache?Myriammyriameter
That yould be way. But because you pay them for service. I would only place you hello_world.php on the server and tell you provider that it is not acceptable that you have a load time of more them 0.01 secondsChamonix
I'm discussing with provider to use APD. But my doubt is this: Most of my waiting time is because of "Time to First Byte". So would adding apd_set_pprof_trace(); to the first line of my code show the cause of the delay?Myriammyriameter
1.) You provider need to compile this. It seams you provider has no clue what he does. 2.) Your provider need to debug this problem. what i would try: Run you script on cli, to test time. Run a per or other cgi script to test performancen. When per also has this problem it is a problem with apache / Webserver. If not it could be possible to trace the problem with APD. 3.) I still be shure that it is a problem with disk io. Also i woul try to disable session auto start and apache logging for testing. If one of this improves performence, then has you provider to mutch hostings on a single hostChamonix
1-2) My provider doesn't interested in APD. They don't want to recompile. Because they tell me that I am the only customer that has problems with this server. 3)If I disable sessions file writes, I can't test the situation. Maybe apache log disabling can help, but this also dependent to provider.. 4) Is there any thing and variable that can give me information about the disk IO problems. So I can ask the provider that parameter values etc..Myriammyriameter
sorry but if you provider is not interested to help you to find the problems, look for another provider. You provider dont needs to recompily, thy only needs to compile a single module. An first idea is to trace how mutch "uninterrupale seeping task" process they have. unixhelp.ed.ac.uk/CGI/man-cgi?psChamonix
E
2

If the same empty PHP file loads instantly when moved to another site, it can't be due to any frameworks or included files because non have been loaded at that point.

It can either be a configuration issue on the php or Apache side or it may be caused by your rewrite rules. I would recommend trying the following:

1.) If per-site PHP settings are allowed, ask the hosting company to rename your specific php.ini to some name else, copy over php.ini from another site and restart Apache, see if that helps. I had a similar problem on Windows and it was due to file access problems on php.ini so this may help.

2.) Temporarily rename you .htaccess and access the php file again. If the load time decreases you'll have an errornous rewrite condition or other directive. Could you also post the contents of your .htaccess?

Envenom answered 29/12, 2012 at 12:17 Comment(11)
I had some gzip and redirect lines. I nearly emptied my htaccess, tried again. But it didn't help.. I pasted my htaccess files to hereMyriammyriameter
Just for testing purposes, what happens if you temporarily remove your htaccess completely?Envenom
After I delete .htaccess file I started to get "500 Internal Server Error" so I restored file.Myriammyriameter
Removing an htaccess file can't be the reason of an Internal Server Error. Assuming your web root would be something like "/usr/htdocs/", putting a foobar.php into the web root and browsing to www.mysite.com/foobar.php should always work without having any htaccess file in the directory. So what happens if you remove the .htaccess, put a phpinfo.php into your web root with <?php phpinfo(); ?> as content and open it in your browser?Envenom
I can't put any file to my "/usr/htdocs" because CakePHP dictates me to put my root files to "/usr/htdocs/app/webroot". So from my root I redirect user to "webroot" folder to access root files by .htaccess files. Removing the .htaccess inside my root folder will crash all my site. So nobody would visit my site in that situation, so load will be very low and I wouldn't see the difference in time.Myriammyriameter
That's why I wrote assuming because I didn't know the path to your web root ;-) To find out what is really causing the problems, may it be php.ini, Apache configuration or .htaccess you temporarily have to rename the .htaccess after you put the phpinfo.php into your webroot directory, then open www.mysite.com/phpinfo.php and if it opens instantly it's going to be htaccess rules.Envenom
"I can't put any file to my "/usr/htdocs" because CakePHP dictates [...]" - that's nonsense, and indicates you have a development-style install what you should be doing is putting your webroot files in /user/htdocs/ directly more information in the book. this also means that example.com/foobar.php is not pointing directly at your php file - you're relying on mod_rewrite to rewrite the request to example.com/app/webroot/foobar.phpHolofernes
Yes I'm relying on mod_rewrte to redirect user request to "webroot" folder. But because of this is a shared hosting, can I change DocumentRoot value of Apache?Myriammyriameter
So have you even tried to see if the slowdown is caused by htaccess rules? I can understand that you want to use rewrite rules but what's the use of having fancy URLs if all your pages load terribly slow? People tend to not visit pages if pages take 20 seconds to load. About your question about DocumentRoot changes: some providers offer to change the base directory for a given website using CPanel or whatever management interface they offer. Otherwise try following this guide: CakePHP on Shared HostsEnvenom
@Envenom @Holofernes I changed my DocumentRoot to /usr/htdocs/app/webroot. Now I don't use .htaccess of /usr/htdocs. I only use htaccess of /usr/htdocs/app/webroot.Myriammyriameter
Good you where able to change the docroot but did you also test script access times with a temporarily removed htaccess?Envenom

© 2022 - 2024 — McMap. All rights reserved.