On the web, every second counts. Studies have shown that a 1 second delay in load time can cost a mid-sized eCommerce company $2.5 million per year in lost revenue. Let’s look at what Magento 2 has done to improve performance and how we can take things a step further to ensure the Magento 2 sites we build and maintain are well designed, well written and very, very fast.
Presented at php[world] 2016.
3. @JoshuaSWarren #phpworld
About Me
• PHP-Based Ecommerce Developer Since 1999
• Magento Developer Since 2008; Magento 2 Dev Since 2014
• Magento Master
• Founder of Creatuity, Magento Enterprise Solution Partner
26. @JoshuaSWarren #phpworld
Ensure Your Environment is Well Configured
• Utilize Redis for cache & session storage
• If appropriate, split your web server and database onto
separate servers
• Run PHP 7
31. @JoshuaSWarren #phpworld
The Right Way
• Provides the desired functionality
• Minimizes technical debt and maintenance costs
• Does not reduce or slow site performance
32. @JoshuaSWarren #phpworld
Ivan’s Rules
• Ivan Chepurnyi, Magento performance expert
• https://ivanchepurnyi.github.io
• Ivan has four rules that changed my (dev) life
33. @JoshuaSWarren #phpworld
Ivan’s Rules
• Minimize amount of I/O operations to bare minimum
• Make I/O operations as lightweight as possible
• Spend time on requirement analysis
• Analyze the possible data impact
37. @JoshuaSWarren #phpworld
Efficient MySQL Usage
• Avoid full table scans, avoid temporary tables and avoid the
join buffer
• Multiple simple queries tend to be faster overall than one
large, complex query
• Think about the underlying database usage triggered by the
code you write
38. @JoshuaSWarren #phpworld
Don’t Be Afraid
• Don’t be afraid to create new tables when needed
• Creating a new table to contain the data you need can be
faster than triggering complex queries and JOINs
• Don’t be afraid to try something new - the core code doesn’t
always use the fastest approach
39. @JoshuaSWarren #phpworld
Don’t Be Afraid
• Don’t fear MySQL. The more you learn about MySQL, query
optimization and how MySQL executes queries, the better
you will become at performance optimization
46. @JoshuaSWarren #phpworld
Code Profiler
• No, not the late 90’s TV show
• A tool to measure the performance of your PHP code,
including the time it takes to run and the resources it
consumes
• There are a many options for profiling your code
51. @JoshuaSWarren #phpworld
Blackfire.io
• I’ve chosen Blackfire.io as my profiler of choice
• I recommend it, but these techniques will work with any
profiler, some just make it easier than others
• Blackfire provides substantially more than what I can show
today, so today we’ll just focus on performance profiling
54. @JoshuaSWarren #phpworld
Reviewing A Profile
• Total load time is 4 minutes 57 seconds
• One call makes up 4 minutes 54 seconds - PDOStatement:execute
select main_table.*, ... from sales_flat_order_grid as main_table left
join sales_order_custom on sales_order_custom.order_id =
main_table.entity_id left join orderarchive as orderarchive_tbl on
orderarchive_tbl.order_id = main_table.entity_id where
(orderarchive_tbl.order_group_id is null or
orderarchive_tbl.order_group_id = ?) order by created_at desc limit ?
55. @JoshuaSWarren #phpworld
Reviewing A Profile
• I/O - database I/O is our bottleneck
• We’re breaking Ivan’s rules - lots of I/O, and it’s not
lightweight
• In this case, we discovered a simple solution - not only were
there multiple LEFT JOINs, they were being performed with
no indexes!
60. @JoshuaSWarren #phpworld
Build a Culture & Process of Performance
• Each new commit, pull request or build should trigger a
profiling run of all key pages
• Any significant decreases in performance should trigger a
failed build alert
61. @JoshuaSWarren #phpworld
Build a Culture & Process of Performance
• Spend time talking about performance when designing
solutions
• Share your experience and the trends you notice with your
teammates and other Magento developers
62. @JoshuaSWarren #phpworld
Writing Fast Magento 2 Code
• Avoid the database as much as possible
• When database access is required, use simple, well-indexed
queries
• If you see a need to access the filesystem…
63.
64. @JoshuaSWarren #phpworld
When Slow I/O Can’t Be Avoided
• Move it to a background or periodic process
• Utilize as much caching as possible
• Design it in such a way that it impacts as few sessions as
possible
• Major difference between slowing down every page view on
a site versus slowing down every cart completion
66. @JoshuaSWarren #phpworld
Advanced M2 Optimization
• The following tips are thanks to Max Pronko - https://
www.maxpronko.com/ - @max_pronko
• Based on Max’s real world experience as CTO of a merchant
that uses Magento 2
67. @JoshuaSWarren #phpworld
Advanced M2 Optimization
• Disable unused core modules - for instance, if you don’t
offer downloadable products, disable
Magento_Downloadable
• Disable reports and features you don’t use
• Remove any blocks you aren’t using from your theme
68. @JoshuaSWarren #phpworld
Advanced M2 Optimization
• If your site only offers one language, disable inline
translation
• Move as many of your scripts as possible to load async
70. @JoshuaSWarren #phpworld
php[world] sessions
• DCPHP User Group Meeting: PHP Performance Profiling
Using Blackfire
• Tonight, 7PM, Ash Grove B
• Magento 2 Development Best Practices
• Friday, 10AM, Ash Grove A
71. @JoshuaSWarren #phpworld
Future Events
• Meet Magento World - Online Conference in December -
http://meet-magento.com/conference/meet-magento-world/
• Magento 2 Performance Training - January 18th-20th in
Orlando with Ivan Chepurnyi - http://bit.ly/2eAo8cz
• Magento Imagine 2017 - April 3rd-5th in Las Vegas -
imagine.magento.com