|  | 
|  | 1 | +.. index:: | 
|  | 2 | + single: Profiling; Profiling data | 
|  | 3 | + | 
|  | 4 | +How to Access Profiling Data Programmatically | 
|  | 5 | +============================================= | 
|  | 6 | + | 
|  | 7 | +Most of the times, the profiler information is accessed and analyzed using its | 
|  | 8 | +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\\HttpKernel\\Profiler\\Profiler::loadProfileFromResponse` | 
|  | 13 | +method to access to its associated profile:: | 
|  | 14 | + | 
|  | 15 | + $profile = $container->get('profiler')->loadProfileFromResponse($response); | 
|  | 16 | + | 
|  | 17 | +When the profiler stores data about a request, it also associates a token with it; | 
|  | 18 | +this token is available in the ``X-Debug-Token`` HTTP header of the response. | 
|  | 19 | +Using this token, you can access the profile of any past response thanks to the | 
|  | 20 | +:method:`Symfony\\Component\\HttpKernel\\Profiler\\Profiler::loadProfile` method:: | 
|  | 21 | + | 
|  | 22 | + $token = $request->headers->get(X-Debug-Token); | 
|  | 23 | + $profile = $container->get('profiler')->loadProfile($token); | 
|  | 24 | + | 
|  | 25 | +.. tip:: | 
|  | 26 | + | 
|  | 27 | + When the profiler is enabled but not the web debug toolbar, use a tool like | 
|  | 28 | + Firebug to get the value of the ``X-Debug-Token`` HTTP header. | 
|  | 29 | + | 
|  | 30 | +The ``profiler`` service also provides the | 
|  | 31 | +:method:`Symfony\\Component\\HttpKernel\\Profiler\\Profiler::find` method to | 
|  | 32 | +look for tokens based on some criteria:: | 
|  | 33 | + | 
|  | 34 | + // get the latest 10 tokens | 
|  | 35 | + $tokens = $container->get('profiler')->find('', '', 10, '', ''); | 
|  | 36 | + | 
|  | 37 | + // get the latest 10 tokens for all URL containing /admin/ | 
|  | 38 | + $tokens = $container->get('profiler')->find('', '/admin/', 10, '', ''); | 
|  | 39 | + | 
|  | 40 | + // get the latest 10 tokens for local requests | 
|  | 41 | + $tokens = $container->get('profiler')->find('127.0.0.1', '', 10, '', ''); | 
|  | 42 | + | 
|  | 43 | + // get the latest 10 tokens for requests that happened between 2 and 4 days ago | 
|  | 44 | + $tokens = $container->get('profiler') | 
|  | 45 | + ->find('', '', 10, '4 days ago', '2 days ago'); | 
|  | 46 | + | 
|  | 47 | +Lastly, if you want to manipulate profiling data on a different machine than the | 
|  | 48 | +one where the information were generated, use the ``profiler:export`` and | 
|  | 49 | +``profiler:import`` commands: | 
|  | 50 | + | 
|  | 51 | +.. code-block:: bash | 
|  | 52 | +
 | 
|  | 53 | + # on the production machine | 
|  | 54 | + $ php app/console profiler:export > profile.data | 
|  | 55 | +
 | 
|  | 56 | + # on the development machine | 
|  | 57 | + $ php app/console profiler:import /path/to/profile.data | 
|  | 58 | +
 | 
|  | 59 | + # you can also pipe from the STDIN | 
|  | 60 | + $ cat /path/to/profile.data | php app/console profiler:import | 
0 commit comments