Last active
December 6, 2018 12:56
-
-
Save aadmathijssen/1dbb529bd856fa24c360713a09bafb72 to your computer and use it in GitHub Desktop.
Magento shell script used for analyzing the performance of the Emico_Tweakwise_Helper_Data::getFilterCategory method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ISAAC Category Load Tester | |
* | |
* @category ISAAC | |
* @package ISAAC_Category | |
* @copyright Copyright (c) 2018 ISAAC Software Solutions B.V. (https://www.isaac.nl) | |
* @license proprietary | |
* @author ISAAC Software Solutions B.V. (https://www.isaac.nl) | |
*/ | |
require_once 'abstract.php'; | |
class ISAAC_Category_Load_Tester extends Mage_Shell_Abstract | |
{ | |
public function run() | |
{ | |
$loadInBulk = false; | |
$useFlatTable = false; | |
$restrictedAttributes = false; | |
$addUrlRewrite = false; | |
if (!$this->getArg('category')) { | |
echo 'category is required ' . PHP_EOL; | |
$this->usageHelp(); | |
return; | |
} | |
$categoryId = $this->getArg('category'); | |
$category = Mage::getModel('catalog/category')->load($categoryId); | |
if (!$category->getId()) { | |
echo sprintf('error: could not find category with id %s', $categoryId) . PHP_EOL; | |
return; | |
} | |
if ($this->getArg('load-in-bulk')) { | |
$loadInBulk = true; | |
} | |
if ($this->getArg('use-flat-table')) { | |
$useFlatTable = true; | |
} | |
if ($this->getArg('restricted-attributes')) { | |
$restrictedAttributes = true; | |
} | |
if ($this->getArg('add-url-rewrite')) { | |
$addUrlRewrite = true; | |
} | |
Mage::app()->setCurrentStore(1); | |
$time = microtime(true); | |
$categoryIds = $category->getResource()->getChildren($category); | |
if ($loadInBulk) { | |
$this->printCategories( | |
$this->getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite) | |
); | |
} else { | |
foreach ($categoryIds as $categoryId) { | |
$this->printCategories( | |
$this->getCategoriesByIds($categoryId, $useFlatTable, $restrictedAttributes, $addUrlRewrite) | |
); | |
} | |
} | |
echo sprintf( | |
'Loading categories %s with %s attributes took %s seconds', | |
$loadInBulk ? 'in bulk' : 'one by one', | |
$restrictedAttributes ? 'restricted' : 'all', | |
microtime(true) - $time | |
) . PHP_EOL; | |
return; | |
} | |
protected function getCategoriesByIds($categoryIds, $useFlatTable, $restrictedAttributes, $addUrlRewrite) | |
{ | |
if ($useFlatTable) { | |
$categoryCollection = Mage::getModel('catalog/category')->getCollection(); | |
} else { | |
$categoryCollection = Mage::getResourceModel('catalog/category_collection'); | |
} | |
if ($restrictedAttributes) { | |
$categoryCollection->addAttributeToSelect('url_key'); | |
$categoryCollection->addAttributeToSelect('include_in_menu'); | |
} else { | |
$categoryCollection->addAttributeToSelect('*'); | |
} | |
$categoryCollection->addIdFilter($categoryIds); | |
if ($addUrlRewrite) { | |
$categoryCollection->addUrlRewriteToResult(); | |
} | |
return $categoryCollection->getItems(); | |
} | |
protected function printCategories($categories) | |
{ | |
foreach ($categories as $category) { | |
echo sprintf( | |
'category %d (%s, parent %d, %s): %s', | |
$category->getId(), | |
$category->getUrlKey(), | |
$category->getParentId(), | |
($category->getIncludeInMenu() ? 'included in menu' : 'not included in menu'), | |
$category->getUrl() | |
) . PHP_EOL; | |
} | |
} | |
public function usageHelp() | |
{ | |
global $argv; | |
echo $argv[0] . ' --category CATEGORY_ID [--load-in-bulk] [--use-flat-table] [--restricted-attributes] [--add-url-rewrite]' . PHP_EOL; | |
echo PHP_EOL; | |
echo 'Tests category load for the children of CATEGORY_ID analyzing the performance of the Emico_Tweakwise_Helper_Data::getFilterCategory method.' . PHP_EOL; | |
echo PHP_EOL; | |
} | |
} | |
$shell = new ISAAC_Category_Load_Tester(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment