Identify Scripts That are Slow on Your Server

When working with PHP, you may notice that your site is taking longer to load than expected. It would be useful to learn the reason why. You can do an strace but sometimes that information is not the easiest to read.

If you are a PHP programmer, you must have used PHP’s error_log function at some point. But PHP itself does not provide a way to identify slow scripts.

Slow scripts don't necessarily break your site, but they can be slow by themselves and they can slow down your server overall as they increase CPU overhead. With PHP-FPM, there is a slow_log function, by which developers can get a trace of functions whose runtime exceeds a specified threshold.

LiteSpeed uses the faster LiteSpeed API (LSPHP) instead of PHP-FPM. So, what's the best way to identify slow scripts with LSWS and LSPHP?

There is a directive which can be placed in the external application settings. Navigate to WebAdmin console > Configuration > External App and add the following to Environment:

LSAPI_SLOW_REQ_MSECS=10000

If LSAPI_SLOW_REQ_MSECS is set to a non-zero number, LiteSpeed Web Server will log requests into an error log file if a request takes longer than the specified number of milliseconds. This can help to identify scripts that are slowing down your server.

The output of the slow script trace is written to stderr.log in the LiteSpeed log directory. An example message would look like this:

 [12/Dec/2017:15:37:41] Slow PHP script: 10993 ms
   URL: GET /wordpress/index.php
   Query String:
   Script: /home/user/olsws/Example/html/wordpress/index.php

There may be many of these messages. Note that script slow-downs tend to appear in groups around the time of highest CPU load. When looking to identify what is slowing down a script, a good place to start is with the script's author.