Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Issue 490 flush method (#632)
Browse files Browse the repository at this point in the history
#490 adding flush to profiler
  • Loading branch information
prisis authored Oct 5, 2017
1 parent 9e38ef9 commit 581af1a
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 69 deletions.
39 changes: 19 additions & 20 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ matrix:
services:
- memcached

cache: # cache is cleared when linked file is modified
- '%LOCALAPPDATA%\Composer\files -> composer.lock'
- composer.phar
# cache is cleared when linked file is modified
cache:
# Cache chocolatey packages
- C:\ProgramData\chocolatey\bin -> .appveyor.yml
- C:\ProgramData\chocolatey\lib -> .appveyor.yml
# Cache php install
- C:\tools\php -> .appveyor.yml
- .phpunit -> phpunit

init:
- SET PHP=1
- SET PATH=C:\Program Files\OpenSSL;C:\tools\php;%PATH%
- SET ANSICON=121x90 (121x90)
- SET COMPOSER_NO_INTERACTION=1
Expand All @@ -41,24 +40,24 @@ init:

install:
# Install PHP
- mkdir C:\tools\php
- appveyor DownloadFile "https://raw.githubusercontent.com/prisis/ps-install-php/master/Install-PHP.ps1"
- ps: .\Install-PHP.ps1 -InstallPath C:\tools\php -Version $Env:php_ver_target -Highest -Arch x64 -Extensions mbstring,intl,openssl,memcache,fileinfo,pdo_sqlite,curl,sodium
- IF EXIST C:\tools\php (SET PHP=0) ELSE (mkdir C:\tools\php)
- IF %PHP%==1 appveyor DownloadFile "https://raw.githubusercontent.com/prisis/ps-install-php/master/Install-PHP.ps1"
- ps: if ($env.PHP) {.\Install-PHP.ps1 -InstallPath C:\tools\php -Version $Env:php_ver_target -Highest -Arch x64 -Extensions mbstring,intl,openssl,memcache,fileinfo,pdo_sqlite,curl,sodium}
# Install redis and start
- nuget install redis-64 -excludeversion -Verbosity quiet
- redis-64\tools\redis-server.exe --service-install
- redis-64\tools\redis-server.exe --service-start
- IF %PHP%==1 nuget install redis-64 -excludeversion -Verbosity quiet
- IF %PHP%==1 redis-64\tools\redis-server.exe --service-install
- IF %PHP%==1 redis-64\tools\redis-server.exe --service-start
# PHP settings and extensions
- cd C:\tools\php
- cd ext
- appveyor DownloadFile https://github.com/nono303/PHP7-memcache-dll/blob/master/vc15/x64/nts/php-7.2.x_memcache.dll?raw=true
- rename php-7.2.x_memcache.dll php_memcache.dll
- cd ..
- echo zend_extension=php_opcache.dll >> php.ini
- echo max_execution_time=1200 >> php.ini
- echo date.timezone="America/Los_Angeles" >> php.ini
- echo opcache.enable_cli=1 >> php.ini
- echo apc.enable_cli=1 >> php.ini
- IF %PHP%==1 cd C:\tools\php
- IF %PHP%==1 cd ext
- IF %PHP%==1 appveyor DownloadFile https://github.com/nono303/PHP7-memcache-dll/blob/master/vc15/x64/nts/php-7.2.x_memcache.dll?raw=true
- IF %PHP%==1 rename php-7.2.x_memcache.dll php_memcache.dll
- IF %PHP%==1 cd ..
- IF %PHP%==1 echo zend_extension=php_opcache.dll >> php.ini
- IF %PHP%==1 echo max_execution_time=1200 >> php.ini
- IF %PHP%==1 echo date.timezone="America/Los_Angeles" >> php.ini
- IF %PHP%==1 echo opcache.enable_cli=1 >> php.ini
- IF %PHP%==1 echo apc.enable_cli=1 >> php.ini
- IF %APPVEYOR_REPO_BRANCH%==master (SET COMPOSER_ROOT_VERSION=dev-master) ELSE (SET COMPOSER_ROOT_VERSION=%APPVEYOR_REPO_BRANCH%.x-dev)
- cd C:\projects\narrowspark
- php -r "readfile('https://getcomposer.org/installer');" | php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"php-amqplib/php-amqplib" : "^2.6",
"pda/pheanstalk" : "^3.1",
"spatie/array-to-xml" : "^2.1",
"twig/twig" : "~2.4",
"twig/twig" : "^2.4.4",
"sepia/po-parser" : "^4.2",
"spatie/flysystem-dropbox" : "^1.0",
"symfony/routing" : "~3.3",
Expand Down
15 changes: 13 additions & 2 deletions src/Viserio/Bridge/Twig/DataCollector/TwigDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TwigDataCollector extends AbstractDataCollector implements
*
* @var array
*/
private $computed;
private $computed = [];

/**
* Create new twig collector instance.
Expand Down Expand Up @@ -218,6 +218,17 @@ public function getTooltip(): string
]);
}

/**
* {@inheritdoc}
*/
public function reset()
{
$this->profile->reset();

$this->computed = [];
$this->data = [];
}

/**
* Get computed data.
*
Expand All @@ -227,7 +238,7 @@ public function getTooltip(): string
*/
private function getComputedData(string $index)
{
if ($this->computed === null) {
if (count($this->computed) === 0) {
$this->computed = $this->generateComputeData($this->getProfile());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Viserio/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php" : "^7.2",
"twig/twig" : "~2.4"
"twig/twig" : "^2.4.4"
},
"require-dev": {
"container-interop/service-provider" : "^0.4",
Expand Down
7 changes: 7 additions & 0 deletions src/Viserio/Component/Contract/Profiler/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ public function getMenu(): array;
* @return string
*/
public function getMenuPosition(): string;

/**
* Resets this data collector to its initial state.
*
* @return void
*/
public function flush(): void;
}
5 changes: 5 additions & 0 deletions src/Viserio/Component/Contract/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ public function setUrlGenerator(UrlGeneratorContract $urlGenerator): Profiler;
* @return null|\Viserio\Component\Contract\Routing\UrlGenerator
*/
public function getUrlGenerator(): ?UrlGeneratorContract;

/**
* @return void
*/
public function flush(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ public function getOrphanedEvents(): array
return $this->orphanedEvents;
}

/**
* Resets this to its initial state.
*
* @return void
*/
public function flush(): void
{
$this->called = [];
}

/**
* @param string $eventName
* @param $listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ public function getPanel(): string
],
]);
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
$this->data = [];

$this->eventManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,19 @@ public function testListenerCanRemoveItselfWhenExecuted()

self::assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
}

public function testClearCalledListeners()
{
$this->wrapperDispatcher->attach('foo', function () {
}, 5);

$this->wrapperDispatcher->trigger('foo');
$this->wrapperDispatcher->flush();

$listeners = $this->wrapperDispatcher->getNotCalledListeners();

self::assertArrayHasKey('stub', $listeners['foo'][0]);

self::assertEquals([], $this->wrapperDispatcher->getCalledListeners());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ protected function getRouteInformation(RouteContract $route): array

$result = [
'uri' => $route->getUri() ?: '-',
'methods' => \count((array) $route->getMethods()) > 1 ?
'methods' => \count($route->getMethods()) > 1 ?
\implode(' | ', $route->getMethods()) :
$route->getMethods(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public function getData(): array
return $this->data;
}

/**
* + * {@inheritdoc}
* + */
public function flush(): void
{
$this->data = [];
}

/**
* Add measurement to float time.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public function getAssets(): array
'js' => __DIR__ . '/../Resources/js/ajaxHandler.js',
];
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
// all collecting is done client side
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ public function getCalls(): int
return $this->data['pools']['calls'];
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
parent::flush();

foreach ($this->pools as $instance) {
// Calling getCalls() will clear the calls.
$instance->getCalls();
}
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace Viserio\Component\Profiler\DataCollector\Bridge\Log;
namespace Viserio\Component\Profiler\DataCollector\Bridge\Monolog;

use Monolog\Logger;

Expand Down Expand Up @@ -65,4 +65,15 @@ public function countErrors(): int
{
return $this->errorCount;
}

/**
* Removes all log records.
*
* @return void
*/
public function flush(): void
{
$this->records = [];
$this->errorCount = 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace Viserio\Component\Profiler\DataCollector\Bridge\Log;
namespace Viserio\Component\Profiler\DataCollector\Bridge\Monolog;

use ErrorException;
use Monolog\Logger;
Expand Down Expand Up @@ -93,13 +93,9 @@ public function getTooltip(): string
*/
public function getPanel(): string
{
$tableHeaders = [
'Level',
'Channel',
'Message',
];
$logs = $this->groupLogLevels();
$tableHeaders = ['Level', 'Channel', 'Message'];

$logs = $this->groupLogLevels();
$html = $this->createTabs([
[
'name' => 'Info. & Errors <span class="counter">' . \count($logs['info_error']) . '</span>',
Expand Down Expand Up @@ -230,10 +226,20 @@ public function getLogs(): array
return [];
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
if (($logger = $this->getDebugLogger()) && method_exists($logger, 'flush')) {
$logger->flush();
}
}

/**
* Returns a DebugProcessor instance if one is registered with this logger.
*
* @return null|\Viserio\Component\Profiler\DataCollector\Bridge\Log\DebugProcessor
* @return null|\Viserio\Component\Profiler\DataCollector\Bridge\Monolog\DebugProcessor
*/
private function getDebugLogger(): ?DebugProcessor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ class MemoryDataCollector extends AbstractDataCollector implements TooltipAwareC
*/
public function __construct()
{
$memoryLimit = \ini_get('memory_limit');

$this->data = [
'memory' => 0,
'memory_limit' => $memoryLimit == '-1' ? -1 : self::convertToBytes($memoryLimit),
];
$this->flush();
}

/**
Expand Down Expand Up @@ -68,4 +63,17 @@ public function updateMemoryUsage(): void
{
$this->data['memory'] = \memory_get_peak_usage(true);
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
$memoryLimit = \ini_get('memory_limit');

$this->data = [
'memory' => 0,
'memory_limit' => $memoryLimit == '-1' ? -1 : self::convertToBytes($memoryLimit),
];
}
}
10 changes: 10 additions & 0 deletions src/Viserio/Component/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ public function getAssetsRenderer(): AssetsRendererContract
return $this->assetsRenderer;
}

/**
* {@inheritdoc}
*/
public function flush(): void
{
foreach ($this->collectors as $data) {
$data['collector']->flush();
}
}

/**
* Injects the web debug toolbar into the given Response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Viserio\Component\Contract\OptionsResolver\RequiresComponentConfig as RequiresComponentConfigContract;
use Viserio\Component\Contract\Profiler\Profiler as ProfilerContract;
use Viserio\Component\OptionsResolver\Traits\OptionsResolverTrait;
use Viserio\Component\Profiler\DataCollector\Bridge\Log\DebugProcessor;
use Viserio\Component\Profiler\DataCollector\Bridge\Log\MonologLoggerDataCollector;
use Viserio\Component\Profiler\DataCollector\Bridge\Monolog\DebugProcessor;
use Viserio\Component\Profiler\DataCollector\Bridge\Monolog\MonologLoggerDataCollector;

class ProfilerMonologDataCollectorServiceProvider implements
ServiceProviderInterface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public static function extendEventManager(
): ?EventManagerContract {
if ($eventManager !== null) {
$eventManager->attach(TerminableContract::TERMINATE, function () use ($container) {
foreach ($container->get(ProfilerContract::class)->getCollectors() as $collector) {
// @todo clear collector
}
$container->get(ProfilerContract::class)->flush();
});
}

Expand Down
Loading

0 comments on commit 581af1a

Please sign in to comment.