magento performance issues? deep dive - l fowell.pdfa 1 second delay can equate to.. • 16%...

43

Upload: others

Post on 19-Mar-2020

2 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store
Page 2: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

MAGENTO PERFORMANCE ISSUES?

FIX them, don’t MASK them

Page 3: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

INTRODUCTION

Luke FowellHead of eCommerce

Page 4: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

BOURNEMOUTH

Bournemouth is the fastest

growing digital economy in UK

Page 5: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

WHY IS MAGENTO OFTEN SLOW?

• It’s a heavy eCommerce platform

• Resource hungry

• Wealth of features makes for a

large codebase

• The EAV model is expensive

• Flexibility #1, Performance #2

Page 6: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

WHY DOES IT MATTER IF MY STORE IS SLOW?

• Lower revenue

• Lower conversion rate

• Increase in bounce rate

• Increase in basket abandonment

• Decrease in customer retention

• Negative impact on reputation

• Negative impact on search rankings

Page 7: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

A 1 SECOND DELAY CAN EQUATE TO..

• 16% decrease in customer

satisfaction

• 7% loss in conversion

• This means that a store with

a turnover of £1000 per day

would lose £25,000 per year

• This is equivalent to Amazon

losing $1.6 billion in sales

each year

Page 8: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

STATS ON PERFORMANCE

57% of visitors abandon a site after waiting

more than 3 seconds for a page to load

