xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / prof.inc.php (ad270b82b26164fc13a886f695504ae36237559e) (4,067B) (mode 100644) [raw]
<?php
// Profiling functions

$rg_prof_main = array();
$rg_prof_state = array();
$rg_prof_tmp = array();

/*
 * This will be called by submodules like git and db
 */
function rg_prof_set($data)
{
	global $rg_prof_state;

	foreach ($data as $k => $v) {
		if (isset($rg_prof_state[$k]))
			$rg_prof_state[$k] += $v;
		else
			$rg_prof_state[$k] = $v;
	}
}

function rg_prof_start($label)
{
	global $peak0;
	global $rg_prof_tmp;
	global $rg_prof_state;

	if (isset($rg_prof_tmp[$label])) {
		// Nested func call
		$rg_prof_tmp[$label]['level']++;
		$rg_prof_tmp[$label]['runs']++;
	} else {
		$mem = memory_get_peak_usage() - $peak0;

		$rg_prof_tmp[$label] = $rg_prof_state;
		$rg_prof_tmp[$label]['time_ms'] = sprintf("%u", microtime(TRUE) * 1000);
		$rg_prof_tmp[$label]['mem'] = intval($mem / 1024);
		$rg_prof_tmp[$label]['level'] = 1;
		$rg_prof_tmp[$label]['runs'] = 1;
	}
}

function rg_prof_end($label)
{
	global $peak0;
	global $rg_prof_tmp;
	global $rg_prof_state;
	global $rg_prof_main;

	if (!isset($rg_prof_tmp[$label])) {
		rg_internal_error("rg_prof_start was not called for label [$label]!");
		return;
	}

	// We are in a recursive call, do not record info in this case
	$rg_prof_tmp[$label]['level']--;
	if ($rg_prof_tmp[$label]['level'] > 0)
		return;

	$start = $rg_prof_tmp[$label];

	$mem = memory_get_peak_usage() - $peak0;

	$c = $rg_prof_state;
	$c['time_ms'] = sprintf("%u", microtime(TRUE) * 1000);
	$c['runs'] = $start['runs']; $start['runs'] = 0;
	$c['mem'] = intval($mem / 1024);
	$c['level'] = 0; // just to not complain that is not defined

	// we substract what was before start
	foreach ($start as $key => $val)
		$c[$key] -= $val;

	// add to main label
	if (!isset($rg_prof_main[$label])) {
		$rg_prof_main[$label] = $c;
	} else {
		foreach ($c as $key => $val) {
			// we may have items that are present at "end" time
			// and not at "start" time
			if (!isset($rg_prof_main[$label][$key]))
				$rg_prof_main[$label][$key] = $val;
			else
				$rg_prof_main[$label][$key] += $val;
		}
	}

	unset($rg_prof_tmp[$label]);
}

function rg_prof_get()
{
	global $rg_prof_main;

	return $rg_prof_main;
}

function rg_prof_html()
{
	global $rg_prof_main;

	// get all possible variables
	$vars = array();
	foreach ($rg_prof_main as $label => $per_label)
		foreach ($per_label as $k => $v)
			$vars[$k] = 1;

	$ret = "<table>";
	$ret .= "<tr>";
	$ret .= "<td>Label</td>";
	foreach ($vars as $var => $junk)
		$ret .= "<td>" . $var . "</td>";
	$ret .= "</tr>";

	foreach ($rg_prof_main as $label => $per_label) {
		$ret .= "<tr>";

		$ret .= "<td>" . $label . "</td>";

		foreach ($vars as $k => $junk) {
			if (!isset($per_label[$k]))
				$v = 0;
			else
				$v = $per_label[$k];
			$ret .= "<td>" . $v . "</td>";
		}

		$ret .= "</tr>";
	}

	$ret .= "</table>\n";

	return $ret;
}

function rg_prof_sort($a, $b)
{
	if (!isset($a['time_ms']))
		return 1;
	if (!isset($b['time_ms']))
		return 1;

	return $a['time_ms'] > $b['time_ms'];
}

function rg_prof_text()
{
	global $rg_prof_main;

	$ret = '';
	$limit = 28;

	// get all possible variables
	$vars = array();
	foreach ($rg_prof_main as $label => $per_label)
		foreach ($per_label as $k => $v)
			$vars[$k] = 1;
	unset($vars['level']);

	// sort by time_ms
	uasort($rg_prof_main, "rg_prof_sort");

	$add = '';
	$ret .= str_pad('Profiling:', $limit, ' ');
	foreach ($vars as $k => $v) {
		$ret .= $add . $k;
		$add = "\t";
	}
	$ret .= "\n";

	$add = "";
	foreach ($rg_prof_main as $label => $per_label) {
		if (($per_label['time_ms'] == 0) && ($per_label['mem'] < 20))
			continue;

		$ret .= $add;
		$add = "\n";

		if (strlen($label) >= $limit)
			$ret .= $label . "\n" . str_pad('', $limit, ' ');
		else
			$ret .= str_pad($label, $limit, ' ');

		$add2 = '';
		foreach ($vars as $k => $junk) {
			if (!isset($per_label[$k]))
				$v = 0;
			else
				$v = $per_label[$k];
			$ret .= $add2 . $v;
			$add2 = "\t";
		}
	}

	return $ret;
}

function rg_prof_log()
{
	$p = rg_prof_text();
	rg_log_ml($p);

	rg_log("peak: " . sprintf("%.2f MiB", memory_get_peak_usage(TRUE) / 1024 / 1024));
}

Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master