-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Comments
Hi @beberlei. Thank you for your report.
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:
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?
|
Hi @davidverholen. Thank you for working on this issue.
|
✅ Confirmed by @davidverholen 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. |
it has been a constant originally. It should probably be changed back again with the constant update being part of the release process This method is called several times in the core. Also, 3rd party extensions are using it to check compatibility of features. |
The easiest fix would be using the composer json finder there and parse the versions out intead of using 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;
- }
} |
A better approach (but more complex code wise) would probably be to write a <?php
// cache/magento-version.php
return '2.3.2'; and then doing |
There was also PR #19133, which introduced caching for the |
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 |
Hi @beberlei. Thank you for your report.
The fix will be available with the upcoming 2.3.4 release. |
For the record, I encountered this issue on a Magento OS 2.3.2 site, as several Amasty extensions were calling |
Hi @beberlei. Thank you for your report.
The fix will be available with the upcoming 2.4.0 release. |
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 |
Preconditions (*)
Any Magento 2 version.
Steps to reproduce (*)
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:
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.
The text was updated successfully, but these errors were encountered: