diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:01:30 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:01:30 +0200 |
| commit | 47cbb529f5723b246125ae083a193e11481b89ef (patch) | |
| tree | 31ab20b2fbf12bee1cd994c3d6fb822fa52622e3 /classes/Benchmark.php | |
| download | classroom-47cbb529f5723b246125ae083a193e11481b89ef.tar.gz classroom-47cbb529f5723b246125ae083a193e11481b89ef.tar.bz2 classroom-47cbb529f5723b246125ae083a193e11481b89ef.zip | |
initializing classroom structure using anom framework
Diffstat (limited to 'classes/Benchmark.php')
| -rw-r--r-- | classes/Benchmark.php | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/classes/Benchmark.php b/classes/Benchmark.php new file mode 100644 index 0000000..a7e93ea --- /dev/null +++ b/classes/Benchmark.php @@ -0,0 +1,150 @@ +<?php + +// define('BENCHMARKED', true); // then you know if class is active + +class Benchmark { + + private static $timeSpots = Array(); + + private static $memoryUse = Array(); + + private static $enableReport = true; + + + /** add_timespot + * --- + * add new timespot + * @param $key : label of timespot + */ + public static function add_spot(string $key) + { + self::$timeSpots[$key] = microtime(true); + self::$memoryUse[$key] = round(memory_get_usage()/(1024*1024),2); + } + + + /** exist + * checks if a benchmark spot with label $key exists + */ + public static function exist(string $key) : bool + { + return isset(self::$timeSpots[$key]); + } + + + /** desableReport + * (Self-explanatory) + */ + public static function disableReport() + { + self::$enableReport = false; + } + + + /** render_benchmark_report + * --- + * renders performance report + * as html comment at the end of webpage in various modes + * @param $mode (string) + * + 'comment' (default): include report as html comment + * + 'html' : include report as a html div + * + 'code' : include report as a pre/code block + */ + public static function render_report(string $mode = 'comment' ) + { + if (self::$enableReport) { + + switch ($mode) { + case 'html': + $open = '<p>'; + $close = '</b></p>'; + $divider = ":<b>"; + $prefix = '<div>'; + $suffix = '</div>'; + break; + case 'code': + $open = ''; + $close = ''; + $divider = ": "; + $prefix = '<pre>'; + $suffix = '</pre>'; + break; + case 'comment': + default: + $open = '<!--'; + $close = '-->'; + $divider = ":"; + $prefix = ''; + $suffix = ''; + break; + } + + echo $prefix; + + // TIME REPORT + // --------------------------------------------------------------------- + echo "\n\n{$open} Timings {$close}"; + + // valid from php 7.3 + // $start = self::$timeSpots[ array_key_first(self::$timespots) ]; + // $end = self::$timeSpots[ array_key_last(self::$timespots) ]; + $start = self::$timeSpots['start']; + $end = self::$timeSpots['end']; + $all_dt = number_format($end - $start , 4)*1000 ." ms"; + + + + // if more than 2 timespots + // echo dt between each spot + if (count(self::$timeSpots) > 2) { + + // calculate dt between spots + $time_results = array(); + $prev_key = ''; + $prev_time = 0; + foreach(self::$timeSpots as $key => $t) { + if (!$prev_time) { + $prev_time = $t; + $prev_key = $key; + } + else { + $dt = number_format($t - $prev_time , 4)*1000 ." ms"; + $time_results[] = array( + 'part' => "{$prev_key}[..{$key}]", ///$key, + 'dt' => $dt + ); + $prev_key = $key; //// "{$prev_key}[..{$key}]"; + $prev_time = $t; + } + } + + // echo timings + foreach ($time_results as $key => $val) { + echo "\n\t{$open} {$val['part']} {$divider} {$val['dt']} {$close}"; + } + } + + // echo total time + echo "\n\t{$open} total {$divider} {$all_dt} {$close}"; + + + // MEMORY REPORT + // --------------------------------------------------------------------- + echo "\n\n{$open} Memory Usage {$close}"; + + if (count(self::$memoryUse) > 2) { + + foreach (self::$memoryUse as $key => $val) { + echo "\n\t{$open} {$key} {$divider} {$val}MB {$close}"; + } + + } else { + echo "\n\t{$open} memory usage {$divider} ". round(memory_get_usage()/(1024*1024),2) ."MB {$close}"; + } + + echo $suffix; + + } + + } +} |
