Sitelet https://github.com/phpbench/phpbench/pull/1137/files
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/Executor/Benchmark/template/remote.template
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ if ($warmup) {

// only pass parameters if they are supplied:passing parameters costs time
if ($parameters) {
$startTime = microtime(true);
$startTime = hrtime(true);

for ($i = 0; $i < {{ revolutions }}; $i++) {
$benchmark->{{ subject }}($parameters);
}
$endTime = microtime(true);
$endTime = hrtime(true);
} else {
$startTime = microtime(true);
$startTime = hrtime(true);

for ($i = 0; $i < {{ revolutions }}; $i++) {
$benchmark->{{ subject }}();
}
$endTime = microtime(true);
$endTime = hrtime(true);
}


$time = ($endTime - $startTime) * 1000000;
$time = ($endTime - $startTime);

// run after methods
foreach ($afterMethods as $afterMethod) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function load(Container $container): void
});

$container->register(TimeUnit::class, function (Container $container) {
return new TimeUnit(TimeUnit::MICROSECONDS, TimeUnit::MICROSECONDS);
return new TimeUnit(TimeUnit::NANOSECONDS, TimeUnit::NANOSECONDS);
});

$this->registerJson($container);
Expand Down
34 changes: 22 additions & 12 deletions lib/Util/TimeUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
*/
class TimeUnit
{
final public const NANOSECOND = 'nanosecond';
final public const MILLISECOND = 'millisecond';
final public const MICROSECOND = 'microsecond';
final public const SECOND = 'second';
final public const MINUTE = 'minute';
final public const HOUR = 'hour';
final public const DAY = 'day';

final public const NANOSECONDS = 'nanoseconds';
final public const MICROSECONDS = 'microseconds';
final public const MILLISECONDS = 'milliseconds';
final public const SECONDS = 'seconds';
Expand All @@ -39,30 +41,34 @@ class TimeUnit

/** @var array<string, positive-int> */
private static array $map = [
self::MICROSECONDS => 1,
self::MILLISECONDS => 1000,
self::SECONDS => 1_000_000,
self::MINUTES => 60_000_000,
self::HOURS => 3_600_000_000,
self::DAYS => 86_400_000_000,
self::NANOSECONDS => 1,
self::MICROSECONDS => 1000,
self::MILLISECONDS => 1000_000,
self::SECONDS => 1_000_000_000,
self::MINUTES => 60_000_000_000,
self::HOURS => 3_600_000_000_000,
self::DAYS => 86_400_000_000_000,
];

/** @var array<string, string> */
private static array $aliases = [
self::NANOSECOND => self::NANOSECONDS,
self::MICROSECOND => self::MICROSECONDS,
self::MILLISECOND => self::MILLISECONDS,
self::SECOND => self::SECONDS,
self::MINUTE => self::MINUTES,
self::HOUR => self::HOURS,
self::DAY => self::DAYS,
'us' => self::MICROSECONDS,
'ns' => self::NANOSECONDS,
'ms' => self::MILLISECONDS,
's' => self::SECONDS,
'm' => self::MINUTES,
];

/** @var array<string, string> */
private static array $suffixes = [
self::NANOSECONDS => 'ns',
self::MICROSECONDS => 'μs',
self::MILLISECONDS => 'ms',
self::SECONDS => 's',
Expand All @@ -77,7 +83,7 @@ class TimeUnit

private bool $overriddenPrecision = false;

public function __construct(private readonly string $sourceUnit = self::MICROSECONDS, private string $destUnit = self::MICROSECONDS, private string $mode = self::MODE_TIME, private int $precision = 3)
public function __construct(private readonly string $sourceUnit = self::NANOSECONDS, private string $destUnit = self::NANOSECONDS, private string $mode = self::MODE_TIME, private int $precision = 3)
{
}

Expand Down Expand Up @@ -373,21 +379,25 @@ public static function normalizeUnit(string $unit): string
public static function resolveSuitableUnit(?float $value): string
{
if (null === $value) {
return self::MICROSECONDS;
return self::NANOSECONDS;
}

if (($value / 60E6) >= 1) {
if (($value / 60E9) >= 1) {
return self::MINUTES;
}

if (($value / 1E6) >= 1) {
if (($value / 1E9) >= 1) {
return self::SECONDS;
}

if (($value / 1E3) >= 1) {
if (($value / 1E6) >= 1) {
return self::MILLISECONDS;
}

return self::MICROSECONDS;
if (($value / 1E3) >= 1) {
return self::MICROSECONDS;
}

return self::NANOSECONDS;
}
}
1 change: 1 addition & 0 deletions tests/Example/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RuntimeException;
use Symfony\Component\Console\Input\StringInput;

use function _HumbugBox904bb52280f8\Amp\Promise\wait;

Check failure on line 15 in tests/Example/CommandsTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Used function _HumbugBox904bb52280f8\Amp\Promise\wait not found.
use function json_last_error_msg;

class CommandsTest extends IntegrationTestCase
Expand Down
28 changes: 21 additions & 7 deletions tests/Unit/Util/TimeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ public static function provideConvert(): array
*/
public static function provideAliases(): Generator
{
yield [
1,
TimeUnit::NANOSECONDS,
TimeUnit::NANOSECONDS,
1
];

yield [
1,
TimeUnit::MICROSECONDS,
TimeUnit::NANOSECONDS,
1000
];

yield [
1,
TimeUnit::MICROSECOND,
Expand Down Expand Up @@ -335,18 +349,18 @@ public function testResolveSuitableUnit(float $value, string $expectedUnit): voi
*/
public static function provideSuitableUnit(): Generator
{
yield [1, 'microseconds'];
yield [1, 'nanoseconds'];

yield [100, 'microseconds'];
yield [100_000, 'microseconds'];

yield [1000, 'milliseconds'];
yield [1_000_000, 'milliseconds'];

yield [10000, 'milliseconds'];
yield [10_000_000, 'milliseconds'];

yield [100000, 'milliseconds'];
yield [100_000_000, 'milliseconds'];

yield [1_000_000, 'seconds'];
yield [1_000_000_000, 'seconds'];

yield [60_000_000, 'minutes'];
yield [60_000_000_000, 'minutes'];
}
}
Loading