Skip to content

Commit

Permalink
chore: update examples to use new datetime API
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Mar 22, 2024
1 parent 6bf3a47 commit 159e2c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/async/usleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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;
});
16 changes: 9 additions & 7 deletions examples/io/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
});
7 changes: 4 additions & 3 deletions examples/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Psl\Example\IO;

use Psl\Async;
use Psl\DateTime;
use Psl\Filesystem;
use Psl\IO;
use Psl\Shell;
Expand Down Expand Up @@ -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];
});
Expand All @@ -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;
Expand Down

0 comments on commit 159e2c2

Please sign in to comment.