未加星标

How we sped up Sylius' Behat suite with Blackfire

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Feedback time is one of the most crucial factors during development and the red - green - refactor cycle. In case of Sylius, the full build used to take two and a half hour, including 55 minutes for onlyphp 5.6 jobs. Waiting so long for feedback is not an option for a project of this size and with so many active contributors. As much as I am Xdebug fan, I have never really used it for profiling - the snapshots generation was slowing down the profiled script significantly and resulted in monstrous files, which weren't easy to read.

Half a year after I have first heard of blackfire.io I decided to give it a try. It resulted in a series of pull requests that speeds up Sylius test suites 6 times and reduces memory usage to one tenth . As a side-effect, we have revealed bugs that can be found only in production-like environment (e.g. displaying 404 page or Doctrine metadata inheritance and slug generation ).

The previous tests execution time was unacceptable and impeded the development. The community was struggling with this issue for some time, but it was hard to find an inspiration in speeding up tests execution, because there is no other open source project that have such a big number of Behat scenarios. After optimizations, we have full build finished just 10 minutes after PR is sent (taking into account that jobs run in parallel on Travis). It will have a huge impact on development tempo and will strongly push the project forward. You can check how the previous builds look in comparison to the new ones .

Preventing bottlenecks Running profiler for the first time

When I ran the Blackfire profiler for the first time, it took a little less than 7 minutes to test Behat products suite. While I was looking at the profiling results , I noticed that some parts of the application were much more time consuming than I would have expected.

First bottleneck: Logging in using web interface

The most interesting was that iAmLoggedInAsRole method, used once in literally every scenario, took a little over 30% of the execution time.


How we sped up Sylius' Behat suite with Blackfire

That code used to make 4 requests: getting the login form, checking credentials (done by firewall), redirecting to dashboard and finally getting the dashboard. While using Behat/Symfony2Extension , the HttpKernel is rebooted after every request and scenario.

To solve this issue, I rewrote the method not to make any HTTP requests. With the awareness of Symfony2 ContextListener setting present token based on the one in session, it wasn't that hard as I would expect.

That change sped up our products suite by over 2 minutes and lowered memory usage by 40%.

Have a look at Blackfire profiles before and after this change and their comparison .

Second bottleneck: Reinitializng router after every request and scenario

We use router service in our Behat suite widely. As I mentioned above, the cotainer and its services are rebooted after every request and scenario. Since router has no cache that can be persisted between different instances, the routes were loaded from YAML files many more times than needed. The fix was quite simple - get the first instance of router and use it all the time.

Assuming that the routes aren't changed during the runtime, this update was 100% safe. Another amount of time was saved, setting our current checkpoint at about 3 minutes 30 seconds, which is half of the inital time.

Have a look at Blackfire profiles before and after this change and their comparison .

Third bottleneck: Running Doctrine without class metadata cache

At that moment, getting class metadata was taking almost half of the execution time. Metadata was loaded for over eight thousand times! We are purging the database before every scenario to be sure that tests are executed as independent as they can be, that was responisble for one fifth of the class metadata-related resources usage too. After setting up Doctrine metadata cache and decorating ORMPurger with caching layer, our products suite was finishing in 1 minute 30 seconds, compared to 7 minutes at the beginning.

Have a look at Blackfire profiles before and after this change and their comparison .

Profiling results

Removing these three main bottlenecks caused insane improvement in resources usage. Take a look at the comparison profile before all the changes and after them:

Few more changes that will make your tests faster

After these tiny in code, but giant in effects changes, the other ones weren't so spectacular. I ended up with products suite executing in 30 seconds on Travis CI, which I am really proud of. The other changes included:

Turning off kernel.debug setting in test environment Enabling OPCache Not executing scripts while using Composer Tweaking Travis cache not to upload every time Applying previous changes to PHP7 builds (full build under 5 minutes) Summary

Since Sylius got these performance optimizations merged, it is unlikely to wait more than a few minutes to see feedback, like it was before. Running our full Behat suite on PHP 7 takes 2:30min for almost 500 scenarios and 5600 steps (37 steps per second!), executes 110 thousands queries, creates 30 thousands entities, boots Symfony2 Kernel over 2 thousand times and uses only 115MB memory !

Previous build (2h 33m):


How we sped up Sylius' Behat suite with Blackfire

Current builds (26m):


How we sped up Sylius' Behat suite with Blackfire

If you want to see how it was implemented, take a look at the related PRs involving #3576 (focused on Behat execution time) , #3596 (improved caching logic on Travis) and #3625 (reduced memory usage a lot) .

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHP
分页:12
转载请注明
本文标题:How we sped up Sylius' Behat suite with Blackfire
本站链接:http://www.codesec.net/view/482162.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(25)