-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(profiling): added support for blackfire to collect performance p…
…rofiles from multiple requests combined into one profile (even with coroutines enabled)
- Loading branch information
Rastusik
committed
Feb 6, 2023
1 parent
993eaf8
commit 52f9133
Showing
21 changed files
with
2,659 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Upscale\Blackfire; | ||
|
||
use Swoole\Http\Server; | ||
use Upscale\Swoole\Blackfire\Profiler; | ||
use Upscale\Swoole\Blackfire\ProfilerDecorator; | ||
use Upscale\Swoole\Reflection\Http\Server as UpscaleServer; | ||
|
||
class ProfilerActivator | ||
{ | ||
private Profiler $profiler; | ||
|
||
public function __construct(Profiler $profiler) | ||
{ | ||
$this->profiler = $profiler; | ||
} | ||
|
||
public function activate(Server $server): void | ||
{ | ||
$server = new UpscaleServer($server); | ||
$server->setMiddleware($this->wrap($server->getMiddleware(), $this->profiler)); | ||
} | ||
|
||
/** | ||
* Decorate a given middleware for profiling. | ||
*/ | ||
private function wrap(callable $middleware, Profiler $profiler): ProfilerDecorator | ||
{ | ||
return new ProfilerDecorator($middleware, $profiler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/Fixtures/Symfony/TestBundle/Blackfire/CollectionProfiler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Blackfire; | ||
|
||
use Blackfire\Client; | ||
use Blackfire\Profile\Configuration; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Upscale\Swoole\Blackfire\Profiler; | ||
|
||
class CollectionProfiler extends Profiler | ||
{ | ||
private Client $client; | ||
|
||
public function __construct(Client $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Starts multiple request profiling when GET parameter "profile_start" is set. | ||
*/ | ||
public function start(Request $request) | ||
{ | ||
/* @phpstan-ignore-next-line */ | ||
if ($this->probe) { | ||
return true; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
if (!isset($request->get['profile_start'])) { | ||
return false; | ||
} | ||
|
||
// start a probe on each request | ||
$configuration = new Configuration(); | ||
$title = sprintf('Collection profile: %s', $request->get['profile_start']); | ||
$configuration->setTitle($title); | ||
$this->probe = $this->client->createProbe($configuration, false); | ||
$this->request = $request; | ||
|
||
if (!$this->probe->enable()) { | ||
$this->reset(); | ||
|
||
throw new \UnexpectedValueException('Cannot enable Blackfire profiler'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Stops multiple request profiling when GET parameter "profile_stop" is set. | ||
*/ | ||
public function stop(Request $request, Response $response) | ||
{ | ||
if (!isset($request->get['profile_stop'])) { | ||
return false; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
if ($this->probe) { | ||
$this->client->endProbe($this->probe); | ||
$this->reset(); | ||
|
||
return true; | ||
} | ||
|
||
/* @phpstan-ignore-next-line */ | ||
return false; | ||
} | ||
|
||
/** | ||
* Reset profiling session. | ||
*/ | ||
public function reset() | ||
{ | ||
/* @phpstan-ignore-next-line */ | ||
$this->probe = null; | ||
/* @phpstan-ignore-next-line */ | ||
$this->request = null; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Fixtures/Symfony/app/config/coroutines_blackfire/framework.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
framework: | ||
cache: | ||
app: cache.adapter.array | ||
system: cache.adapter.array | ||
|
||
messenger: | ||
enabled: true | ||
reset_on_message: true # do not forget this or an equivalent! :) | ||
transports: | ||
swoole: swoole://task | ||
routing: | ||
'K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Message\SleepAndAppend': swoole | ||
'K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Message\RunDummy': swoole |
80 changes: 80 additions & 0 deletions
80
tests/Fixtures/Symfony/app/config/coroutines_blackfire/swoole.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
parameters: | ||
env(WORKER_COUNT): 1 | ||
env(TASK_WORKER_COUNT): 1 | ||
env(REACTOR_COUNT): 1 | ||
|
||
swoole: | ||
http_server: | ||
exception_handler: | ||
type: symfony | ||
services: | ||
blackfire_profiler: true | ||
task_worker: | ||
settings: | ||
worker_count: '%env(int:TASK_WORKER_COUNT)%' | ||
services: | ||
reset_handler: true # default is true anyway | ||
platform: | ||
coroutines: | ||
enabled: true | ||
stateful_services: | ||
- K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\ShouldBeProxified | ||
compile_processors: | ||
- class: K911\Swoole\Tests\Fixtures\Symfony\TestBundle\DependencyInjection\CompilerPass\SleepingCounterCompileProcessor | ||
priority: 10 | ||
- K911\Swoole\Tests\Fixtures\Symfony\TestBundle\DependencyInjection\CompilerPass\ResetCountCompileProcessor | ||
|
||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\ShouldBeProxified2: | ||
tags: | ||
- { name: 'swoole_bundle.stateful_service' } | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Controller\CoroutinesTaskController: | ||
tags: | ||
- controller.service_arguments | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\MessageHandler\SleepAndAppendHandler: | ||
tags: | ||
- messenger.message_handler | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\MessageHandler\RunDummyHandler: | ||
tags: | ||
- messenger.message_handler | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysReset: | ||
tags: | ||
- { name: 'swoole_bundle.stateful_service', reset_on_each_request: true } | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysResetSafe: | ||
tags: | ||
- { name: 'swoole_bundle.safe_stateful_service', reset_on_each_request: true } | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\NonSharedExample: | ||
public: true | ||
shared: false | ||
tags: | ||
- { name: 'swoole_bundle.stateful_service' } | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\RepositoryFactory: | ||
tags: | ||
- name: 'swoole_bundle.unmanaged_factory' | ||
factoryMethod: 'newInstance' | ||
returnType: 'K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\InMemoryRepository' | ||
limit: 1000 | ||
resetter: 'inmemory_repository_resetter' | ||
|
||
Blackfire\Client: | ||
|
||
K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Blackfire\CollectionProfiler: | ||
arguments: | ||
$client: '@Blackfire\Client' | ||
|
||
# comment out these service definition to disable blackfire profiling for multiple requests, contained in the k6 test script | ||
K911\Swoole\Bridge\Upscale\Blackfire\ProfilerActivator: | ||
arguments: | ||
$profiler: '@K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Blackfire\CollectionProfiler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* global postman */ | ||
|
||
import cheerio from '../cheerio.js'; | ||
|
||
const Extend = Symbol.for('extend'); | ||
|
||
postman[Extend].module.cheerio = cheerio; |
Oops, something went wrong.