Benchmark   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 170
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 4 1
A end() 0 10 2
A getTime() 0 14 4
A getMemoryUsage() 0 4 2
A getMemoryPeak() 0 6 2
A run() 0 11 1
A readableSize() 0 20 4
A readableElapsedTime() 0 18 3
A hasEnded() 0 4 1
A hasStarted() 0 4 1
1
<?php
2
3
namespace Asad\Bench;
4
5
use LogicException;
6
7
class Benchmark
8
{
9
    protected $start_time;
10
11
    protected $end_time;
12
13
    protected $memory_usage;
14
15
    /**
16
     * Sets start microtime
17
     *
18
     * @return void
19
     */
20
    public function start()
21
    {
22
        $this->start_time = microtime(true);
23
    }
24
25
    /**
26
     * Sets end microtime
27
     *
28
     * @return $this
29
     * @throws Exception
30
     */
31
    public function end()
32
    {
33
        if (!$this->hasStarted()) {
34
            throw new LogicException("You must call start()");
35
        }
36
37
        $this->end_time = microtime(true);
38
        $this->memory_usage = memory_get_usage(true);
39
        return $this;
40
    }
41
42
    /**
43
     * Returns the elapsed time, readable or not
44
     *
45
     * @param bool $raw
46
     * @param  string $format The format to display (printf format)
47
     * @return float|string
48
     * @throws Exception
49
     */
50
    public function getTime($raw = false, $format = null)
51
    {
52
        if (!$this->hasStarted()) {
53
            throw new LogicException("You must call start()");
54
        }
55
56
        if (!$this->hasEnded()) {
57
            throw new LogicException("You must call end()");
58
        }
59
60
        $elapsed = $this->end_time - $this->start_time;
61
62
        return $raw ? $elapsed : self::readableElapsedTime($elapsed, $format);
63
    }
64
65
    /**
66
     * Returns the memory usage at the end checkpoint
67
     *
68
     * @param  boolean $readable Whether the result must be human readable
0 ignored issues
show
Bug introduced by
There is no parameter named $readable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
     * @param  string  $format   The format to display (printf format)
70
     * @return string|float
71
     */
72
    public function getMemoryUsage($raw = false, $format = null)
73
    {
74
        return $raw ? $this->memory_usage : self::readableSize($this->memory_usage, $format);
75
    }
76
77
    /**
78
     * Returns the memory peak, readable or not
79
     *
80
     * @param  boolean $readable Whether the result must be human readable
0 ignored issues
show
Bug introduced by
There is no parameter named $readable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
81
     * @param  string  $format   The format to display (printf format)
82
     * @return string|float
83
     */
84
    public function getMemoryPeak($raw = false, $format = null)
85
    {
86
        $memory = memory_get_peak_usage(true);
87
88
        return $raw ? $memory : self::readableSize($memory, $format);
89
    }
90
91
    /**
92
     * Wraps a callable with start() and end() calls
93
     *
94
     * Additional arguments passed to this method will be passed to
95
     * the callable.
96
     *
97
     * @param callable $callable
98
     * @return mixed
99
     */
100
    public function run(callable $callable)
101
    {
102
        $arguments = func_get_args();
103
        array_shift($arguments);
104
105
        $this->start();
106
        $result = call_user_func_array($callable, $arguments);
107
        $this->end();
108
109
        return $result;
110
    }
111
112
    /**
113
     * Returns a human readable memory size
114
     *
115
     * @param   int    $size
116
     * @param   string $format   The format to display (printf format)
117
     * @param   int    $round
118
     * @return  string
119
     */
120
    public static function readableSize($size, $format = null, $round = 3)
121
    {
122
        $mod = 1024;
123
124
        if (is_null($format)) {
125
            $format = '%.2f%s';
126
        }
127
128
        $units = explode(' ', 'B Kb Mb Gb Tb');
129
130
        for ($i = 0; $size > $mod; $i++) {
131
            $size /= $mod;
132
        }
133
134
        if (0 === $i) {
135
            $format = preg_replace('/(%.[\d]+f)/', '%d', $format);
136
        }
137
138
        return sprintf($format, round($size, $round), $units[$i]);
139
    }
140
141
    /**
142
     * Returns a human readable elapsed time
143
     *
144
     * @param  float $microtime
145
     * @param  string  $format   The format to display (printf format)
146
     * @return string
147
     */
148
    public static function readableElapsedTime($microtime, $format = null, $round = 3)
149
    {
150
        if (is_null($format)) {
151
            $format = '%.3f%s';
152
        }
153
154
        if ($microtime >= 1) {
155
            $unit = 's';
156
            $time = round($microtime, $round);
157
        } else {
158
            $unit = 'ms';
159
            $time = round($microtime * 1000);
160
161
            $format = preg_replace('/(%.[\d]+f)/', '%d', $format);
162
        }
163
164
        return sprintf($format, $time, $unit);
165
    }
166
167
    public function hasEnded()
168
    {
169
        return isset($this->end_time);
170
    }
171
172
    public function hasStarted()
173
    {
174
        return isset($this->start_time);
175
    }
176
}
177