Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Add serializer profiler #45656

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

Merged
merged 1 commit into from
Apr 1, 2022
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
[Serializer] Add serializer profiler
  • Loading branch information
mtarld committed Apr 1, 2022
commit 0e20ad6085b62d9bea91136d58db2ad785c75f99
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class FrameworkExtension extends Extension
private bool $mailerConfigEnabled = false;
private bool $httpClientConfigEnabled = false;
private bool $notifierConfigEnabled = false;
private bool $serializerConfigEnabled = false;
private bool $propertyAccessConfigEnabled = false;
private static bool $lockConfigEnabled = false;

Expand Down Expand Up @@ -386,7 +387,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->getDefinition('exception_listener')->replaceArgument(3, $config['exceptions']);

if ($this->isConfigEnabled($container, $config['serializer'])) {
if ($this->serializerConfigEnabled = $this->isConfigEnabled($container, $config['serializer'])) {
if (!class_exists(\Symfony\Component\Serializer\Serializer::class)) {
throw new LogicException('Serializer support cannot be enabled as the Serializer component is not installed. Try running "composer require symfony/serializer-pack".');
}
Expand Down Expand Up @@ -516,7 +517,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerNotifierConfiguration($config['notifier'], $container, $loader);
}

// profiler depends on form, validation, translation, messenger, mailer, http-client, notifier being registered
// profiler depends on form, validation, translation, messenger, mailer, http-client, notifier, serializer being registered
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);

$this->addAnnotatedClassesToCompile([
Expand Down Expand Up @@ -798,6 +799,10 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
$loader->load('notifier_debug.php');
}

if ($this->serializerConfigEnabled) {
$loader->load('serializer_debug.php');
}

$container->setParameter('profiler_listener.only_exceptions', $config['only_exceptions']);
$container->setParameter('profiler_listener.only_main_requests', $config['only_main_requests']);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Serializer\DataCollector\SerializerDataCollector;
use Symfony\Component\Serializer\Debug\TraceableSerializer;

return static function (ContainerConfigurator $container) {
$container->services()
->set('debug.serializer', TraceableSerializer::class)
->decorate('serializer', null, 255)
->args([
service('debug.serializer.inner'),
service('serializer.data_collector'),
])

->set('serializer.data_collector', SerializerDataCollector::class)
->tag('data_collector', [
'template' => '@WebProfiler/Collector/serializer.html.twig',
'id' => 'serializer',
])
;
};
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"symfony/mime": "<5.4",
"symfony/property-info": "<5.4",
"symfony/property-access": "<5.4",
"symfony/serializer": "<5.4",
"symfony/serializer": "<6.1",
"symfony/security-csrf": "<5.4",
"symfony/security-core": "<5.4",
"symfony/stopwatch": "<5.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% import _self as helper %}

{% block menu %}
<span class="label {{ not collector.handledCount ? 'disabled' }}">
<span class="icon">{{ include('@WebProfiler/Icon/validator.svg') }}</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Icon needs to be updated. We are working on updating all icons soon, so let's not block this PR for now.

<strong>Serializer</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Serializer</h2>
{% if not collector.handledCount %}
<div class="empty">
<p>Nothing was handled by the serializer for this request.</p>
</div>
{% else %}
<div class="metrics">
<div class="metric">
<span class="value">{{ collector.handledCount }}</span>
<span class="label">Handled</span>
</div>

<div class="metric">
<span class="value">{{ '%.2f'|format(collector.totalTime * 1000) }} <span class="unit">ms</span></span>
<span class="label">Total time</span>
</div>
</div>

<div class="sf-tabs">
{{ helper.render_serialize_tab(collector.data, true) }}
{{ helper.render_serialize_tab(collector.data, false) }}

{{ helper.render_normalize_tab(collector.data, true) }}
{{ helper.render_normalize_tab(collector.data, false) }}

{{ helper.render_encode_tab(collector.data, true) }}
{{ helper.render_encode_tab(collector.data, false) }}
</div>
{% endif %}
{% endblock %}

{% macro render_serialize_tab(collectorData, serialize) %}
{% set data = serialize ? collectorData.serialize : collectorData.deserialize %}
{% set cellPrefix = serialize ? 'serialize' : 'deserialize' %}

<div class="tab {{ not data ? 'disabled' }}">
<h3 class="tab-title">{{ serialize ? 'serialize' : 'deserialize' }} <span class="badge">{{ data|length }}</h3>
<div class="tab-content">
{% if not data|length %}
<div class="empty">
<p>Nothing was {{ serialize ? 'serialized' : 'deserialized' }}.</p>
</div>
{% else %}
<table>
<thead>
<tr>
<th>Data</th>
<th>Context</th>
<th>Normalizer</th>
<th>Encoder</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ helper.render_data_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_context_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_normalizer_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_encoder_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_time_cell(item) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
{% endmacro %}

{% macro render_normalize_tab(collectorData, normalize) %}
{% set data = normalize ? collectorData.normalize : collectorData.denormalize %}
{% set cellPrefix = normalize ? 'normalize' : 'denormalize' %}

<div class="tab {{ not data ? 'disabled' }}">
<h3 class="tab-title">{{ normalize ? 'normalize' : 'denormalize' }} <span class="badge">{{ data|length }}</h3>
<div class="tab-content">
{% if not data|length %}
<div class="empty">
<p>Nothing was {{ normalize ? 'normalized' : 'denormalized' }}.</p>
</div>
{% else %}
<table>
<thead>
<tr>
<th>Data</th>
<th>Context</th>
<th>Normalizer</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ helper.render_data_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_context_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_normalizer_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_time_cell(item) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
{% endmacro %}

{% macro render_encode_tab(collectorData, encode) %}
{% set data = encode ? collectorData.encode : collectorData.decode %}
{% set cellPrefix = encode ? 'encode' : 'decode' %}

<div class="tab {{ not data ? 'disabled' }}">
<h3 class="tab-title">{{ encode ? 'encode' : 'decode' }} <span class="badge">{{ data|length }}</h3>
<div class="tab-content">
{% if not data|length %}
<div class="empty">
<p>Nothing was {{ encode ? 'encoded' : 'decoded' }}.</p>
</div>
{% else %}
<table>
<thead>
<tr>
<th>Data</th>
<th>Context</th>
<th>Encoder</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{ helper.render_data_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_context_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_encoder_cell(item, loop.index, cellPrefix) }}</td>
<td>{{ helper.render_time_cell(item) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
{% endmacro %}

{% macro render_data_cell(item, index, method) %}
{% set data_id = 'data-' ~ method ~ '-' ~ index %}

<span class="nowrap">{{ item.dataType }}</span>

<div>
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ data_id }}" data-toggle-alt-content="Hide contents">Show contents</a>
<div id="{{ data_id }}" class="context sf-toggle-content sf-toggle-hidden">
{{ profiler_dump(item.data) }}
</div>
</div>
{% endmacro %}

