Created
December 1, 2020 14:07
-
-
Save magevision/6fae2ee30f1fad10dc755d4b3edc0784 to your computer and use it in GitHub Desktop.
UseProfilerInCLICommand
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
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Magento\Framework\Console\CommandListInterface"> | |
<arguments> | |
<argument name="commands" xsi:type="array"> | |
<item name="magevision_profiler" xsi:type="object">MageVision\Blog64\Console\Command\ProfilerCommand</item> | |
</argument> | |
</arguments> | |
</type> | |
<type name="MageVision\Blog64\Console\Command\ProfilerCommand"> | |
<arguments> | |
<argument name="name" xsi:type="string">magevision:blog64:profiler</argument> | |
</arguments> | |
</type> | |
</config> |
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 | |
namespace MageVision\Blog64\Console\Command; | |
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory; | |
use Magento\Framework\Console\Cli; | |
use Magento\Framework\Profiler\Driver\Standard\Stat; | |
use Magento\Framework\Profiler\Driver\Standard\StatFactory; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class ProfilerCommand extends Command | |
{ | |
/** | |
* @var StatFactory | |
*/ | |
private $statFactory; | |
/** | |
* @var CollectionFactory | |
*/ | |
private $countryCollectionFactory; | |
/** | |
* @param StatFactory $statFactory | |
* @param CollectionFactory $countryCollectionFactory | |
* @param string $name | |
*/ | |
public function __construct( | |
StatFactory $statFactory, | |
CollectionFactory $countryCollectionFactory, | |
string $name | |
) { | |
$this->statFactory = $statFactory; | |
$this->countryCollectionFactory = $countryCollectionFactory; | |
parent::__construct($name); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
protected function configure() | |
{ | |
$this->setDescription('MageVision console command with profiler.'); | |
parent::configure(); | |
} | |
/** | |
* Execute the command | |
* | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* | |
* @return int | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln('<info>Process starts.</info>'); | |
$stat = $this->statFactory->create(); | |
$stat->start( | |
'magevision_blog64_profiler', | |
microtime(true), | |
memory_get_usage(true), | |
memory_get_usage() | |
); | |
$countries = $this->countryCollectionFactory->create()->getItems(); | |
foreach ($countries as $country) { | |
// Add here your functionality | |
//... | |
} | |
$stat->stop( | |
'magevision_blog64_profiler', | |
microtime(true), | |
memory_get_usage(true), | |
memory_get_usage() | |
); | |
$stat = $stat->get('magevision_blog64_profiler'); | |
$stat['time'] = $stat[Stat::TIME]; | |
$output->writeln([ | |
'<info>Process successfully finished.</info>', | |
sprintf('<info>Executing time in microseconds.: %s</info>', $stat[Stat::TIME]), | |
sprintf('<info>Real size of memory allocated from system: %s</info>', $stat[Stat::REALMEM]), | |
sprintf('<info>Memory used by emalloc(): %s</info>', $stat[Stat::EMALLOC]), | |
]); | |
return Cli::RETURN_SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment