8000 Extract the profiler to a new component · symfony/symfony-docs@8995be1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8995be1

Browse files
author
jelte
committed
Extract the profiler to a new component
1 parent ae02f08 commit 8995be1

File tree

5 files changed

+53
-75
lines changed

5 files changed

+53
-75
lines changed

components/map.rst.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115

116116
* :doc:`/components/process`
117117

118+
* :doc:`/components/profiler/index`
119+
120+
* :doc:`/components/profiler/introduction`
121+
118122
* :doc:`/components/property_access/index`
119123

120124
* :doc:`/components/property_access/introduction`

components/profiler/introduction.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ Enabling them all is as easy as it can get::
3232
$profiler = new Profiler($storage);
3333

3434
// $profile is an implementation of ProfileInterface.
35-
$profile = $profiler->profile($profile);
36-
37-
$profiler->save($profile);
38-
39-
Shortcuts are provided to profile HTTP Requests and Console Commands::
40-
41-
// Profile an HTTP Request & Repsonse
42-
$profiler->profileRequest($request, $response);
43-
44-
// Profile a Console Command
45-
$profiler->profileCommand($command, $input, $exitCode);
35+
$profile = $profiler->profile();
36+
37+
$profiler->save(
38+
$profile,
39+
array(
40+
'url' => http://localhost/',
41+
'ip' => '127.0.0.1',
42+
'method' => 'GET',
43+
'response_code' => 200,
44+
'profile_type' => 'http',
45+
)
46+
);
4647

4748
if your project makes use of the :doc:`EventDispatcher component </components/event_dispatcher/introduction>`, you can automate the profiling by using the corresponding
48-
EventListeners :class:`Symfony\\Component\\Profiler\\EventListener\\HttpProfilerListener` and
49-
:class:`Symfony\\Component\\Profiler\\EventListener\\ConsoleProfilerListener`.
49+
EventListeners :class:`Symfony\\Component\\Profiler\\EventListener\\HttpProfilerListener`.
5050

5151
.. caution::
5252

cookbook/profiler/data_collector.rst

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,40 @@ Creating a custom data collector is as simple as implementing the
1717
interface DataCollectorInterface
1818
{
1919
/**
20-
* Set the Token of the active profile.
20+
* Returns the collected data.
2121
*
22-
* @param $token
23-
*/
24-
public function setToken($token);
25-
26-
/**
27-
* Returns the name of the co 67E6 llector.
22+
* @return ProfileDataInterface
2823
*
29-
* @return string The collector name
24+
* @todo introduce in 3.0
3025
*/
31-
function getName();
26+
public function getCollectedData();
3227
}
3328

34-
The ``getName()`` method must return a unique name. This is used to access the
35-
information later on (see :doc:`/cookbook/testing/profiling` for
36-
instance).
37-
38-
And implementing either the :class:`Symfony\\Component\\Profiler\\DataCollector\\RuntimeDataCollectorInterface`::
29+
if the data should be collected just prior to the Profile being saved add the :class:`Symfony\\Component\\Profiler\\DataCollector\\LateDataCollectorInterface`::
3930

40-
interface RuntimeDataCollectorInterface
31+
interface LateDataCollectorInterface
4132
{
42-
/**
43-
* Collects data when profiler is triggered.
44-
*
45-
* @return ProfileDataInterface
46-
*/
47-
public function collect();
4833
}
4934

50-
or the :class:`Symfony\\Component\\Profiler\\DataCollector\\LateDataCollectorInterface`::
35+
The ``getCollectedData()`` method is responsible for storing the data it wants to give
36+
access to in a :class:`Symfony\\Component\\Profiler\\ProfileData\\ProfileDataInterface`::
5137

52-
interface LateDataCollectorInterface
38+
interface ProfileDataInterface extends \Serializable
5339
{
54-
/**
55-
* Collects data as late as possible.
56-
*
57-
* @return ProfileDataInterface
58-
*/
59-
public function lateCollect();
40+
public function getName();
6041
}
6142

62-
The ``collect()`` or ``lateCollect()`` method is responsible for storing the data it wants to give
63-
access to in a :class:`Symfony\\Component\\Profiler\\ProfileData\\ProfileDataInterface`.
43+
The ``getName()`` method must return a unique name. This is used to access the
44+
information later on (see :doc:`/cookbook/testing/profiling` for
45+
instance).
6446

6547
.. caution::
6648

6749
As the profiler serializes ProfileData instances, you should not
6850
store objects that cannot be serialized (like PDO objects), or you need
6951
to provide your own ``serialize()`` method.
7052

71-
Most of the time, it is convenient to extend
72-
:class:`Symfony\\Component\\Profiler\\DataCollector\\AbstractDataCollector` which already implements
73-
:class:`Symfony\\Component\\Profiler\\DataCollector\\DataCollectorInterface` and `setToken($token)` the only thing
74-
left to do is to decide when the data is collected::
53+
Example DataCollector::
7554

