Skip to content

Commit

Permalink
feat(profiling): added support for blackfire to collect performance p…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 21 changed files with 2,659 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mutagen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ sync:
defaultDirectoryMode: 755
swoolebundle:
alpha: "."
beta: "docker://app@swoole-bundle-cli/usr/src/app"
beta: "docker://app@swoole-bundle-cli-74/usr/src/app"
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use K911\Swoole\Bridge\Tideways\Apm\RequestProfiler;
use K911\Swoole\Bridge\Tideways\Apm\TidewaysMiddlewareFactory;
use K911\Swoole\Bridge\Tideways\Apm\WithApm;
use K911\Swoole\Bridge\Upscale\Blackfire\ProfilerActivator;
use K911\Swoole\Bridge\Upscale\Blackfire\WithProfiler;
use K911\Swoole\Server\Config\Socket;
use K911\Swoole\Server\Config\Sockets;
Expand Down Expand Up @@ -358,12 +359,20 @@ private function configureHttpServerServices(array $config, ContainerBuilder $co
->setClass(BlackfireProfiler::class)
;

$container->register(ProfilerActivator::class)
->setClass(ProfilerActivator::class)
->setAutowired(false)
->setAutoconfigured(false)
->setPublic(false)
->addArgument(new Reference(BlackfireProfiler::class))
;

$container->register(WithProfiler::class)
->setClass(WithProfiler::class)
->setAutowired(false)
->setAutoconfigured(false)
->setPublic(false)
->addArgument(new Reference(BlackfireProfiler::class))
->addArgument(new Reference(ProfilerActivator::class))
;
$def = $container->getDefinition('swoole_bundle.server.http_server.configurator.for_server_run_command');
$def->addArgument(new Reference(WithProfiler::class));
Expand Down
34 changes: 34 additions & 0 deletions src/Bridge/Upscale/Blackfire/ProfilerActivator.php
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);
}
}
12 changes: 4 additions & 8 deletions src/Bridge/Upscale/Blackfire/WithProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@

use K911\Swoole\Server\Configurator\ConfiguratorInterface;
use Swoole\Http\Server;
use Upscale\Swoole\Blackfire\Profiler;

final class WithProfiler implements ConfiguratorInterface
{
/**
* @var Profiler
*/
private $profiler;
private ProfilerActivator $profilerActivator;

public function __construct(Profiler $profiler)
public function __construct(ProfilerActivator $profilerActivator)
{
$this->profiler = $profiler;
$this->profilerActivator = $profilerActivator;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$this->profiler->instrument($server);
$this->profilerActivator->activate($server);
}
}
82 changes: 82 additions & 0 deletions tests/Fixtures/Symfony/TestBundle/Blackfire/CollectionProfiler.php
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;
}
}
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 tests/Fixtures/Symfony/app/config/coroutines_blackfire/swoole.yaml
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'
22 changes: 10 additions & 12 deletions tests/Unit/Server/Configurator/WithProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,43 @@

namespace K911\Swoole\Tests\Unit\Server\Configurator;

use K911\Swoole\Bridge\Upscale\Blackfire\ProfilerActivator;
use K911\Swoole\Bridge\Upscale\Blackfire\WithProfiler;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Swoole\Http\Server;
use Upscale\Swoole\Blackfire\Profiler;

/**
* @runTestsInSeparateProcesses
*/
class WithProfilerTest extends TestCase
{
use \Prophecy\PhpUnit\ProphecyTrait;
use ProphecyTrait;

/**
* @var WithProfiler
*/
private $configurator;
private WithProfiler $configurator;

/**
* @var ObjectProphecy|Profiler
* @var ObjectProphecy|ProfilerActivator
*/
private $configurationProphecy;

protected function setUp(): void
{
$this->configurationProphecy = $this->prophesize(Profiler::class);
$this->configurationProphecy = $this->prophesize(ProfilerActivator::class);

/** @var Profiler $profilerMock */
$profilerMock = $this->configurationProphecy->reveal();
/** @var ProfilerActivator $profileActivatorMock */
$profileActivatorMock = $this->configurationProphecy->reveal();

$this->configurator = new WithProfiler($profilerMock);
$this->configurator = new WithProfiler($profileActivatorMock);
}

public function testProfiler(): void
{
$swooleServer = $this->createMock(Server::class);

$this->configurationProphecy
->instrument($swooleServer)
->activate($swooleServer)
->shouldBeCalled()
;

Expand Down
7 changes: 7 additions & 0 deletions tests/k6/libs/shim/cheerio.js
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;
Loading

0 comments on commit 52f9133

Please sign in to comment.