9
9
10
10
*/
11
11
12
- namespace FriendsOfHyperf \CommandBenchmark \Listener ;
12
+ namespace FriendsOfHyperf \CommandBenchmark \Aspect ;
13
13
14
14
use Hyperf \Collection \Collection ;
15
15
use Hyperf \Command \Command ;
16
- use Hyperf \Command \Event \AfterHandle ;
17
- use Hyperf \Command \Event \BeforeHandle ;
18
16
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 ;
20
20
use Hyperf \Event \ListenerProvider ;
21
21
use Psr \Container \ContainerInterface ;
22
22
use Psr \EventDispatcher \ListenerProviderInterface ;
23
23
use WeakMap ;
24
24
25
25
use function Hyperf \Collection \collect ;
26
26
27
- class CommandExecutedListener implements ListenerInterface
27
+ #[Aspect()]
28
+ class CommandAspect extends AbstractAspect
28
29
{
30
+ public array $ classes = [
31
+ Command::class . '::__construct ' ,
32
+ Command::class . '::execute ' ,
33
+ ];
34
+
29
35
private WeakMap $ metrics ;
30
36
31
37
public function __construct (private ContainerInterface $ container )
32
38
{
33
39
$ this ->metrics = new WeakMap ();
34
40
}
35
41
36
- public function listen (): array
42
+ public function process ( ProceedingJoinPoint $ proceedingJoinPoint )
37
43
{
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 ();
43
48
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 ' ) {
51
50
$ this ->metrics [$ command ] = [
52
51
'start_at ' => microtime (true ),
53
52
'start_memory ' => memory_get_usage (true ),
@@ -59,8 +58,12 @@ public function process(object $event): void
59
58
$ listenerProvider ->on (QueryExecuted::class, function () use ($ command ) {
60
59
++$ this ->metrics [$ command ]['queries ' ];
61
60
});
61
+
62
+ $ command ->addOption ('benchmark ' , null , null , 'Benchmark the command ' );
63
+ $ command ->addOption ('tableToWatch ' , null , null , 'Table to watch ' );
62
64
}
63
- if ($ event instanceof AfterHandle) {
65
+
66
+ if ($ method === 'execute ' && $ command ->option ('benchmark ' )) {
64
67
$ metrics = collect ([
65
68
'time ' => $ this ->formatExecutionTime (microtime (true ) - $ this ->metrics [$ command ]['start_at ' ]),
66
69
'memory ' => round ((memory_get_usage () - $ this ->metrics [$ command ]['start_memory ' ]) / 1024 / 1024 , 2 ) . 'MB ' ,
@@ -69,6 +72,8 @@ public function process(object $event): void
69
72
$ this ->renderBenchmarkResults ($ command , $ metrics );
70
73
$ this ->metrics ->offsetUnset ($ command );
71
74
}
75
+
76
+ return $ result ;
72
77
}
73
78
74
79
private function formatExecutionTime (float $ executionTime ): string
0 commit comments