BenchMark   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 208
rs 10
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A repeat() 0 9 2
A systemInfo() 0 11 2
A tasks() 0 3 1
A duration() 0 9 2
A report() 0 3 1
A getMatrix() 0 6 1
A end() 0 10 2
A title() 0 11 1
A task() 0 3 1
A start() 0 17 3
A run() 0 18 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace App\BenchMark\Reporter;
19
20
use App\BenchMark\Reporter\Printer\PrinterInterface;
21
22
class BenchMark
23
{
24
    /** @var int The repetition number. */
25
    protected int $repeat = 1;
26
27
    /** @var array<string,Task> The task collection. */
28
    protected array $tasks = [];
29
30
    /** The visual reporter instance. */
31
    protected PrinterInterface $printer;
32
33
    public function __construct(?PrinterInterface $printer = null)
34
    {
35
        $this->printer = $printer ?? new Printer\Text();
36
        $this->printer->bind($this);
37
    }
38
39
    /**
40
     * Returns the reporter
41
     *
42
     * @return string
43
     */
44
    public function report(): string
45
    {
46
        return $this->printer->chart();
47
    }
48
49
    /**
50
     * Gets/sets the repeat number.
51
     *
52
     * @param int $repeat the repeat value to set or none the get the current one
53
     *
54
     * @return int|self the repeat value or `$this` on set
55
     */
56
    public function repeat(?int $repeat = null)
57
    {
58
        if (null !== $repeat) {
59
            $this->repeat = $repeat;
60
61
            return $this;
62
        }
63
64
        return $this->repeat;
65
    }
66
67
    /**
68
     * Wraps a callable with start() and end() calls
69
     *
70
     * Additional arguments passed to this method will be passed to
71
     * the callable.
72
     *
73
     * @param callable $callable
74
     * @param  mixed    ...
75
     *
76
     * @return mixed
77
     */
78
    public function run(string $taskName, callable $callback)
79
    {
80
        $args = array_slice(\func_get_args(), 2);
81
82
        $task = $this->start($taskName, $this->repeat());
83
84
        for ($i = 0; $i < $task->repeat(); $i++) {
85
            $handle = ($callback)(...$args);
86
87
            if ($handle === false) {
88
                $task->failed(true);
89
90
                break;
91
            }
92
        }
93
        $this->end($taskName);
94
95
        return $task;
96
    }
97
98
    /**
99
     * Starts the timer for a task
100
     *
101
     * @param string   $taskName the taskname to start
102
     * @param intnull| $repeat   the number of times the task will be executed
0 ignored issues
show
Bug introduced by
The type App\BenchMark\Reporter\intnull was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
     *
104
     * @return Task the started task
105
     */
106
    public function start(string $taskName, ?int $repeat = null)
107
    {
108
        $task = new Task();
109
        $task->name($taskName);
110
111
        if (null !== $repeat) {
112
            $task->repeat($repeat);
113
        }
114
115
        if (isset($this->tasks[$taskName])) {
116
            throw new \RuntimeException("Task {$taskName} is already defined.");
117
        }
118
119
        $this->tasks[$taskName] = $task;
120
        $task->start();
121
122
        return $task;
123
    }
124
125
    /**
126
     * Ends the timer for a task
127
     *
128
     * @param string $taskName the taskname to stop the timer for
129
     *
130
     * @return Task the stopped task
131
     */
132
    public function end(string $taskName): Task
133
    {
134
        if (!isset($this->tasks[$taskName])) {
135
            throw new \RuntimeException("Undefined task name: `'{$taskName}`.");
136
        }
137
138
        $task = $this->tasks[$taskName];
139
        $task->end();
140
141
        return $task;
142
    }
143
144
    /**
145
     * Returns a specific task.
146
     *
147
     * @param string $name the task name
148
     *
149
     * @return null|Task
150
     */
151
    public function task(string $name): ?Task
152
    {
153
        return $this->tasks[$name] ?? null;
154
    }
155
156
    /**
157
     * Returns all created tasks.
158
     *
159
     * @return Task[]
160
     */
161
    public function tasks()
162
    {
163
        return \array_values($this->tasks);
164
    }
165
166
    /**
167
     * Returns the total duration.
168
     *
169
     * @return float|int the total duration (in microseconds)
170
     */
171
    public function duration()
172
    {
173
        $duration = 0;
174
175
        foreach ($this->tasks as $task) {
176
            $duration += $task->duration();
177
        }
178
179
        return $duration;
180
    }
181
182
    /**
183
     * Returns the processed matrix result report.
184
     *
185
     * @return Matrix
186
     */
187
    public function getMatrix(): Matrix
188
    {
189
        $matrix = new Matrix($this->tasks());
190
        $matrix->process();
191
192
        return $matrix;
193
    }
194
195
    /**
196
     * Returns the system info.
197
     *
198
     * @return string the system info
199
     */
200
    public static function systemInfo(): string
201
    {
202
        $result = '';
203
204
        $result .= 'PHP Version: ' . \PHP_MAJOR_VERSION . '.' . \PHP_MINOR_VERSION . '.' . \PHP_RELEASE_VERSION . ' [' . \php_uname() . ']';
205
206
        if (\extension_loaded('xdebug')) {
207
            $result .= " - With XDebug Extension.\n";
208
        }
209
210
        return $result;
211
    }
212
213
    /**
214
     * Titleizes a string
215
     *
216
     * @param string $title the string to titleize
217
     * @param string $pad
218
     */
219
    public static function title(string $title, string $pad = '='): string
220
    {
221
        $rest = (78 - \mb_strlen($title)) / 2;
222
223
        $result = "\n\n";
224
        $result .= \str_repeat($pad, (int) $rest);
225
        $result .= ' ' . $title . ' ';
226
        $result .= \str_repeat($pad, (int) $rest);
227
        $result .= "\n\n";
228
229
        return $result;
230
    }
231
}
232