Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow Performance of ProductMetadata::getVersion #24025

Closed
beberlei opened this issue Aug 6, 2019 · 12 comments
Closed

Slow Performance of ProductMetadata::getVersion #24025

beberlei opened this issue Aug 6, 2019 · 12 comments
Assignees
Labels
Component: Framework/App Fixed in 2.3.x The issue has been fixed in 2.3 release line Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@beberlei
Copy link

beberlei commented Aug 6, 2019

Preconditions (*)

Any Magento 2 version.

Steps to reproduce (*)

  1. Have a more complex composer.json with source checked repositories (git checkouts for example).
  2. A plugin that calls ProductMetadata::getVersion to check for Magento version.

Expected result (*)

Calling ProductMetadata::getVersion, I expected it to be a function returning a constant. Super-fast.

Actual result (*)

147ms in this case with 7 calls to proc_open, calling svn, git, hg to retrieve versions of source packages. In addition the PHP based JSON Parser is triggered for ~15ms. This overhead is present regardless f all packages were dist or source based and their version were known.

This output is from Tideways callgraph profiler:

tideways_magento2_getversion

This is related to the closed #23242 ticket that wasn't reproduced.

This performance issue is especially critical, because they way plugins check the magento version, this overhead is usually present in all requests to a Magento 2 shop.

@m2-assistant
Copy link

m2-assistant bot commented Aug 6, 2019

Hi @beberlei. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@beberlei do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Aug 6, 2019
@davidverholen davidverholen self-assigned this Aug 6, 2019
@m2-assistant
Copy link

m2-assistant bot commented Aug 6, 2019

Hi @davidverholen. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@davidverholen davidverholen added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Component: Framework/App Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Aug 6, 2019
@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Aug 6, 2019
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @davidverholen
Thank you for verifying the issue. Based on the provided information internal tickets MC-18958 were created

Issue Available: @davidverholen, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@davidverholen
Copy link
Member

it has been a constant originally. It should probably be changed back again with the constant update being part of the release process
e46a3cf#diff-162b082e41b9d0990e5dcc7439d19f26

This method is called several times in the core. Also, 3rd party extensions are using it to check compatibility of features.
In very many cases this leads to every uncached request being 100ms - 200ms slower

@beberlei
Copy link
Author

beberlei commented Aug 6, 2019

The easiest fix would be using the composer json finder there and parse the versions out intead of using ComposerInformation:

diff --git a/lib/internal/Magento/Framework/App/ProductMetadata.php b/lib/internal/Magento/Framework/App/ProductMetadata.php
index c9fde94352a..5fdef2ba5c5 100644
--- a/lib/internal/Magento/Framework/App/ProductMetadata.php
+++ b/lib/internal/Magento/Framework/App/ProductMetadata.php
@@ -7,10 +7,7 @@
  */
 namespace Magento\Framework\App;
 
-use Magento\Framework\Composer\ComposerFactory;
 use \Magento\Framework\Composer\ComposerJsonFinder;
-use \Magento\Framework\App\Filesystem\DirectoryList;
-use \Magento\Framework\Composer\ComposerInformation;
 
 /**
  * Class ProductMetadata
@@ -41,11 +38,6 @@ class ProductMetadata implements ProductMetadataInterface
      */
     protected $composerJsonFinder;
 
-    /**
-     * @var \Magento\Framework\Composer\ComposerInformation
-     */
-    private $composerInformation;
-
     /**
      * @param ComposerJsonFinder $composerJsonFinder
      */
@@ -62,11 +54,13 @@ class ProductMetadata implements ProductMetadataInterface
     public function getVersion()
     {
         if (!$this->version) {
-            if (!($this->version = $this->getSystemPackageVersion())) {
-                if ($this->getComposerInformation()->isMagentoRoot()) {
-                    $this->version = $this->getComposerInformation()->getRootPackage()->getPrettyVersion();
-                } else {
-                    $this->version = 'UNKNOWN';
+            $composerJsonFile = $this->composerJsonFinder->findComposerJson();
+            $installedPackages = json_decode(file_get_contents(dirname($composerJsonFile) . "/vendor/composer/installed.json"));
+
+            foreach ($installedPackages as $package) {
+                if (in_array($package['name'], ['magento/product-community-edition', 'magento/product-enterprise-edition'])) {
+                    $this->version = $package['version'];
+                    break;
                 }
             }
         }
@@ -92,37 +86,4 @@ class ProductMetadata implements ProductMetadataInterface
     {
         return self::PRODUCT_NAME;
     }
-
-    /**
-     * Get version from system package
-     *
-     * @return string
-     * @deprecated 100.1.0
-     */
-    private function getSystemPackageVersion()
-    {
-        $packages = $this->getComposerInformation()->getSystemPackages();
-        foreach ($packages as $package) {
-            if (isset($package['name']) && isset($package['version'])) {
-                return $package['version'];
-            }
-        }
-        return '';
-    }
-
-    /**
-     * Load composerInformation
-     *
-     * @return ComposerInformation
-     * @deprecated 100.1.0
-     */
-    private function getComposerInformation()
-    {
-        if (!$this->composerInformation) {
-            $directoryList              = new DirectoryList(BP);
-            $composerFactory            = new ComposerFactory($directoryList, $this->composerJsonFinder);
-            $this->composerInformation  = new ComposerInformation($composerFactory);
-        }
-        return $this->composerInformation;
-    }
 }

@beberlei
Copy link
Author

beberlei commented Aug 6, 2019

A better approach (but more complex code wise) would probably be to write a magento-version.php file into the cache directory upon cache generation that contains the version:

<?php
// cache/magento-version.php
return '2.3.2';

and then doing return require $this->cacheDir . '/magento-version.php'; in getVersion() instead.

@sprankhub
Copy link
Member

There was also PR #19133, which introduced caching for the getVersion method. However, it was rejected.

@davidverholen
Copy link
Member

while the core might not really need it (because it knows its own version) it is a valid use case for 3rd Party developers for not having to maintain different code bases for each magento version.

Even when it's not considered best practice, still, many 3rd party extensions use it and so this is a very real problem

davidverholen pushed a commit to davidverholen/magento2 that referenced this issue Aug 7, 2019
@magento-engcom-team
Copy link
Contributor

Hi @beberlei. Thank you for your report.
The issue has been fixed in #24030 by @davidverholen in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.4 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Sep 26, 2019
@erikhansen
Copy link
Contributor

For the record, I encountered this issue on a Magento OS 2.3.2 site, as several Amasty extensions were calling \Magento\Framework\App\ProductMetadata::getVersion. After applying #24030 as a patch (via composer-patches), the ~100ms slowdown went away.

@VladimirZaets
Copy link
Contributor

Hi @beberlei. Thank you for your report.
The issue has been fixed in #26001 by @luklewluk in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.0 release.

@VladimirZaets VladimirZaets added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Dec 18, 2019
@erikhansen
Copy link
Contributor

After upgrading from Magento 2.3.2 to 2.3.3, the patch I mentioned above no longer applies (since the underlying code changed). If you're using 2.3.3 and you need to fix this issue, you can use this updated patch (it contains the relevant changes from 16ab63):

magento-framework-github-24025-fix-composer-version-lookup.patch.txt (remove the .txt from the filename)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Framework/App Fixed in 2.3.x The issue has been fixed in 2.3 release line Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

6 participants