PHP Performance Hack – Throttling During Load Issues

PHP Performance Hack – Throttling During Load Issues

I”ve had some performance issues lately with the server buckling under the pressure of a high peak user load . Other times, I”ve had issues with process intensive tasks that kept recursively spawning more instances of themelves.

Creating performance intensive scripts require a good amount of design & architecture forethought, and sometimes you can”t always foresee the ways things could go wrong.

I ended up adding a method to my PHP codebase, is_server_overloaded() , to partially alleviate the problem.

Source Below: (note: only works on linux boxes)

/****************************************/
// - Input vars:
// - Preconditions:
// - Postconditions:
// - Returns: server load as a percentage
// - Comments:
// -
// -
function getServerLoad() {
    if(file_exists("/proc/loadavg"))
      {
           $Load = file_get_contents("/proc/loadavg");
           $Load = explode('' '', $Load);
           return $Load[0];
      }
    elseif(function_exists("shell_exec"))
      {
           $Load = explode('' '', `uptime`);
           return $Load[count($Load)-1];
      }
    else {
           return "";
    }
}
/****************************************/
// - Input vars:
// - Preconditions:
// - Postconditions:
// - Returns: bool - true if server is overloaded
// - Comments:
// -
// -
function is_server_overloaded(){
  return getServerLoad() > 10;
  }

 

If your code executes a lot of performance intensive tasks, and you”re starting to hit a lot of scale, you may want to consider using this code to establish an upper limit to the end users experienced load time.

I use this is_server_overloaded() method to establish an upper limit to the level I will push my server, and have a few performance intensive areas of my PHP/MySQL code that are designed to perform differently when a load issue appears (ie. when is_server_overloaded() == true).


Uncategorized
Comments are closed.