Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
Update Benchmark.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 authored Mar 9, 2017
1 parent bc5452f commit 1addcc5
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/MicroBenchmark/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,56 @@

class Benchmark
{

/**
* @param $iterations
* @param callable $callback
*
* @return BenchmarkResult
*
* @deprecated use task
*/
public static function test($iterations, callable $callback)
{
$i = 0;
$memory = \memory_get_usage();
$time = \microtime(true);
while ($i < $iterations)
return self::task($iterations, $callback);
}

/**
* @param $iterations
* @param callable $callback
*
* @return BenchmarkResult
*/
public static function task($iterations, callable $callback)
{
set_time_limit(0);

$iterator = 0;
$memory = \memory_get_usage();
$time = \microtime(true);

while ($iterator < $iterations)
{
$callback();
$i++;
$callback($iterator++);
}
$stopTime = \microtime(true);
$stopMemory = \memory_get_usage();

$stopTime = \microtime(true);
$stopMemory = \memory_get_usage();
$stopMemoryReal = \memory_get_usage(true);
$stopMemoryPeak = \memory_get_peak_usage();

return new BenchmarkResult([
'start' => $time,
'stop' => $stopTime,
'execution' => $stopTime - $time,
], [
'usage' => $stopMemory - $memory,
'realUsage' => $stopMemoryReal,
'peakUsage' => $stopMemoryPeak,
]);
return new BenchmarkResult(
[
'start' => $time,
'stop' => $stopTime,
'execution' => $stopTime - $time,
],
[
'usage' => $stopMemory - $memory,
'realUsage' => $stopMemoryReal,
'peakUsage' => $stopMemoryPeak,
]
);
}

}

0 comments on commit 1addcc5

Please sign in to comment.