Skip to content

Commit

Permalink
feat: from Buzz to Amphp
Browse files Browse the repository at this point in the history
  • Loading branch information
il-masaru-yamagishi committed Sep 10, 2023
1 parent 7904406 commit 96d8640
Show file tree
Hide file tree
Showing 38 changed files with 643 additions and 1,253 deletions.
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@
],
"require": {
"php": ">=8.1",
"amphp/http-client": "v5.0.0-beta.15",
"kriswallsmith/buzz": "^1.2",
"amphp/http-client": "v5.0.0-beta.17",
"laminas/laminas-diactoros": "^3.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0|^2.0",
"slim/slim": "^4.11",
"symfony/console": "^6.3",
"symfony/process": "^6.3"
"psr/http-message": "^2.0",
"symfony/console": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^10.1"
},
"suggest": {
"ext-mbstring": "For more accurate bodyLength"
"ext-mbstring": "For more accurate bodyLength",
"ext-uv": "For higher concurrency"
},
"config": {
"optimize-autoloader": true,
Expand Down
2 changes: 0 additions & 2 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function __construct()
{
parent::__construct(self::NAME, self::VERSION);
$this->addCommands([
new Commands\GenerateStubCommand(),
new Commands\RunCommand(),
new Commands\WebCommand(),
]);
}
}
27 changes: 0 additions & 27 deletions src/Console/Commands/GenerateStubCommand.php

This file was deleted.

75 changes: 16 additions & 59 deletions src/Console/Commands/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
use Closure;
use Heavyrain\Executor\ExecutorConfig;
use Heavyrain\Executor\ExecutorFactory;
use Heavyrain\Executor\SyncExecutor;
use Heavyrain\HttpClient\ClientFactory;
use Heavyrain\HttpClient\HttpProfiler;
use Heavyrain\Reporters\TableReporter;
use Heavyrain\Scenario\CancellationToken;
use Heavyrain\Scenario\DefaultScenarioConfig;
use Heavyrain\Scenario\HttpProfiler;
use Heavyrain\Scenario\ScenarioConfigInterface;
use ReflectionFunction;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,8 +27,8 @@
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'run',
description: 'Run scenario',
name: 'run:single',
description: 'Run scenario once',
)]
final class RunCommand extends Command implements SignalableCommandInterface
{
Expand All @@ -47,33 +46,11 @@ protected function configure(): void
InputArgument::REQUIRED,
'Request base URI',
)->addOption(
'config',
'c',
'output',
'o',
InputOption::VALUE_REQUIRED,
'PHP filename for scenario config',
)->addOption(
'timeout',
't',
InputOption::VALUE_REQUIRED,
'Request timeout seconds',
0.0,
)->addOption(
'verify-cert',
null,
InputOption::VALUE_NONE,
'Enables SSL/TLS certificate verification',
)->addOption(
'wait-after-scenario',
null,
InputOption::VALUE_REQUIRED,
'Wait seconds after scenario',
1.0,
)->addOption(
'wait-after-request',
null,
InputOption::VALUE_REQUIRED,
'Wait seconds after request',
0.3,
'Output format',
'table',
);
}

Expand All @@ -88,10 +65,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

/** @var string $baseUri */
$baseUri = $input->getArgument('base-uri');
$timeout = \floatval($input->getOption('timeout'));
$verifyCert = \boolval($input->getOption('verify-cert'));
$waitAfterScenarioSec = \floatval($input->getOption('wait-after-scenario'));
$waitAfterSendRequestSec = \floatval($input->getOption('wait-after-request'));

/** @var string */
$scenarioFileName = $input->getArgument('scenario-php-file');
Expand All @@ -113,49 +86,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (\method_exists($scenarioFunction, 'isStatic') && !$scenarioFunction->isStatic()) {
$io->warning('Scenario Closure should be static: `return static function(...`');
}

$userAgentBase = \sprintf(
'%s/%s',
$this->getApplication()?->getName() ?? 'heavyrain',
$this->getApplication()?->getVersion() ?? 'dev',
);
$config = new ExecutorConfig(
$baseUri,
$userAgentBase,
$waitAfterScenarioSec,
$waitAfterSendRequestSec,
$verifyCert,
$timeout,
);
$profiler = new HttpProfiler();
$this->cancelToken = new CancellationToken();

$io->definitionList(
['Base URI' => $baseUri],
['Scenario' => $scenarioFilePath],
['SSL/TLS verify' => $verifyCert ? 'yes' : 'no (default)'],
);

$startMicrosec = \microtime(true);
$io->writeln(\sprintf('Start execution at %s', \date('Y-m-d H:i:s')));
$profiler = new HttpProfiler();

$client = ClientFactory::create($baseUri);

// TODO: Select executor
(new ExecutorFactory($config, $scenarioFunction->getClosure(), $profiler, $client))
->createSync()
(new SyncExecutor($scenarioFunction->getClosure(), new ClientFactory($profiler, $baseUri)))
->execute($this->cancelToken);

$io->writeln(
\sprintf(
'End execution at %s (%f seconds)',
'End execution at %s (%.3f seconds)',
\date('Y-m-d H:i:s'),
\microtime(true) - $startMicrosec,
),
);

// TODO: Select reporter
(new TableReporter($io))->report($profiler);
$reporter = match ($input->getOption('output')) {
'table' => new TableReporter($io),
default => new TableReporter($io),
};
$reporter->report($profiler->getResults());

return Command::SUCCESS;
}
Expand Down
58 changes: 0 additions & 58 deletions src/Console/Commands/WebCommand.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Contracts/ExecutorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ExecutorInterface
* It must not throw Exception
*
* @param CancellationTokenInterface $token
* @return void
* @return iterable
*/
public function execute(CancellationTokenInterface $token): void;
public function execute(CancellationTokenInterface $token): iterable;
}
4 changes: 2 additions & 2 deletions src/Contracts/HttpClientInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

declare(strict_types=1);

/**
* @license MIT
*/

declare(strict_types=1);

namespace Heavyrain\Contracts;

use Heavyrain\Contracts\MiddlewareInterface;
Expand Down
60 changes: 0 additions & 60 deletions src/Contracts/HttpProfilerInterface.php

This file was deleted.

Loading

0 comments on commit 96d8640

Please sign in to comment.