how can i get my script's load?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpist
Forum Newbie
Posts: 7
Joined: Wed Feb 22, 2006 4:03 am
Location: istanbul_TR

how can i get my script's load?

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Styx
Forum Newbie
Posts: 9
Joined: Thu Jun 21, 2007 5:37 am

Post by Styx »

Put a time() variable at top and time() variable at bottom, subtract the two, and format.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply