Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f525822870
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 183 lines (176 sloc) 4.29 kb
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
<?php
namespace shozu;
/**
* Put benchmark points.
*
* Forked from Green framework by Philippe Archambault
*/
final class Benchmark
{
    public static $marks = array();
    public static $enabled = false;
/**
* Bench start point
*
* @param string $name point name
* @return boolean
*/
    public static function start($name)
    {
        if (!self::$enabled)
        {
            return false;
        }
        if (!isset(self::$marks[$name]))
        {
            self::$marks[$name] = array(
                'start' => microtime(true) ,
                'stop' => false,
                'memory_start' => function_exists('memory_get_usage') ? memory_get_usage() : 0,
                'memory_stop' => false
            );
        }
        return true;
    }

/**
* Bench stop point
*
* @param string $name point name
* @return boolean
*/
    public static function stop($name)
    {
        if (!self::$enabled)
        {
            return false;
        }
        if (isset(self::$marks[$name]))
        {
            self::$marks[$name]['stop'] = microtime(true);
            self::$marks[$name]['memory_stop'] = function_exists('memory_get_usage') ? memory_get_usage() : 0;
        }
        return true;
    }
    /**
* Get the elapsed time between a start and stop of a mark name, TRUE for all.
*
* @param string $name
* @param integer $decimals
* @return array
*/
    public static function get($name, $decimals = 4)
    {
        if (!self::$enabled)
        {
            return false;
        }
        if ($name === true)
        {
            $times = array();
            foreach(array_keys(self::$marks) as $name)
            {
                $times[$name] = self::get($name, $decimals);
            }
            return $times;
        }
        if (!isset(self::$marks[$name]))
        {
            return false;
        }
        if (self::$marks[$name]['stop'] === false)
        {
            self::stop($name);
        }
        return array(
            'time' => number_format(self::$marks[$name]['stop'] - self::$marks[$name]['start'], $decimals) ,
            'memory' => self::convert_size(self::$marks[$name]['memory_stop'] - self::$marks[$name]['memory_start'])
        );
    }

/**
* Convert byte size in human readable format
*
* @param integer
* @return string
*/
    public static function convert_size($num)
    {
        if ($num >= 1073741824)
        {
            $num = round($num / 1073741824 * 100) / 100 . ' gb';
        }
        else if ($num >= 1048576)
        {
            $num = round($num / 1048576 * 100) / 100 . ' mb';
        }
        else if ($num >= 1024)
        {
            $num = round($num / 1024 * 100) / 100 . ' kb';
        }
        else
        {
            $num.= ' b';
        }
        return $num;
    }

/**
* Generate HTML-formatted report
*
* @return string
*/
    public static function htmlReport()
    {
        if (!self::$enabled)
        {
            return '';
        }
        $html = '<div style="font-size:14px;font-family:monospace;"><ol>';
        foreach(self::get(true) as $key => $val)
        {
            $html.= '<li><strong>' . htmlspecialchars($key) . '</strong><br/>time&nbsp;&nbsp;: ' . $val['time'] . '<br/>memory: ' . $val['memory'] . '</li>';
        }
        return $html . '</ol></div>';
    }

/**
* Generate CLI/text formatted report
*
* @return string
*/
    public static function cliReport()
    {
        $output = '';
        if (!self::$enabled)
        {
            return $output;
        }
        $points = self::get(true);
        if (!empty($points))
        {
            $output.= "\n#### Bench ####\n";
            foreach($points as $key => $val)
            {
                $output.= "\n[ " . $key . " ]\n time: " . $val['time'] . "\n memory: " . $val['memory'] . "\n";
            }
        }
        return $output;
    }

/**
* Enable benchmark
*
* @return boolean state
*/
    public static function enable()
    {
        self::$enabled = true;
        return self::$enabled;
    }

/**
* Disable benchmark
*
* @return boolean state
*/
    public static function disable()
    {
        self::$enabled = false;
        return self::$enabled;
    }
}
Something went wrong with that request. Please try again.