<?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 $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 {
$rg_prof_tmp[$label] = $rg_prof_state;
$rg_prof_tmp[$label]['time_ms'] = sprintf("%u", microtime(TRUE) * 1000);
$rg_prof_tmp[$label]['mem'] = intval(memory_get_usage() / 1024);
$rg_prof_tmp[$label]['level'] = 1;
$rg_prof_tmp[$label]['runs'] = 1;
}
}
function rg_prof_end($label)
{
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];
$c = $rg_prof_state;
$c['time_ms'] = sprintf("%u", microtime(TRUE) * 1000);
$c['runs'] = $start['runs']; $start['runs'] = 0;
$c['mem'] = intval(memory_get_usage() / 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 = "";
// 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");
$ret .= str_pad("Profiling:", 22, ' ');
foreach ($vars as $k => $v)
$ret .= "\t" . $k;
$ret .= "\n";
$add = "";
foreach ($rg_prof_main as $label => $per_label) {
$ret .= $add;
$add = "\n";
$ret .= str_pad($label, 22, ' ');
foreach ($vars as $k => $junk) {
if (!isset($per_label[$k]))
$v = 0;
else
$v = $per_label[$k];
$ret .= "\t$v";
}
}
return $ret;
}
function rg_prof_log()
{
$p = rg_prof_text();
rg_log_ml($p);
}
?>
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