qa-util-debug.php 4.28 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-util-debug.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Debugging stuff, currently used for tracking resource usage


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	function qa_usage_init()
/*
	Initialize the counts of resource usage
*/
	{
		global $qa_database_usage, $qa_database_queries, $qa_usage_start, $qa_usage_last;
Scott Vivian committed
33

Gideon Greenspan committed
34 35 36 37 38
		$qa_database_usage=array('queries' => 0, 'clock' => 0);
		$qa_database_queries='';
		$qa_usage_last=$qa_usage_start=qa_usage_get();
	}

Scott Vivian committed
39

Gideon Greenspan committed
40 41 42 43 44 45
	function qa_usage_get()
/*
	Return an array representing the resource usage as of now
*/
	{
		global $qa_database_usage;
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49 50 51 52 53
		$usage=array(
			'files' => count(get_included_files()),
			'queries' => $qa_database_usage['queries'],
			'ram' => function_exists('memory_get_usage') ? memory_get_usage() : 0,
			'clock' => array_sum(explode(' ', microtime())),
			'mysql' => $qa_database_usage['clock'],
		);
Scott Vivian committed
54

Gideon Greenspan committed
55 56 57 58 59 60
		if (function_exists('getrusage')) {
			$rusage=getrusage();
			$usage['cpu']=$rusage["ru_utime.tv_sec"]+$rusage["ru_stime.tv_sec"]
				+($rusage["ru_utime.tv_usec"]+$rusage["ru_stime.tv_usec"])/1000000;
		} else
			$usage['cpu']=0;
Scott Vivian committed
61

Gideon Greenspan committed
62
		$usage['other']=$usage['clock']-$usage['cpu']-$usage['mysql'];
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66
		return $usage;
	}

Scott Vivian committed
67

Gideon Greenspan committed
68 69 70 71 72 73
	function qa_usage_delta($oldusage, $newusage)
/*
	Return the difference between two resource usage arrays, as an array
*/
	{
		$delta=array();
Scott Vivian committed
74

Gideon Greenspan committed
75 76
		foreach ($newusage as $key => $value)
			$delta[$key]=max(0, $value-@$oldusage[$key]);
Scott Vivian committed
77

Gideon Greenspan committed
78 79 80
		return $delta;
	}

Scott Vivian committed
81

Gideon Greenspan committed
82 83 84 85 86 87
	function qa_usage_mark($stage)
/*
	Mark the beginning of a new stage of script execution and store usages accordingly
*/
	{
		global $qa_usage_last, $qa_usage_stages;
Scott Vivian committed
88

Gideon Greenspan committed
89 90 91 92 93 94 95 96 97 98 99 100
		$usage=qa_usage_get();
		$qa_usage_stages[$stage]=qa_usage_delta($qa_usage_last, $usage);
		$qa_usage_last=$usage;
	}


	function qa_usage_line($stage, $usage, $totalusage)
/*
	Return HTML to represent the resource $usage, showing appropriate proportions of $totalusage
*/
	{
		return sprintf(
Gideon Greenspan committed
101
			"%s &ndash; <b>%.1fms</b> (%d%%) &ndash; PHP %.1fms (%d%%), MySQL %.1fms (%d%%), Other %.1fms (%d%%) &ndash; %d PHP %s, %d DB %s, %dk RAM (%d%%)",
Gideon Greenspan committed
102 103 104 105 106 107 108 109 110 111
			$stage, $usage['clock']*1000, $usage['clock']*100/$totalusage['clock'],
			$usage['cpu']*1000, $usage['cpu']*100/$totalusage['clock'],
			$usage['mysql']*1000, $usage['mysql']*100/$totalusage['clock'],
			$usage['other']*1000, $usage['other']*100/$totalusage['clock'],
			$usage['files'], ($usage['files']==1) ? 'file' : 'files',
			$usage['queries'], ($usage['queries']==1) ? 'query' : 'queries',
			$usage['ram']/1024, $usage['ram'] ? ($usage['ram']*100/$totalusage['ram']) : 0
		);
	}

Scott Vivian committed
112

Gideon Greenspan committed
113 114 115 116 117 118
	function qa_usage_output()
/*
	Output an (ugly) block of HTML detailing all resource usage and database queries
*/
	{
		global $qa_usage_start, $qa_usage_stages, $qa_database_queries;
Scott Vivian committed
119

Gideon Greenspan committed
120
		echo '<p><br><table bgcolor="#cccccc" cellpadding="8" cellspacing="0" width="100%">';
Scott Vivian committed
121

Gideon Greenspan committed
122
		echo '<tr><td colspan="2">';
Scott Vivian committed
123

Gideon Greenspan committed
124
		$totaldelta=qa_usage_delta($qa_usage_start, qa_usage_get());
Scott Vivian committed
125

Gideon Greenspan committed
126
		echo qa_usage_line('Total', $totaldelta, $totaldelta).'<br>';
Scott Vivian committed
127

Gideon Greenspan committed
128
		foreach ($qa_usage_stages as $stage => $stagedelta)
Scott Vivian committed
129

Gideon Greenspan committed
130
		echo '<br>'.qa_usage_line(ucfirst($stage), $stagedelta, $totaldelta);
Scott Vivian committed
131

Gideon Greenspan committed
132
		echo '</td></tr><tr valign="bottom"><td width="30%"><textarea cols="40" rows="20" style="width:100%;">';
Scott Vivian committed
133

Gideon Greenspan committed
134 135
		foreach (get_included_files() as $file)
			echo qa_html(implode('/', array_slice(explode('/', $file), -3)))."\n";
Scott Vivian committed
136

Gideon Greenspan committed
137
		echo '</textarea></td>';
Scott Vivian committed
138

Gideon Greenspan committed
139
		echo '<td width="70%"><textarea cols="40" rows="20" style="width:100%;">'.qa_html($qa_database_queries).'</textarea></td>';
Scott Vivian committed
140

Gideon Greenspan committed
141
		echo '</tr></table>';
Gideon Greenspan committed
142 143 144 145 146 147
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/