Skip to content

Commit 3ee7101

Browse files
committed
feat: replace CommandExecutedListener with CommandAspect for improved command benchmarking
1 parent 316a96e commit 3ee7101

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/command-benchmark/src/Listener/CommandExecutedListener.php src/command-benchmark/src/Aspect/CommandAspect.php

+24-19
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,44 @@
99
* @contact [email protected]
1010
*/
1111

12-
namespace FriendsOfHyperf\CommandBenchmark\Listener;
12+
namespace FriendsOfHyperf\CommandBenchmark\Aspect;
1313

1414
use Hyperf\Collection\Collection;
1515
use Hyperf\Command\Command;
16-
use Hyperf\Command\Event\AfterHandle;
17-
use Hyperf\Command\Event\BeforeHandle;
1816
use Hyperf\Database\Events\QueryExecuted;
19-
use Hyperf\Event\Contract\ListenerInterface;
17+
use Hyperf\Di\Annotation\Aspect;
18+
use Hyperf\Di\Aop\AbstractAspect;
19+
use Hyperf\Di\Aop\ProceedingJoinPoint;
2020
use Hyperf\Event\ListenerProvider;
2121
use Psr\Container\ContainerInterface;
2222
use Psr\EventDispatcher\ListenerProviderInterface;
2323
use WeakMap;
2424

2525
use function Hyperf\Collection\collect;
2626

27-
class CommandExecutedListener implements ListenerInterface
27+
#[Aspect()]
28+
class CommandAspect extends AbstractAspect
2829
{
30+
public array $classes = [
31+
Command::class . '::__construct',
32+
Command::class . '::execute',
33+
];
34+
2935
private WeakMap $metrics;
3036

3137
public function __construct(private ContainerInterface $container)
3238
{
3339
$this->metrics = new WeakMap();
3440
}
3541

36-
public function listen(): array
42+
public function process(ProceedingJoinPoint $proceedingJoinPoint)
3743
{
38-
return [
39-
BeforeHandle::class,
40-
AfterHandle::class,
41-
];
42-
}
44+
/** @var Command $command */
45+
$command = $proceedingJoinPoint->getInstance();
46+
$method = $proceedingJoinPoint->methodName;
47+
$result = $proceedingJoinPoint->process();
4348

44-
/**
45-
* @param BeforeHandle|AfterHandle $event
46-
*/
47-
public function process(object $event): void
48-
{
49-
$command = $event->getCommand();
50-
if ($event instanceof BeforeHandle) {
49+
if ($method === '__construct') {
5150
$this->metrics[$command] = [
5251
'start_at' => microtime(true),
5352
'start_memory' => memory_get_usage(true),
@@ -59,8 +58,12 @@ public function process(object $event): void
5958
$listenerProvider->on(QueryExecuted::class, function () use ($command) {
6059
++$this->metrics[$command]['queries'];
6160
});
61+
62+
$command->addOption('benchmark', null, null, 'Benchmark the command');
63+
$command->addOption('tableToWatch', null, null, 'Table to watch');
6264
}
63-
if ($event instanceof AfterHandle) {
65+
66+
if ($method === 'execute' && $command->option('benchmark')) {
6467
$metrics = collect([
6568
'time' => $this->formatExecutionTime(microtime(true) - $this->metrics[$command]['start_at']),
6669
'memory' => round((memory_get_usage() - $this->metrics[$command]['start_memory']) / 1024 / 1024, 2) . 'MB',
@@ -69,6 +72,8 @@ public function process(object $event): void
6972
$this->renderBenchmarkResults($command, $metrics);
7073
$this->metrics->offsetUnset($command);
7174
}
75+
76+
return $result;
7277
}
7378

7479
private function formatExecutionTime(float $executionTime): string

src/command-benchmark/src/ConfigProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class ConfigProvider
1616
public function __invoke()
1717
{
1818
return [
19-
'listeners' => [
20-
Listener\CommandExecutedListener::class,
19+
'aspects' => [
20+
Aspect\CommandAspect::class,
2121
],
2222
];
2323
}

0 commit comments

Comments
 (0)