[Source: http://www.marketingprofs.com/]

Page 9: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

QUICK WINS YOU GET FOR FREE

• Enable Caching

• Flat Product & Category Enabled

• Compilation

• Merge JS & CSS

• Database Log Cleaning

• Disable Debug Logging

Page 10: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

SOME THOUGHTS ON ENVIRONMENT

• Less preference on software choice

• Software set up properly

• Community advice & guides

• Performance “buzzwords”

• A harmonious system, software working

together

Page 11: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

SO WHERE IS THE PROBLEM?

• We have found approximately 75% of performance issues are related to code

• Often Introduced by a developer during implementation

• Usually relate to some kind of database interaction

Application Level

Page 12: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS

This is one of the most common culprits of performance

bottlenecks in Magento

Doing anything memory intensive inside a loop is

undoubtedly going to be amplified

Therefore you need to be meticulous about keeping code

lean when inside a loop

Page 13: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 1

foreach($this->getProductCollection() as $bestseller)

{

$_product = Mage::getModel(‘catalog/product’)->load($bestseller->getId());

echo $_product->getSubtitle();

}

Assuming getProductCollection()returns 12 products

This will result in 13 unnecessary database calls

Page 14: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 1

$_bestseller_products = $this->getProductCollection();

$_bestseller_products->addAttributeToSelect(‘subtitle’);

foreach($_bestseller_products as $bestseller)

{

echo $bestseller->getSubtitle();

}

This time it only makes 1 database call

Page 15: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS– EXAMPLE 1

Page 16: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 2

Model.

class Wcm_Demo_Model_Message extends Mage_Core_Model_Abstract

{

public function markAsSeen(){

$this->getResource()->updateViewedStatus($this->getMessageId(), 1);

return $this;

}

}

Resource Model.

class Wcm_Demo_Model_Resource_Message extends Mage_Core_Model_Resource_Db_Abstract

{

public function updateViewedStatus($messageId, $status){

$data = array(‘message_id’ => $messageId, ‘status’ => $status);

$adapter->insert($this->getMainTable(), $data);

}

}

Page 17: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 2

foreach($this->getMessageCollection() as $message)

{

echo $message->markAsSeen()->toHtml();

}

This code will mark each message as “seen” when it outputs it to the page

Assuming there are 10 messages, that will result in 11 database queries

Page 18: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 2

Model.

class Wcm_Demo_Model_Message extends Mage_Core_Model_Abstract

{

public function markAsSeen($items = null){

if(is_array($items))

{

$this->getResource()->updateViewedStatuses($items, 1);

}

else

{

$this->getResource()->updateViewedStatus($this->getMessageId(), 1);

}

return $this;

}

}

Page 19: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 2

Resource Model.

class Wcm_Demo_Model_Resource_Message extends Mage_Core_Model_Resource_Db_Abstract

{

public function updateViewedStatus($messageId, $status){

$data = array(‘message_id’ => $messageId, ‘status’ => $status);

$adapter->insert($this->getMainTable(), $data);

}

public function updateViewedStatuses($messageIds, $status){

$data = array();

foreach($messageIds as $messageId){

$data[] = array(‘message_id’ => $messageId, ‘status’ => $status);

}

$adapter->insertMultiple($this->getMainTable(), $data);

}

}

Page 20: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 2

$seen = array();

foreach($this->getMessageCollection() as $message)

{

echo $message->toHtml();

$seen[] = $message->getMessageId();

}

Mage::getModel(‘demo/message’)->markAsSeen($seen);

Inserting the data has been aggregated into a single database

round-trip

Page 21: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

$products = Mage::getModel(‘catalog/product’)->getCollection()

->setStoreId(Mage::app()->getStore()->getId())

->addFieldToFilter(‘is_featured’, 1);

Mage::getSingleton(‘catalog/product_status’)

->addVisibleFilterToCollection($products);

Mage::getSingleton(‘catalog/product_visibility’)

->addVisibleInCatalogFilterToCollection($products);

return $products->getFirstItem()->getSku();

LOOPS / COLLECTIONS – EXAMPLE 3

Loading full product collection to return a single item

Page 22: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 3

$products = Mage::getModel(‘catalog/product’)->getCollection()

->setStoreId(Mage::app()->getStore()->getId())

->addFieldToFilter(‘is_featured’, 1)

->setPageSize(1);

Mage::getSingleton(‘catalog/product_status’)

->addVisibleFilterToCollection($products);

Mage::getSingleton(‘catalog/product_visibility’)

->addVisibleInCatalogFilterToCollection($products);

return $products->getFirstItem()->getSku();

Applying a limitation will improve code performance

Page 23: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

LOOPS / COLLECTIONS – EXAMPLE 3

Approximate performance improvement based on 500 products

38.4% decrease in load time

Page 24: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

USE OF THE LOAD METHOD - EXAMPLE 1

<?php $productId = Mage::app()->getRequest()->getParam('id'); ?>

<div class="product">

<a href="<?php echo Mage::getModel('catalog/product')->load($productId)->getUrl(); ?>">

<div class="rollover">

<div class="rollover-container">

<h5 class="txt-white">

<?php echo Mage::getModel('catalog/product')->load($productId)->getName(); ?>

</h5>

<hr class="bg-white">

<span class="txt-white">

<?php echo Mage::getModel('catalog/product')->load($productId)->getSubtitle(); ?>

</span>

</div>

</div>

<img src="<?php echo Mage::getModel('catalog/product')->load($productId)->getImageUrl(); ?>"

/>

</a>

</div>

Page 25: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

USE OF THE LOAD METHOD - EXAMPLE 1

<?php

$productId = Mage::app()->getRequest()->getParam('id');

$product = Mage::getModel(‘catalog/product’)->load($productId);

?>

<div class="product">

<a href="<?php echo $product->getUrl(); ?>">

<div class="rollover">

<div class="rollover-container">

<h5 class="txt-white">

<?php echo $product->getName(); ?>

</h5>

<hr class="bg-white">

<span class="txt-white">

<?php echo $product->getSubtitle(); ?>

</span>

</div>

</div>

<img src="<?php echo $product->getImageUrl(); ?>" />

</a>

</div>

Page 26: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

Page 27: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

<?php $i=0; foreach ($_productCollection as $_product): ?>

<?php if($_product->getTypeId() == 'configurable'): ?>

<?php $_type = $_product->getTypeInstance(true); ?>

<?php foreach($_type->getUsedProducts(null, $_product) as $_used_product): ?>

<?php

$_size_id = $_used_product->getSize();

$_size_label = $_used_product->getAttributeText(‘size’);

$_sizes[$_size_id] = $_size_label;

?>

<?php endforeach; ?>

<?php krsort($_sizes); ?>

<p><?php echo $this->__('Available in: %s', implode(', ', $_sizes)); ?></p>

<?php endif; ?>

<?php endforeach ?>

Page 28: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

With 5 Configurable Products, each with 5 associated simple products

Page 29: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

<?php $i=0; foreach ($_productCollection as $_product): ?>

<?php if($_product->getTypeId() == 'configurable'): ?>

<?php foreach($_product->getChildrenProducts() as $_used_product): ?>

<?php

$_size_id = $_used_product->getSize();

$_size_label = $_used_product->getAttributeText(‘size’);

$_sizes[$_size_id] = $_size_label;

?>

<?php endforeach; ?>

<?php krsort($_sizes); ?>

<p><?php echo $this->__('Available in: %s', implode(', ', $_sizes)); ?></p>

<?php endif; ?>

<?php endforeach ?>

Page 30: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE – CONFIG.XML

<config>

<frontend>

<events>

<catalog_block_product_list_collection>

<observers>

<add_sizes_to_list>

<class>wcm_demo/observer</class>

<method>productListCollectionLoadAfter</method>

</add_sizes_to_list>

</observers>

</catalog_block_product_list_collection>

</events>

</frontend>

</config>

Page 31: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE – OBSERVER.PHP

public function productListCollectionLoadAfter(Varien_Event_Observer $observer)

{

$products = $observer->getCollection();

$productIds = array();

foreach($products->getItems() as $product) {

$productIds[] = $product->getId();

}

$collection = Mage::getResourceModel(‘catalog/product_type_configurable_product_collection’);

$collection->addStoreFilter(Mage::app()->getStore()->getId())

->addAttributeToSelect(array(‘size’));

$collection->getSelect()->where(‘link_table.parent_id in (?)’, $productIds);

foreach($collection as $childProduct) {

foreach($childProduct->getParentIds() as $parentId) {

if (!isset($mapping[$parentId])) $mapping[$parentId] = array();

$mapping[$parentId][] = $childProduct;

}

}

foreach($mapping as $parentId => $childrenProducts) {

$products[$parentId]->setChildrenProducts($childrenProducts);

}

}

Page 32: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

Page 33: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

REAL LIFE EXAMPLE

Same output, except in 0.0024 seconds instead of 0.48

An improvement of 19900%

Page 34: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

PHP CODE OPTIMISATIONS

• Use single quotes rather than double quotes

• Use === instead of ==

• Disable debugging messages

• Use JSON over XML where possible

• Use isset() over count(), strlen() or sizeof()

• Use if/else statements over switch statements

Page 35: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

PHP CODE OPTIMISATIONS

Maintainability > Micro Optimisations

Page 36: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

PHP CODE OPTIMISATIONS

“ We should forget about small efficiencies, say about 97% of the

time: premature optimisation is the root of all evil”

Donald Knuth

Page 37: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

Profilers

FINDING YOUR PERFORMANCE ISSUES

Xhprof

Built-in

Profiler

Xdebug

Built-in

ProfilerBlackfire.io

Page 38: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

FINDING YOUR PERFORMANCE ISSUES

Magento’s built in profiler

Page 39: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

FINDING YOUR PERFORMANCE ISSUES

Try Aoe_Profiler (http://bit.ly/aoeprofiler) by Fabrizio Branca

Page 40: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

FINDING YOUR PERFORMANCE ISSUES

Only optimize after measuring & profiling

Otherwise you will almost certainly optimize the wrong

things

Page 41: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

TAKEAWAYS

• Simple things first

• Focus on quality code

• Consider performance

implications

• Refactor your code

• Focus solely on

performance “buzzwords”

• Cover up performance

issues

• Optimise without

measuring and profiling

DO DON’T

Page 42: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store

THANK YOU

@lukefowell / @weclickmedia

[email protected]

Page 43: MAGENTO PERFORMANCE ISSUES? Deep Dive - L Fowell.pdfA 1 SECOND DELAY CAN EQUATE TO.. • 16% decrease in customer satisfaction • 7% loss in conversion • This means that a store