summaryrefslogtreecommitdiff
path: root/classes/Benchmark.php
blob: a7e93eabe5f21f98dda3936b68df793446db3154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;

        }
        
    }
}