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

Update demo #153

Merged
merged 3 commits into from
Sep 27, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
build
composer.lock
coverage.xml
demo/tmp
vendor
.phpcs-cache
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ install:

script:
- ./vendor/bin/phpunit;
- php ./demo/run.php

jobs:
fast_finish: true
Expand Down
Binary file added demo/.cache
Binary file not shown.
6 changes: 4 additions & 2 deletions demo/01-explicit-bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use Ray\Aop\Compiler;
use RuntimeException;

use const PHP_EOL;

$compiler = new Compiler(__DIR__ . '/tmp');
$bind = (new Bind)->bindInterceptors(
$bind = (new Bind())->bindInterceptors(
'chargeOrder', // method name
[new WeekendBlocker] // interceptors
[new WeekendBlocker()] // interceptors
);
$billingService = $compiler->newInstance(RealBillingService::class, [], $bind);

Expand Down
10 changes: 6 additions & 4 deletions demo/02-matcher-bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use Ray\Aop\Pointcut;
use RuntimeException;

use const PHP_EOL;

require __DIR__ . '/bootstrap.php';

$pointcut = new Pointcut(
(new Matcher)->any(), // class match
(new Matcher)->startsWith('charge'), // method match
[new WeekendBlocker] // interceptors
(new Matcher())->any(), // class match
(new Matcher())->startsWith('charge'), // method match
[new WeekendBlocker()] // interceptors
);
$bind = (new Bind)->bind(RealBillingService::class, [$pointcut]);
$bind = (new Bind())->bind(RealBillingService::class, [$pointcut]);
$compiler = new Compiler(__DIR__ . '/tmp');
$billingService = $compiler->newInstance(RealBillingService::class, [], $bind);

Expand Down
10 changes: 6 additions & 4 deletions demo/03-annotation-bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use Ray\Aop\Pointcut;
use RuntimeException;

use const PHP_EOL;

require __DIR__ . '/bootstrap.php';

$pointcut = new Pointcut(
(new Matcher)->any(),
(new Matcher)->annotatedWith(WeekendBlock::class),
[new WeekendBlocker]
(new Matcher())->any(),
(new Matcher())->annotatedWith(WeekendBlock::class),
[new WeekendBlocker()]
);
$bind = (new Bind)->bind(AnnotationRealBillingService::class, [$pointcut]);
$bind = (new Bind())->bind(AnnotationRealBillingService::class, [$pointcut]);
$compiler = new Compiler(__DIR__ . '/tmp');
$billingService = $compiler->newInstance(AnnotationRealBillingService::class, [], $bind);

Expand Down
8 changes: 5 additions & 3 deletions demo/04-my-matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
use Ray\Aop\Pointcut;
use RuntimeException;

use const PHP_EOL;

require __DIR__ . '/bootstrap.php';

$pointcut = new Pointcut(
(new Matcher)->any(),
(new Matcher())->any(),
new IsContainsMatcher('charge'),
[new WeekendBlocker]
[new WeekendBlocker()]
);
$bind = new Bind;
$bind = new Bind();
$bind->bind(RealBillingService::class, [$pointcut]);
$compiler = new Compiler(__DIR__ . '/tmp');
$billingService = $compiler->newInstance(RealBillingService::class, [], $bind);
Expand Down
21 changes: 15 additions & 6 deletions demo/05-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
use Ray\Aop\Compiler;
use RuntimeException;

use function file_exists;
use function file_get_contents;
use function microtime;
use function number_format;
use function printf;
use function unserialize;

use const PHP_EOL;

require __DIR__ . '/bootstrap.php';

$cache = '.cache';
if (! file_exists(__DIR__ . '/.cache')) {
$cache = __DIR__ . '/.cache';
if (! file_exists($cache)) {
throw new RuntimeException('Run cache-write.php first');
}

$max = 1;
$start = microtime(true);
$compiler = new Compiler(__DIR__ . '/tmp');
cache_reads:
for ($i = 0; $i < $max; $i++) {
[$bind, $time1] = unserialize(file_get_contents($cache));
$billingService2 = $compiler->newInstance(AnnotationRealBillingService::class, [], $bind);
}
for ($i = 0; $i < $max; $i++) {
[$bind, $time1] = unserialize(file_get_contents($cache));
$billingService2 = $compiler->newInstance(AnnotationRealBillingService::class, [], $bind);
}

$time2 = microtime(true) - $start;

Expand Down
53 changes: 33 additions & 20 deletions demo/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,76 @@
use Ray\Aop\NullInterceptor;
use Ray_Aop_Demo_Optimized;

use function array_map;
use function assert;
use function glob;
use function microtime;
use function sprintf;

use const PHP_EOL;

require __DIR__ . '/bootstrap.php';
require __DIR__ . '/src/FooClass_Optimized.php';

$tmpDir = __DIR__ . '/tmp';
$compiler = new Compiler($tmpDir);
$bind = (new Bind)->bindInterceptors(
$bind = (new Bind())->bindInterceptors(
'intercepted', // method name
[new NullInterceptor] // interceptors
[new NullInterceptor()] // interceptors
);

array_map('unlink', glob("{$tmpDir}/*.php"));
$max = 1000 * 1000;

compile:
$t = microtime(true);
/** @var FooClass $foo */
$foo = $compiler->newInstance(FooClass::class, [], $bind);
assert($foo instanceof FooClass);
echo sprintf('%-16s%.8f[ms]', 'compile', (microtime(true) - $t) * 1000) . PHP_EOL;

initialize:
$t = microtime(true);
/* @var FooClass $foo */
for ($i = 0; $i < $max; $i++) {
$foo = $compiler->newInstance(FooClass::class, [], $bind);
}
/** @var FooClass $foo */
for ($i = 0; $i < $max; $i++) {
$foo = $compiler->newInstance(FooClass::class, [], $bind);
}

echo sprintf('%-16s%.8f[ms]', 'initialize', microtime(true) - $t) . PHP_EOL;

intercepting:
$t = microtime(true);
for ($i = 0; $i < $max; $i++) {
$foo->intercepted();
}
for ($i = 0; $i < $max; $i++) {
$foo->intercepted();
}

echo sprintf('%-16s%.8f[μs]', 'intercepting', microtime(true) - $t) . PHP_EOL;

optimized:
$foo = new Ray_Aop_Demo_Optimized();
$foo->bindings = ['intercepted' => [new NullInterceptor]];
$foo->bindings = ['intercepted' => [new NullInterceptor()]];
$t = microtime(true);
for ($i = 0; $i < $max; $i++) {
$foo->intercepted();
}
for ($i = 0; $i < $max; $i++) {
$foo->intercepted();
}

echo sprintf('%-16s%.8f[μs]', 'optimized?', microtime(true) - $t) . PHP_EOL;

no_intercepting:
$t = microtime(true);
for ($i = 0; $i < $max; $i++) {
$foo->noInterceptor();
}
for ($i = 0; $i < $max; $i++) {
$foo->noInterceptor();
}

// should be same with native_call
echo sprintf('%-16s%.8f[μs]', 'no_intercepting', microtime(true) - $t) . PHP_EOL;

native_call:
$bareFoo = new FooClass();
$t = microtime(true);
for ($i = 0; $i < $max; $i++) {
$bareFoo->noInterceptor();
}
for ($i = 0; $i < $max; $i++) {
$bareFoo->noInterceptor();
}

echo sprintf('%-16s%.8f[μs]', 'native_call', microtime(true) - $t) . PHP_EOL;

//compile 7.96413422[ms]
Expand Down
22 changes: 13 additions & 9 deletions demo/cache-write.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
use Ray\Aop\Matcher;
use Ray\Aop\Pointcut;

use function file_put_contents;
use function microtime;
use function serialize;

$start = microtime(true);
$max = 1000;
cache_write:
$compiler = new Compiler(__DIR__ . '/tmp');
for ($i = 0; $i < $max; $i++) {
$pointcut = new Pointcut(
(new Matcher)->any(),
(new Matcher)->annotatedWith(WeekendBlock::class),
[new WeekendBlocker]
);
$bind = (new Bind)->bind(AnnotationRealBillingService::class, [$pointcut]);
$compiler->newInstance(AnnotationRealBillingService::class, [], $bind);
}
for ($i = 0; $i < $max; $i++) {
$pointcut = new Pointcut(
(new Matcher())->any(),
(new Matcher())->annotatedWith(WeekendBlock::class),
[new WeekendBlocker()]
);
$bind = (new Bind())->bind(AnnotationRealBillingService::class, [$pointcut]);
$compiler->newInstance(AnnotationRealBillingService::class, [], $bind);
}

$time1 = microtime(true) - $start;
file_put_contents(__DIR__ . '/.cache', serialize([$bind, $time1]));
2 changes: 2 additions & 0 deletions demo/src/AnnotationRealBillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ray\Aop\Demo;

use const PHP_EOL;

class AnnotationRealBillingService implements BillingService
{
/**
Expand Down
1 change: 1 addition & 0 deletions demo/src/FooClass_Optimized.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function intercepted()

return call_user_func_array([$this, 'parent::' . __FUNCTION__], func_get_args());
}

$this->isAspect = false;
$result = (new Invocation($this, __FUNCTION__, func_get_args(), $this->bindings[__FUNCTION__]))->proceed();
$this->isAspect = true;
Expand Down
6 changes: 4 additions & 2 deletions demo/src/IsContainsMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use ReflectionClass;
use ReflectionMethod;

use function strpos;

final class IsContainsMatcher extends AbstractMatcher
{
/**
* {@inheritdoc}
*/
public function matchesClass(ReflectionClass $class, array $arguments) : bool
public function matchesClass(ReflectionClass $class, array $arguments): bool
{
[$contains] = $arguments;

Expand All @@ -23,7 +25,7 @@ public function matchesClass(ReflectionClass $class, array $arguments) : bool
/**
* {@inheritdoc}
*/
public function matchesMethod(ReflectionMethod $method, array $arguments) : bool
public function matchesMethod(ReflectionMethod $method, array $arguments): bool
{
[$contains] = $arguments;

Expand Down
2 changes: 2 additions & 0 deletions demo/src/ManualAdvice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ray\Aop\Demo;

use const PHP_EOL;

class ManualAdvice
{
public function before()
Expand Down
2 changes: 2 additions & 0 deletions demo/src/RealBillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Ray\Aop\Demo;

use const PHP_EOL;

class RealBillingService implements BillingService
{
public function chargeOrder()
Expand Down
5 changes: 5 additions & 0 deletions demo/src/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

use function microtime;
use function sprintf;

use const PHP_EOL;

class Timer implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
Expand Down
2 changes: 2 additions & 0 deletions demo/src/WeekendBlocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Ray\Aop\MethodInvocation;
use RuntimeException;

use function getdate;

class WeekendBlocker implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
Expand Down
2 changes: 2 additions & 0 deletions demo/src/interceptorA.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

use const PHP_EOL;

class interceptorA implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
Expand Down
2 changes: 2 additions & 0 deletions demo/src/interceptorB.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

use const PHP_EOL;

class interceptorB implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
Expand Down