{% macro render_context_cell(item, index, method) %}
{% set context_id = 'context-' ~ method ~ '-' ~ index %}

{% if item.type %}
<span class="nowrap">Type: {{ item.type }}</span>
<div class="nowrap">Format: {{ item.format ? item.format : 'none' }}</div>
{% else %}
<span class="nowrap">Format: {{ item.format ? item.format : 'none' }}</span>
{% endif %}

<div>
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
{{ profiler_dump(item.context) }}
</div>
</div>
{% endmacro %}

{% macro render_normalizer_cell(item, index, method) %}
{% set nested_normalizers_id = 'nested-normalizers-' ~ method ~ '-' ~ index %}

<span class="nowrap"><a href="{{ item.normalizer.file|file_link(item.normalizer.line) }}" title="{{ item.normalizer.file }}">{{ item.normalizer.class }}</a> ({{ '%.2f'|format(item.normalizer.time * 1000) }} ms)</span>

{% if item.normalization|length > 1 %}
<div>
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ nested_normalizers_id }}" data-toggle-alt-content="Hide nested normalizers">Show nested normalizers</a>
<div id="{{ nested_normalizers_id }}" class="context sf-toggle-content sf-toggle-hidden">
<ul class="text-small" style="line-height:80%;margin-top:10px">
{% for normalizer in item.normalization %}
<li><span class="nowrap">x{{ normalizer.calls }} <a href="{{ normalizer.file|file_link(normalizer.line) }}" title="{{ normalizer.file }}">{{ normalizer.class }}</a> ({{ '%.2f'|format(normalizer.time * 1000) }} ms)</span></li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% endmacro %}

{% macro render_encoder_cell(item, index, method) %}
{% set nested_encoders_id = 'nested-encoders-' ~ method ~ '-' ~ index %}

<span class="nowrap"><a href="{{ item.encoder.file|file_link(item.encoder.line) }}" title="{{ item.encoder.file }}">{{ item.encoder.class }}</a> ({{ '%.2f'|format(item.encoder.time * 1000) }} ms)</span>

{% if item.encoding|length > 1 %}
<div>
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ nested_encoders_id }}" data-toggle-alt-content="Hide nested encoders">Show nested encoders</a>
<div id="{{ nested_encoders_id }}" class="context sf-toggle-content sf-toggle-hidden">
<ul class="text-small" style="line-height:80%;margin-top:10px">
{% for encoder in item.encoding %}
<li><span class="nowrap">x{{ encoder.calls }} <a href="{{ encoder.file|file_link(encoder.line) }}" title="{{ encoder.file }}">{{ encoder.class }}</a> ({{ '%.2f'|format(encoder.time * 1000) }} ms)</span></li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% endmacro %}

{% macro render_time_cell(item) %}
<span class="nowrap">{{ '%.2f'|format(item.time * 1000) }} ms</span>
{% endmacro %}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.1
---

* Add `TraceableSerializer`, `TraceableNormalizer`, `TraceableEncoder` and `SerializerDataCollector` to integrate with the web profiler
* Add the ability to create contexts using context builders
* Set `Context` annotation as not final
* Deprecate `ContextAwareNormalizerInterface`, use `NormalizerInterface` instead
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.