Magento 2 Performance: Every Second Counts
-
Upload
joshua-warren -
Category
Software
-
view
426 -
download
0
Transcript of Magento 2 Performance: Every Second Counts
![Page 1: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/1.jpg)
20162016
![Page 2: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/2.jpg)
@JoshuaSWarren #phpworld
Magento 2 Performance: Every Second Counts
![Page 3: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/3.jpg)
@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
![Page 4: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/4.jpg)
@JoshuaSWarren #phpworld
Performance Matters
![Page 5: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/5.jpg)
@JoshuaSWarren #phpworld
47% of consumers expect a page to load in 2 seconds
![Page 6: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/6.jpg)
@JoshuaSWarren #phpworld
40% abandon a website that takes more than 3 seconds to load
![Page 7: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/7.jpg)
@JoshuaSWarren #phpworld
Each 1 second delay causes at least a 7% drop in conversions
![Page 8: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/8.jpg)
@JoshuaSWarren #phpworld
If a site sells $1,000 per day, a 1 second delay could cost $25,000/year
![Page 9: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/9.jpg)
@JoshuaSWarren #phpworld
If a site sells $100,000 a day, a 1 second delay can cost $2.5 million/year
![Page 10: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/10.jpg)
@JoshuaSWarren #phpworld
Improving the load time for mobile users by 1 second can increase
conversion rates 27%
![Page 11: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/11.jpg)
@JoshuaSWarren #phpworld
Load time is money
![Page 12: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/12.jpg)
@JoshuaSWarren #phpworld
If a bug caused 40% of visitors to leave, should you fix it?
![Page 13: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/13.jpg)
@JoshuaSWarren #phpworld
Poor performance is a bug.
![Page 14: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/14.jpg)
@JoshuaSWarren #phpworld
And it’s a bug for developers to solve.
![Page 15: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/15.jpg)
@JoshuaSWarren #phpworld
Magento 2 Performance Out of the Box
![Page 16: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/16.jpg)
![Page 17: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/17.jpg)
![Page 18: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/18.jpg)
![Page 19: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/19.jpg)
![Page 20: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/20.jpg)
@JoshuaSWarren #phpworld
Given the budget, Magento 2 can scale to any level and remain very, very fast
![Page 21: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/21.jpg)
@JoshuaSWarren #phpworld
Complex customizations, if not handled properly, can slow any site down
significantly
![Page 22: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/22.jpg)
@JoshuaSWarren #phpworld
Infrastructure Matters
![Page 23: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/23.jpg)
@JoshuaSWarren #phpworld
All the performance optimizations in the world can’t compensate for bad hardware
![Page 24: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/24.jpg)
![Page 25: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/25.jpg)
@JoshuaSWarren #phpworld
Please don’t host your ecommerce site on $5/month discount hosting
![Page 26: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/26.jpg)
@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
![Page 27: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/27.jpg)
@JoshuaSWarren #phpworld
Now that your infrastructure is optimized…
![Page 28: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/28.jpg)
@JoshuaSWarren #phpworld
Performance is a State of Mind
![Page 29: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/29.jpg)
@JoshuaSWarren #phpworld
There are thousands of ways to implement a feature
![Page 30: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/30.jpg)
@JoshuaSWarren #phpworld
Take the time to find the right way
![Page 31: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/31.jpg)
@JoshuaSWarren #phpworld
The Right Way
• Provides the desired functionality
• Minimizes technical debt and maintenance costs
• Does not reduce or slow site performance
![Page 32: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/32.jpg)
@JoshuaSWarren #phpworld
Ivan’s Rules
• Ivan Chepurnyi, Magento performance expert
• https://ivanchepurnyi.github.io
• Ivan has four rules that changed my (dev) life
![Page 33: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/33.jpg)
@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
![Page 34: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/34.jpg)
@JoshuaSWarren #phpworld
In other words…
![Page 35: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/35.jpg)
@JoshuaSWarren #phpworld
Performance problems tend to stem from too much I/O or I/O that’s too slow
![Page 36: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/36.jpg)
@JoshuaSWarren #phpworld
In Magento 2, generally I/O = MySQL database operations
![Page 37: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/37.jpg)
@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
![Page 38: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/38.jpg)
@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
![Page 39: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/39.jpg)
@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
![Page 40: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/40.jpg)
@JoshuaSWarren #phpworld
But what if you didn’t write the code on a site that’s running slowly?
![Page 41: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/41.jpg)
![Page 42: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/42.jpg)
@JoshuaSWarren #phpworld
Real World Examples
![Page 43: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/43.jpg)
@JoshuaSWarren #phpworld
Finding Performance Issues
![Page 44: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/44.jpg)
@JoshuaSWarren #phpworld
You’ll need a profiler…
![Page 45: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/45.jpg)
![Page 46: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/46.jpg)
@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
![Page 47: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/47.jpg)
@JoshuaSWarren #phpworld
Profiling Options
• xdebug
• xhprof
• blackfire.io
• There are many options, but…
![Page 48: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/48.jpg)
@JoshuaSWarren #phpworld
Rifleman’s Creed
This is my rifle. There are many like it, but this one is mine.
Without me, my rifle is useless. Without my rifle, I am useless.
![Page 49: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/49.jpg)
@JoshuaSWarren #phpworld
Hunting For Slow Code…
![Page 50: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/50.jpg)
@JoshuaSWarren #phpworld
Profiler’s Creed
This is my profiler. There are many like it, but this one is mine.
Without me, my profiler is useless. Without my profiler, I am useless.
![Page 51: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/51.jpg)
@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
![Page 52: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/52.jpg)
@JoshuaSWarren #phpworld
What can Blackfire Do?• How about point you to a way to make a page 99% faster?
• Before:
• After:
![Page 53: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/53.jpg)
@JoshuaSWarren #phpworld
Let’s review the profiles in detail…
![Page 54: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/54.jpg)
@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 ?
![Page 55: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/55.jpg)
@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!
![Page 56: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/56.jpg)
@JoshuaSWarren #phpworld
Let’s review the ‘after’ profile now…
![Page 57: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/57.jpg)
@JoshuaSWarren #phpworld
Reviewing A Profile
• Reduced the time needed for this SQL query from 4 minutes 54 seconds to 550 milliseconds.
![Page 58: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/58.jpg)
@JoshuaSWarren #phpworld
Adventures in Magento 2 profiling on conference wifi…
![Page 59: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/59.jpg)
@JoshuaSWarren #phpworld
Writing Fast Magento 2 Code
![Page 60: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/60.jpg)
@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
![Page 61: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/61.jpg)
@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
![Page 62: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/62.jpg)
@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…
![Page 63: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/63.jpg)
![Page 64: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/64.jpg)
@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
![Page 65: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/65.jpg)
@JoshuaSWarren #phpworld
Test and profile multiple potential implementations.
![Page 66: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/66.jpg)
@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
![Page 67: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/67.jpg)
@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
![Page 68: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/68.jpg)
@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
![Page 69: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/69.jpg)
@JoshuaSWarren #phpworld
Homework
![Page 70: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/70.jpg)
@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
![Page 71: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/71.jpg)
@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
![Page 72: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/72.jpg)
@JoshuaSWarren #phpworld
Remember that performance is a state of mind
![Page 73: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/73.jpg)
@JoshuaSWarren #phpworld
Poor performance is a bug
![Page 74: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/74.jpg)
@JoshuaSWarren #phpworld
Go try a profiler on a Magento site, identify one performance problem and
solve it
![Page 75: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/75.jpg)
@JoshuaSWarren #phpworld
Tweet at me @JoshuaSWarren with what the problem was and how you solved it.
I bet it’s I/O related…
![Page 76: Magento 2 Performance: Every Second Counts](https://reader034.fdocuments.us/reader034/viewer/2022051404/5871af311a28abda6a8b63e9/html5/thumbnails/76.jpg)
@JoshuaSWarren #phpworld
Q&A