1

I was wondering if someone could give a high-level answer about how to track functions which are causing a slow-down.

We have a site with 6 thousand lines of code and at times there's a significant slowdown.

I was wondering what would be the best way to track the source of these occasional slowdowns? Should we attach a time execution tracker on each function or would you recommend something else?

It's a standard LAMP stack setup with PHP 5.2.9 (no frameworks).

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
  • 1
    assuming the code is.. well, not stupid, I tend to suspect database bottlenecks. Make sure you use a INNODB database (MyISAM is Sooo last year!) and make sure the columns used in WHERE clauses have indexes to speed up searches. On a side note, you could use something like Xenu to crawl your site, it may be able to point out certain pages that take longer to serve than normal. http://home.snafu.de/tilman/xenulink.html – Duane Lortie Nov 07 '16 at 19:39
  • thank you for the reply, is there a way to isolate the problem to some bottleneck in the DB? I'm assuming most of the time it's a problem with the database? – Robert Sinclair Nov 13 '16 at 02:13
  • 1
    http://stackoverflow.com/questions/21133/simplest-way-to-profile-a-php-script – Reactgular Nov 13 '16 at 02:33
  • 2
    Personally, I use [Blackfire](https://blackfire.io/). – maiorano84 Nov 13 '16 at 02:48
  • 1
    Does the slowdown occur in test environments - (It's reproducible) or only on 'live' – Steve E. Nov 14 '16 at 05:06
  • 1
    I'm sure there's a way to manually interrupt it, and then display the call stack. That lets you use [*random pausing*](http://stackoverflow.com/a/378024/23771). – Mike Dunlavey Nov 14 '16 at 12:32
  • 1
    I know this is outside of your question's scope but any specific reason you are using php 5.2? What version of MySQL are you using? Also, a tool i have used in the past for tracking slow query/code blocks is NewRelic (https://docs.newrelic.com/docs/agents/php-agent/getting-started/new-relic-php). You get a 14 day trial that can definetly help you identify the culprit. – Mathieu de Lorimier Nov 15 '16 at 12:37
  • We just never bothered updating.. stupid reason I know. It happens in Prod only. – Robert Sinclair Nov 16 '16 at 01:59

1 Answers1

3

The only way to properly track down a why and where a script is slowing down, is by the use of a profiler.
There are a few of these available for PHP. Some of which requires that you install a module on the server, some which uses a PHP-only library, and others again which are stand alone.

My preferred profiler is Zend Studio, mainly because I use it as my IDE. It has the benefit of being both stand-alone, and to be used in the conjunction with server-side modules (or the Zend Server package). Allowing you to profile both locally, and on production systems.

One of the easiest things to look for, however, are SELECT queries inside loops. They are notorious for causing slow-downs, especially when you have a more than a few hundred records in the table being queried.

Another if is you have multiple AJAX calls in rapid succession, and you're using the default PHP session handler (flat files). This can cause the loading time to increase significantly because the IO-operations are locking. This means that it can only handle one request that uses session at a time, even though AJAX is by its very nature asynchronous.
The best way to combat this, is to use/write a custom session handler that utilizes a database to store the sessions. Just make sure you don't saturate the DB connection limit.

First and foremost though: Get yourself a proper profiler. ;)

Community
  • 1
  • 1
ChristianF
  • 2,068
  • 9
  • 14