Find Out Where Your PHP Code Is Slowing Down (Performance Issue)

Find out where your PHP code is slowing down (Performance Issue)

I've used XDebug profiling recently in a similiar situation. It outputs a full profile report that can be read with many common profiling apps ( Can't give you a list though, I just used the one that came with slackware ).

Check the cause of slow loading time among different server

Refer to the comments - I misinterpreted the data I was looking at earlier. It appears the server can only handle maybe 5-10 requests at a time so things get blocked until the other things are done loading. You likely just need to update your web server's configuration to handle more requests.

There is also a lot of JS data in the file. It is 318KB just to load the page and it has to do many requests to get JS/CSS files before it even gets to any of the HTML. So it is 318KB + all the external JS/CSS it needs to fetch (wow!). That's like 4MB of stuff just to load a page.

Check the modify timestamp on the files generated by your caching system to verify that caching is working.

Edit:

Since there is now a bounty out - please review the comment discussion we had. There is an issue where a traceroute doesn't make it to the server destination and I suspect that is related to the slowness but that type of network issue is over my head.

Why is php script slowing down?

I found out that php >=5.3 has garbage collector cleaner. So, when I import chunk of articles I call gc_collect_cycles(); to clear memory out of all entities which script will needs no more. The script is slowing down no more!

If you are using a framework, check if it has its own cache system.
If you are using doctrine turn off SQL logger

/** @var $em EntityManager */
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$em->getConnection()->getConfiguration()->setSQLLogger(null);

and then clear doctrine cache after every chunk is imported

$em->clear();

PHP does lots of type checks slow down your code?

You can find out yourself.

Store the current time in milliseconds at the start of your script, run a loop which does a type check several thousand times, and compare the time at the end of your script with the time at the start of your script.

Very slow execution of php scripts on the server

Finally found what was causing the issue: it was one of the extensions, namely, the browsecap extension, or more likely, the lack of it.

At some point, it was installed, but when we realized, we wouldn't use it, for different kind of reasons, we uninstalled it, yet it wasn't removed from the php.ini file. By removing a single line related to browsecap from the ini file, the problem solved completly.

I would like to thank @Pampy for pointing me in the right direction, with the extensions. If anyone experiences something simmilar in the future, an option to debug if the problem is simmilar, is to run php from the command line, with the -n parameter, which actually disables it's ini, and all the loaded extensions at once.

How to find out what is causing a slow down of the application?

The problem is almost certainly MySQL based. If you look at the final graph mysql/mysql_threads you can see the number of threads hits 200 (which I assume is your setting for max_connections) at 20:00. Once the max_connections has been hit things do tend to take a while to recover.

Using mtop to monitor MySQL just before the hour will really help you figure out what is going on but if you cannot install this you could just using SHOW PROCESSLIST;. You will need to establish your connection to mysql before the problem hits. You will probably see lots of processes queued with only 1 process currently executing. This will be the most likely culprit.

Having identified the query causing the problems you can attack your code. Without understanding how your application is actually working my best guess would be that using an explicit transaction around the problem query(ies) will probably solve the problem.

Good luck!

PHP is very slow when printing a large amount of information

The problem is unlikely PHP, it's the massive amount of HTML being output to the browser. You could verify this yourself by saving the page that you get, then loading the static file from your hard drive.

If you are outputting a large table, then the browser will often have problems with "redrawing" it as content is loaded. This can be minimized by setting a fixed width for all the columns. Just do this on the first row.

You can also jump out of the PHP block to output most of the HTML, then just print the variables where appropriate. Here's an example, assuming all your data is in the $rows variable:

<?php
// some code here
?>

<table>
<thead>
<tr>
<th width="50">Header 1</th>
<th width="200">Header 2</th>
<th width="150">Header 3</th>
<th width="100">Header 4</th>
</tr>
</thead>
<tbody>
<?php foreach ( $rows as $r ) : ?>
<tr>
<td><?=$r['var1']?><td>
<td><?=$r['var2']?><td>
<td><?=$r['var3']?><td>
<td><?=$r['var4']?><td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<?php
// continue here if necessary

Note: if you don't have PHP short tags enabled you'll need to do <?php echo $r['var1'] ?> instead.

Finally, you can try adding gzip compression. It's best done at a server level but in PHP you can do add the line ob_start("ob_gzhandler"); as the very first line of PHP code right after <?php.

If this still does not help, then it's a simple fact that your table is way too big. You'd be much better off using pagination or filtering to keep the size down.



Related Topics



Leave a reply



Submit