1<?php
2
3/**
4 * MPLT - Measure Page Load Time
5 *
6 * @author Nicolas Embriz <nbari@dalmp.com>
7 * @license BSD License
8 */
9class MPLT
10{
11    private $decimals;
12    private $time_start;
13    private $time_end;
14    private $marks = array();
15
16    /**
17     * constructor
18     *
19     * @param int $decimals
20     */
21    public function __construct($decimals = 4)
22    {
23        $this->time_start = microtime(true);
24        $this->decimals = $decimals;
25    }
26
27    /**
28     * stop
29     */
30    public function Stop()
31    {
32        $this->time_end = microtime(true);
33    }
34
35    /**
36     * setMark
37     *
38     * @param string $name
39     */
40    public function setMark($name = null)
41    {
42        $mark = microtime(true) - $this->time_start;
43        $last_mark = end($this->marks)[0];
44        $diff = $mark - $last_mark;
45        $mark = number_format($mark, $this->decimals);
46        $diff = number_format($diff, $this->decimals);
47        if ($name && $name != 'total') {
48            $this->marks[$name] = array($mark, $diff);
49        } else {
50            $this->marks[] = array($mark, $diff);
51        }
52    }
53
54    /**
55     * getMark
56     *
57     * @param  string $name
58     * @return array
59     */
60    public function getMark($name = null)
61    {
62        return $name ? $this->marks[$name] : reset($this->marks);
63    }
64
65    /**
66     * getMarks
67     *
68     * @return array
69     */
70    public function getMarks()
71    {
72        return $this->marks;
73    }
74
75    /**
76     * printMarks
77     */
78    public function printMarks()
79    {
80        if ($this->marks) {
81            $pad = $this->decimals * 2;
82            $max_length = max(array_map('strlen', array_keys($this->marks)));
83
84            if ($pad < $max_length) {
85                $pad = $max_length + 2;
86            }
87
88            echo str_pad('mark', $pad), str_pad('time', $pad), 'elapsed-time', $this->isCli(1);
89            foreach ($this->marks as $mark => $values) {
90                echo str_pad($mark, $pad), str_pad($values[0], $pad), $values[1], $this->isCli(1);
91            }
92        } else {
93            echo 'no defined marks', $this->isCli(1);
94        }
95    }
96
97    /**
98     * getPageLoadTime
99     *
100     * @param bool $marks
101     */
102    public function getPageLoadTime($marks = false)
103    {
104        if (empty($this->time_end)) {
105            $this->Stop();
106        }
107
108        $lt = number_format($this->time_end - $this->time_start, $this->decimals);
109
110        if ($marks) {
111            $this->marks['total'] = $lt;
112
113            return $this->marks;
114        } else {
115            return $lt;
116        }
117    }
118
119    /**
120     * getMemoryUsage
121     *
122     * @param bool $convert "Human-readable" output.
123     */
124    public function getMemoryUsage($convert = false)
125    {
126        return $convert ? $this->convert(memory_get_usage(true)) : memory_get_usage(true);
127    }
128
129    /**
130     * convert
131     *
132     * @param int $size
133     */
134    public function convert($size)
135    {
136        $unit = array('B','KB','MB','GB','TB','PB');
137
138        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
139    }
140
141    /**
142     * isCli()
143     *
144     * @param  boolean $eol
145     * @return boolean or PHP_EOL, <br/>
146     */
147    public function isCli($eol = null)
148    {
149        ($cli = (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))) && $cli = $eol ? PHP_EOL : true;
150
151        return $cli ?: ($eol ? '<br/>' : false);
152    }
153
154}
155