Skip to content

Commit

Permalink
add sync vs async example
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed May 18, 2024
1 parent 671fab3 commit f761d94
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 7 deletions.
71 changes: 64 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,6 @@ $run = run(
image: '/path/to/image-to-upload.png',
savePath: '/path/to/storage/'
);

// Alternative syntax
$variables = [
'image' => '/path/to/image-to-upload.png',
'savePath' => '/path/to/storage/'
];
$run = run($workflow, ...$variables);
```

Use `getReturn` to retrieve a job response as a `CastArgument` object which can be used to get a typed response.
Expand All @@ -482,6 +475,70 @@ Use `getReturn` to retrieve a job response as a `CastArgument` object which can
$thumbFile = $run->getReturn('thumb')->string();
```

### Sync vs Async

Run live example: `php demo/sync-vs-async.php` - [view source](./demo/sync-vs-async.php)

For this example you can compare the execution time between synchronous and asynchronous jobs. The example fetches the content of three URLs using `FetchUrl` action.

```php
use Chevere\Demo\Actions\FetchUrl;
use function Chevere\Workflow\async;
use function Chevere\Workflow\run;
use function Chevere\Workflow\sync;
use function Chevere\Workflow\variable;
use function Chevere\Workflow\workflow;

$sync = workflow(
php: sync(
new FetchUrl(),
url: variable('php'),
),
github: sync(
new FetchUrl(),
url: variable('github'),
),
chevere: sync(
new FetchUrl(),
url: variable('chevere'),
),
);
$async = workflow(
php: async(
new FetchUrl(),
url: variable('php'),
),
github: async(
new FetchUrl(),
url: variable('github'),
),
chevere: async(
new FetchUrl(),
url: variable('chevere'),
),
);
$variables = [
'php' => 'https://www.php.net',
'github' => 'https://github.com/chevere/workflow',
'chevere' => 'https://chevere.org',
];
$time = microtime(true);
$run = run($sync, ...$variables);
$time = microtime(true) - $time;
echo "Time sync: {$time}\n";
$time = microtime(true);
$run = run($async, ...$variables);
$time = microtime(true) - $time;
echo "Time async: {$time}\n";
```

When running sync (blocking) jobs the execution time is higher than async (non-blocking) jobs. This is because async jobs run in parallel.

```plain
Time sync: 2.5507028102875
Time async: 1.5810508728027
```

### Conditional jobs

Run live example: `php demo/run-if.php` - [view source](./demo/run-if.php)
Expand Down
30 changes: 30 additions & 0 deletions demo/Actions/FetchUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Demo\Actions;

use Chevere\Action\Action;
use RuntimeException;

class FetchUrl extends Action
{
protected function main(string $url): string
{
$content = file_get_contents($url);
if ($content === false) {
throw new RuntimeException('Error fetching URL');
}

return $content;
}
}
66 changes: 66 additions & 0 deletions demo/sync-vs-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use Chevere\Demo\Actions\FetchUrl;
use function Chevere\Workflow\async;
use function Chevere\Workflow\run;
use function Chevere\Workflow\sync;
use function Chevere\Workflow\variable;
use function Chevere\Workflow\workflow;

require 'loader.php';

/*
php demo/sync-vs-async.php
*/
$sync = workflow(
php: sync(
new FetchUrl(),
url: variable('php'),
),
github: sync(
new FetchUrl(),
url: variable('github'),
),
chevere: sync(
new FetchUrl(),
url: variable('chevere'),
),
);
$async = workflow(
php: async(
new FetchUrl(),
url: variable('php'),
),
github: async(
new FetchUrl(),
url: variable('github'),
),
chevere: async(
new FetchUrl(),
url: variable('chevere'),
),
);
$variables = [
'php' => 'https://www.php.net',
'github' => 'https://github.com/chevere/workflow',
'chevere' => 'https://chevere.org',
];
$time = microtime(true);
$run = run($sync, ...$variables);
$time = microtime(true) - $time;
echo "Time sync: {$time}\n";
$time = microtime(true);
$run = run($async, ...$variables);
$time = microtime(true) - $time;
echo "Time async: {$time}\n";

0 comments on commit f761d94

Please sign in to comment.