Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add example runner for parallel execution of examples #107

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ coverage:
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage

run-all-examples:
for file in ./examples/*.php; do \
echo "Running $$file..."; \
php $$file; \
echo ""; \
done
./example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't now 😃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it's a command now 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was just annoyed of waiting and was keen building that :D

2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"symfony/css-selector": "^6.4 || ^7.1",
"symfony/dom-crawler": "^6.4 || ^7.1",
"symfony/dotenv": "^6.4 || ^7.1",
"symfony/finder": "^6.4 || ^7.1",
"symfony/process": "^6.4 || ^7.1",
"symfony/var-dumper": "^6.4 || ^7.1"
},
"conflict": {
Expand Down
67 changes: 67 additions & 0 deletions example
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env php
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Process;

require_once __DIR__.'/vendor/autoload.php';

$app = (new SingleCommandApplication('LLM Chain Example Runner'))
->setDescription('Runs all LLM Chain examples in folder examples/')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$io->title('LLM Chain Examples');

$examples = (new Finder())
->in(__DIR__.'/examples')
->name('*.php')
->sortByName()
->files();

/** @var array{example: SplFileInfo, section: ConsoleSectionOutput, process: Process, state: 'running'|'finished'} $exampleRuns */
$exampleRuns = [];
foreach ($examples as $example) {
$exampleRuns[] = [
'example' => $example,
'section' => $section = $output->section(),
'process' => $process = new Process(['php', $example->getRealPath()]),
'state' => 'running',
];

$process->start();
$section->writeln(sprintf('Example %s: <comment>Running</comment>', $example->getFilename()));
}

$examplesRunning = fn () => array_reduce($exampleRuns, fn ($running, $example) => $running || $example['process']->isRunning(), false);
$examplesSuccessful = fn () => array_reduce($exampleRuns, fn ($successful, $example) => $successful && $example['process']->isSuccessful(), true);

while ($examplesRunning()) {
sleep(1);
foreach ($exampleRuns as $run) {
if ('running' === $run['state'] && !$run['process']->isRunning()) {
$result = $run['process']->isSuccessful() ? '<info>Finished</info>' : '<error>Failed</error>';
$run['section']->overwrite(sprintf('Example %s: %s', $run['example']->getFilename(), $result));
$run['state'] = 'finished';
}
}
}

$io->newLine();
if (!$examplesSuccessful()) {
$io->error('Some examples failed!');

return Command::FAILURE;
}

$io->success('All examples ran successfully!');

return Command::SUCCESS;
})
->run();