Page 1 of 1

how can i get my script's load?

Posted: Fri Jun 22, 2007 6:47 am
by phpist
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello  everyone,

how can i get my script's load?  i just post a function to get system general load below. but this is not enough to me. i want to know only my script's load.

// -----------------------------------------------> Function - BEGIN

Code: Select all

function get_server_load()
	{
		$server_load_snapshot = '/proc/loadavg';
		if(@file_exists($server_load_snapshot))
			{
				$load = file_get_contents($server_load_snapshot);
				$load = explode(' ', ltrim($load));
				return $load[0];
			}
		else
			return '?';
	}
// -----------------------------------------------> Function - END


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 22, 2007 6:57 am
by feyd
General Discussion description wrote:Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Posted: Fri Jun 22, 2007 7:15 am
by Styx
Put a time() variable at top and time() variable at bottom, subtract the two, and format.

Posted: Fri Jun 22, 2007 7:31 am
by Mordred
Styx wrote:Put a time() variable at top and time() variable at bottom, subtract the two, and format.
microtime() rather, time() returns seconds

Posted: Fri Jun 22, 2007 7:35 am
by Chris Corbyn
Styx wrote:Put a time() variable at top and time() variable at bottom, subtract the two, and format.
That's for times. The poster wants the CPU load.

How about this:

Code: Select all

function getScriptLoad()
{
  $pid = getmypid();
  $process_list = `ps aux`;
  if (!empty($process_list) && preg_match('/^\w+\s+' . $pid . '\s+(\d+\.\d+)/m', $process_list, $matches))
  {
    return $matches[1] / 100;
  }
  else
  {
    return false;
  }
}

var_dump(getScriptLoad());
Untested.