7655
class MemoryDataCollector extends AbstractDataCollector implements LateDataCollectorInterface
7756
{
@@ -92,14 +71,6 @@ left to do is to decide when the data is collected::
9271
{
9372
return new MemoryData(memory_get_peak_usage(true), $this->memoryLimit);
9473
}
95-
96-
/**
97-
* {@inheritdoc}
98-
*/
99-
public function getName()
100-
{
101-
return 'memory';
102-
}
10374
}
10475

10576
class MemoryData implements ProfileDataInterface
@@ -119,6 +90,14 @@ left to do is to decide when the data is collected::
11990
$this->memoryLimit = $this->convertToBytes($memoryLimit);
12091
}
12192

93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function getName()
97+
{
98+
return 'memory';
99+
}
100+
122101
/**
123102
* Returns the memory.
124103
*
@@ -143,6 +122,8 @@ left to do is to decide when the data is collected::
143122
}
144123

145124

125+
126+
146127
.. _data_collector_tag:
147128

148129
Enabling custom Data Collectors

cookbook/profiler/profiling_data.rst

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,37 @@ How to Access Profiling Data Programmatically
66

77
Most of the times, the profiler information is accessed and analyzed using its
88
web-based visualizer. However, you can also retrieve profiling information
9-
programmatically thanks to the methods provided by the ``profiler`` service.
10-
11-
When the response object is available, use the
12-
:method:`Symfony\\Component\\Profiler\\Profiler::loadFromResponse`
13-
method to access to its associated profile::
14-
15-
// ... $profiler is the 'profiler' service
16-
$profile = $profiler->loadFromResponse($response);
9+
programmatically thanks to the methods provided by the ``profiler.storage`` service.
1710

1811
When the profiler stores data about a request, it also associates a token with it;
1912
this token is available in the ``X-Debug-Token`` HTTP header of the response.
2013
Using this token, you can access the profile of any past response thanks to the
21-
:method:`Symfony\\Component\\Profiler\\Profiler::load` method::
14+
:method:`Symfony\\Component\\Profiler\\Storage\\ProfilerStorageInterface::read` method::
2215

2316
$token = $response->headers->get('X-Debug-Token');
24-
$profile = $container->get('profiler')->load($token);
17+
$profile = $container->get('profiler.storage')->read($token);
2518

2619
.. tip::
2720

2821
When the profiler is enabled but not the web debug toolbar, inspect the page
2922
with your browser's developer tools to get the value of the ``X-Debug-Token``
3023
HTTP header.
3124

32-
The ``profiler`` service also provides the
33-
:method:`Symfony\\Component\\Profiler\\Profiler::findBy` method to
25+
The ``profiler.storage`` service also provides the
26+
:method:`Symfony\\Component\\Profiler\\Storage\\ProfilerStorageInterface::findBy` method to
3427
look for tokens based on some criteria::
3528

3629
// get the latest 10 tokens
37-
$tokens = $container->get('profiler')->findBy(array(), 10, '', '');
30+
$tokens = $container->get('profiler.storage')->findBy(array(), 10, '', '');
3831

3932
// get the latest 10 tokens for all URL containing /admin/
40-
$tokens = $container->get('profiler')->findBy(array('url' => '/admin/'), 10, '', '');
33+
$tokens = $container->get('profiler.storage')->findBy(array('url' => '/admin/'), 10, '', '');
4134

4235
// get the latest 10 tokens for local requests
43-
$tokens = $container->get('profiler')->findBy(array('ip' => '127.0.0.1'), 10, '', '');
36+
$tokens = $container->get('profiler.storage')->findBy(array('ip' => '127.0.0.1'), 10, '', '');
4437

4538
// get the latest 10 tokens for requests that happened between 2 and 4 days ago
46-
$tokens = $container->get('profiler')->findBy(array(), 10, '4 days ago', '2 days ago');
39+
$tokens = $container->get('profiler.storage')->findBy(array(), 10, '4 days ago', '2 days ago');
4740

4841
Lastly, if you want to manipulate profiling data on a different machine than the
4942
one where the information was generated, use the ``profiler:export`` and

cookbook/testing/profiling.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ the ``test`` environment)::
3434
// check the number of requests
3535
$this->assertLessThan(
3636
10,
37-
$profile->getCollector('db')->getQueryCount()
37+
$profile->get('db')->getQueryCount()
3838
);
3939

4040
// check the time spent in the framework
4141
$this->assertLessThan(
4242
500,
43-
$profile->getCollector('time')->getDuration()
43+
$profile->get('time')->getDuration()
4444
);
4545
}
4646
}
@@ -52,7 +52,7 @@ finish. It's easy to achieve if you embed the token in the error message::
5252

5353
$this->assertLessThan(
5454
30,
55-
$profile->getCollector('db')->getQueryCount(),
55+
$profile->get('db')->getQueryCount(),
5656
sprintf(
5757
'Checks that query count is less than 30 (token %s)',
5858
$profile->getToken()

0 commit comments

Comments
 (0)
0