Discovering software bottlenecks is always a difficult task, but detecting them in Drupal can be a real nightmare. A simple contrib module can cause a lot of database queries, service instantiations or events to be triggered.
Classic debug tools like xDebug or XHProf fail to report those kinds of problems because they work at a lower level and they don't have any knowledge of Drupal internal structures.
Luckly Drupal 8 is built on Symfony 2 components and one of those components (the HTTP Kernel) provides the infrastructure for build custom profilers.
In this talk we'll see how to build a profiler to analyze the internal data structures of Drupal 8 and how to exend the profiler to add new data collectors.
The code is available as Drupal 8 module here: http://www.drupal.org/project/webprofiler
7. But wait… the Symfony profiler classes are declared in
the HttpKernel component, here:
core/vendor/symfony/http-kernel/Symfony/
Component/HttpKernel/Profiler
And the HttpKernel IS in Drupal 8, so what is missing?
8. Glue code is in
FrameworkBundle
and
WebProfilerBundle
But Symfony bundles doesn't works in Drupal 8 :-(
9. Two options:
1. make Symfony bundles works in Drupal 8
(www.drupal.org/node/1880196 -> won't fix)
2. move the bundle code to a Drupal module
We choose the Drupal way™
12. How does it work
Instrumented
(collect data and
store profile)
Request Response
<div id="webprofilerefa2f1"></div>
<script>
Webprofiler = (function () {
[…]
}
</script>
Response
• Add HTTP header -> X-Debug-Token: efa2f1
• Inject some javascript in the page:
13. How does it work
Profiling turned off
Ajax Request Response
/profiler/efa2f1
Returns only the
toolbar DOM (with
embedded
javascript, css and
icons)
14. Integration with XHProf/UProfiler
Drupal 8 version of XHProf module is compatibile with
Webprofiler (www.drupal.org/project/xhprof)
admin/config/development/profiler/configureadmin/config/development/xhprof
17. Integration with Drupal Console project
• Leverages the Symfony Console Component to provide a
powerful CLI
• Started as scaffolding generator now expose an
increasing number of commands to interact with a Drupal
8 installation
• Modules can contribute implementing new commands
drupalconsole.com
18. Implemented commands
• webprofiler:list -> list stored profiles
• webprofiler:export -> export stored profiles
• webprofiler:benchmark -> benchmark an URL running a
huge number of requests and compute statistics
• webprofiler:analyze -> analyze all stored profiles for a
specific route and compute statistics (not yet available on
drupal.org)
19. Benchmark an URL
• Anonymous user
console webprofiler:benchmark http://d8 —runs=10
• Logged-in user
console webprofiler:benchmark http://admin:admin@d8/admin/
people —runs=10
• Rebuild cache before benchmark
console webprofiler:benchmark http://d8 --runs=10 —cache-
rebuild
• Store results in a file
console webprofiler:benchmark http://d8 --runs=10 --file=/Users/
lussoluca/Desktop/benchmark.yml
20. Analyze a route
• Default front page
console webprofiler:analyze view.frontpage.page_1 GET
average
• User profile page
console webprofiler:analyze entity.user.canonical GET
average
21. Write a new datacollector
Contrib modules can provides new data collectors
• find a service to instrument
• write a new class to wrap/replace the original service
• alter the container to instrument the service
• write a new data collector class
• write a new template class
22. Functional testing
• We can mock the plugin.manager.mail service or the
http_client service, for example, but if we want to write a
functional test with a real integration?
• Maybe we want to test how much time is spent or how
much memory is consumed
We can use data profiled by Webprofiler in our assertions
symfony.com/doc/current/cookbook/testing/profiling.html
23. Contributors
• Daniel Wehner (dawehner) - co-mantainer
• Eric Sod (esod)
• willzyx
• Francesco Caspani (fcaspani)
• Jesus Manuel Olivas (jmolivas)
24. We need a huge help on
• UX
• Javascript (Backbone) - www.drupal.org/node/2468007
• Statistical analysis
• Documentation
So please contact us!