-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38dbbe8
commit b4293d7
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support; | ||
|
||
use Closure; | ||
use Illuminate\Support\Arr; | ||
|
||
class Benchmark | ||
{ | ||
/** | ||
* Measure a callable or array of callables over the given number of iterations. | ||
* | ||
* @param \Closure|array $benchmarkables | ||
* @param int $iterations | ||
* @return array|float | ||
*/ | ||
public static function measure(Closure|array $benchmarkables, int $iterations = 1): array|float | ||
{ | ||
return collect(Arr::wrap($benchmarkables))->map(function ($callback) use ($iterations) { | ||
return collect(range(1, $iterations))->map(function () use ($callback) { | ||
gc_collect_cycles(); | ||
|
||
$start = hrtime(true); | ||
|
||
$callback(); | ||
|
||
return (hrtime(true) - $start) / 1000000; | ||
})->average(); | ||
})->when( | ||
$benchmarkables instanceof Closure, | ||
fn ($c) => $c->first(), | ||
fn ($c) => $c->all(), | ||
); | ||
} | ||
|
||
/** | ||
* Measure a callable or array of callables over the given number of iterations, then die and dump. | ||
* | ||
* @param \Closure|array $benchmarkables | ||
* @param int $iterations | ||
* @return void | ||
*/ | ||
public static function dd(Closure|array $benchmarkables, int $iterations = 1): void | ||
{ | ||
dd(static::measure($benchmarkables, $iterations)); | ||
} | ||
} |
b4293d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful to have a syntax like that to spot the bottleneck.
I can work on that.