From 159e2c2e7a0fa1cc6fe1e1948cd0f7b4abd5e5ae Mon Sep 17 00:00:00 2001 From: azjezz Date: Fri, 22 Mar 2024 00:19:17 +0000 Subject: [PATCH] chore: update examples to use new datetime API Signed-off-by: azjezz --- examples/async/usleep.php | 6 +++--- examples/io/benchmark.php | 16 +++++++++------- examples/run.php | 7 ++++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/async/usleep.php b/examples/async/usleep.php index b30f84b3..7c433847 100644 --- a/examples/async/usleep.php +++ b/examples/async/usleep.php @@ -11,7 +11,7 @@ require __DIR__ . '/../../vendor/autoload.php'; Async\main(static function (): int { - $start = microtime(true); + $start = DateTime\Timestamp::now(); Async\concurrently([ static fn() => Async\sleep(DateTime\Duration::hours(0)), @@ -23,9 +23,9 @@ static fn() => Async\sleep(DateTime\Duration::milliseconds(2000)), ]); - $duration = DateTime\Duration::microseconds((int) ((microtime(true) - $start) * DateTime\MICROSECONDS_PER_SECOND)); + $duration = DateTime\Timestamp::now()->since($start); - IO\write_error_line("duration: %s.", $duration->toString(max_decimals: 5)); + IO\write_error_line("duration: %s.", $duration->toString(max_decimals: 2)); return 0; }); diff --git a/examples/io/benchmark.php b/examples/io/benchmark.php index 24454a36..cfe33604 100644 --- a/examples/io/benchmark.php +++ b/examples/io/benchmark.php @@ -5,13 +5,15 @@ namespace Psl\Example\IO; use Psl\Async; +use Psl\DateTime; use Psl\IO; +use Psl\Math; use Psl\Regex; + use function fopen; use function getopt; use function memory_get_peak_usage; -use function microtime; -use function round; + use const PHP_OS_FAMILY; require __DIR__ . '/../../vendor/autoload.php'; @@ -39,7 +41,7 @@ Async\Scheduler::delay($seconds, static fn() => $input->close()); - $start = microtime(true); + $start = DateTime\Timestamp::now(); $i = 0; try { while ($chunk = $input->read(65536)) { @@ -51,12 +53,12 @@ } catch (IO\Exception\AlreadyClosedException) { } - $seconds = microtime(true) - $start; + $duration = DateTime\Timestamp::now()->since($start); $bytes = $i * 65536; - $bytes_formatted = round($bytes / 1024 / 1024 / $seconds, 1); + $bytes_formatted = Math\round($bytes / 1024 / 1024 / $duration->getTotalSeconds(), 1); - IO\write_error_line('read %d byte(s) in %d second(s) => %dMiB/s', $bytes, round($seconds, 3), $bytes_formatted); - IO\write_error_line('peak memory usage of %dMiB', round(memory_get_peak_usage(true) / 1024 / 1024, 1)); + IO\write_error_line('read %d byte(s) in %s => %dMiB/s', $bytes, $duration->toString(), $bytes_formatted); + IO\write_error_line('peak memory usage of %dMiB', Math\round(memory_get_peak_usage(true) / 1024 / 1024, 1)); return 0; }); diff --git a/examples/run.php b/examples/run.php index 45b60756..1613c400 100644 --- a/examples/run.php +++ b/examples/run.php @@ -6,6 +6,7 @@ namespace Psl\Example\IO; use Psl\Async; +use Psl\DateTime; use Psl\Filesystem; use Psl\IO; use Psl\Shell; @@ -34,9 +35,9 @@ IO\write_error_line('- %s/%s -> started', $component, $script); $awaitables[] = Async\run(static function() use($component, $script, $file): array { - $start = microtime(true); + $start = DateTime\Timestamp::now(); Shell\execute(PHP_BINARY, [$file]); - $duration = microtime(true) - $start; + $duration = DateTime\Timestamp::now()->since($start); return [$component, $script, $duration]; }); @@ -46,7 +47,7 @@ foreach (Async\Awaitable::iterate($awaitables) as $awaitable) { [$component, $script, $duration] = $awaitable->await(); - IO\write_error_line('+ %s/%s -> finished in %ds', $component, $script, $duration); + IO\write_error_line('+ %s/%s -> finished in %s', $component, $script, $duration); } return 0;