Skip to content

Commit

Permalink
Merge pull request #41 from gacela-project/feature/add-phpbench
Browse files Browse the repository at this point in the history
Add phpbench dependency
  • Loading branch information
Chemaclass committed Jul 17, 2021
2 parents 81459d5 + 318ae13 commit 81ae753
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
/.idea/
/.vscode/
/vendor/
/data/
/.phpbench/
.phpunit.*
.php_cs.cache
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"vimeo/psalm": "^4.6",
"friendsofphp/php-cs-fixer": "^2.19",
"symfony/var-dumper": "^5.3",
"phpstan/phpstan": "^0.12.90"
"phpstan/phpstan": "^0.12.90",
"phpbench/phpbench": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -56,6 +57,7 @@
"psalm": "./vendor/bin/psalm",
"phpstan": "./vendor/bin/phpstan analyze -c phpstan.neon src",
"csfix": "./vendor/bin/php-cs-fixer fix --allow-risky=yes",
"csrun": "./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run"
"csrun": "./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run",
"phpbench": "vendor/bin/phpbench run --report=aggregate --ansi"
}
}
253 changes: 252 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark/",
"runner.retry_threshold": 5,
"runner.iterations": 3,
"runner.revs": 10,
"runner.time_unit": "time",
"runner.assert": [
"mode(variant.time.avg) <= mode(baseline.time.avg) +/- 15%"
],
"report.generators": {
"all": {
"generator": "composite",
"reports": [
"env",
"default",
"aggregate"
]
}
}
}
26 changes: 25 additions & 1 deletion src/Framework/ClassResolver/AbstractClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ abstract class AbstractClassResolver
protected static array $cachedInstances = [];
protected static ?ClassNameFinderInterface $classNameFinder = null;

/** @var array<string,object> */
private static array $globalResolvedClasses = [];

abstract public function resolve(object $callerClass): ?object;

public static function addGlobal(string $key, object $resolvedClass): void
{
self::$globalResolvedClasses[$key] = $resolvedClass;
}

/**
* @return null|mixed
*/
Expand All @@ -29,7 +37,7 @@ public function doResolve(object $callerClass)
$resolvedClassName = $this->findClassName($classInfo);

if (null === $resolvedClassName) {
return null;
return $this->resolveGlobal($cacheKey);
}

self::$cachedInstances[$cacheKey] = $this->createInstance($resolvedClassName);
Expand All @@ -39,6 +47,22 @@ public function doResolve(object $callerClass)

abstract protected function getResolvableType(): string;

/**
* @return null|mixed
*/
private function resolveGlobal(string $cacheKey)
{
$resolvedClass = self::$globalResolvedClasses[$cacheKey] ?? null;

if (null === $resolvedClass) {
return null;
}

self::$cachedInstances[$cacheKey] = $resolvedClass;

return self::$cachedInstances[$cacheKey];
}

private function getCacheKey(ClassInfo $classInfo): string
{
return $classInfo->getCacheKey($this->getResolvableType());
Expand Down

0 comments on commit 81ae753

Please sign in to comment.