Quantcast
Channel: Open Judo » Fabrizio Branca
Viewing all articles
Browse latest Browse all 3

Day 11: How to log classes for the log generator

0
0

This blog post is part of the 25 Days of Magento 2 collection.

See related blog posts All about Autoloading and Magento 2 Part 1 and Part 2.

Option 1

The Magento documentation suggests adding following line at the end of your pub/index.php:

include '../dev/tools/classmap/logger.php';

Option 2

I’d prefer not to touch any Magento core class. Even if this is only temporary. PHP offers this mechanism to execute a php file after any request configured using the auto_append_file directive. Replace the relative path with an absolute path and add this line to your .htaccess file or vhost configuration:

php_value auto_append_file /var/www/sandbox/magento2/dev/tools/classmap/logger.php

Make sure the directory dev/tools/classmap/log is writeable for the webserver.

Generate Classmap

We’re not done yet. You should see a *.ser file in dev/tools/classmap/log/ for every request you did with the logger.php in place. Now we need to generate the classmap out of this information.

There’s another command line tool for this:

php -f dev/tools/classmap/log_generator.php ./dev/tools/classmap/log > ./var/classmap.ser

This tool will check all include path locations to search where those files are actually located. This information will be stored in the classmap (acting as a cache) so this slow operation doesn’t have to be done thousands of times during the actual Magento execution and page rendering process in production.

Once the classmap is generated you can output the content of this file more readable using this command:

php -r 'var_dump(unserialize(file_get_contents("var/classmap.ser")));'

Classmap Autoloader

The existence of the file var/classmap.ser is actually triggering the classmap autoloader that is using this file to read the classmap array. Check out bootstrap.php.

The file var/classmap.ser contains a serialized array containing the class names as array keys and the corresponding relative file path (relative to the Magento root directory instead of relative to the code pool!).

The classmap autoloader will be used to lookup the relative file path for a given class name. The cached relative file path will be appended to the Magento root directory converting it to an absolute file path instead. Including an absolute file path won’t make PHP look this file up in all include_path locations but load that file right away. We’ll save lots of unneeded syscalls and reduce disk io remarkable. If a file is not found in the classmap (and the minor bug with the order of the autoloaders is fixed) Magento will check the default IncludePath autoloader that will find the file in any case. This is why missing files in the classmap won’t be any problem.

Results

The following chart shows some statistics on lstat64 syscalls. In this case the script dev/shell/indexer.php is executed twice (without any parameters – showing the help screen only), with and without a classmap. The number of disk accesses, especially the ones checking for a file or directory that doesn’t exist, is much lower when using a classmap:

Magento 1 Backport: Aoe_ClassPathCache

And here’s the surprise for Magento 1 developers: A couple of months ago I implemented this exact same idea as a performance improvement for Magento 1. A Magento 2 core developer pointed out that my solution was pretty close to the Magento 2 classmap autoloader and actually the underlying idea is the same: Caching the real file path location instead of searching for relative file paths in the PHP include path.

Consider my module as a backport of this awesome Magento 2 feature. You can read about my extension and get the extension from Github.

Fabrizio Branca (Twitter: @fbrnc) is a Magento developer at AOE media. He, his wife Janine and their daughter Fiona live in San Francisco, California. On his website, he’s blogging about Magento, Varnish, Selenium and TYPO3. There you’ll salso find his free Magento modules like Aoe_Profiler, Aoe_Scheduler, Aoe_TemplateHints or the Magento-Varnish-Integration Aoe_Static.

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images