# 頁面性能
> If an ecommerce site is making \$100,000 per day, a 1 second page delay could potentially cost you \$2.5 million in lost sales every year.
>
> \-[Kissmetrics](https://blog.kissmetrics.com/loading-time/)
> [According to Akamai](https://www.akamai.com/uk/en/about/news/press/2017-press/akamai-releases-spring-2017-state-of-online-retail-performance-report.jsp), a **100-millisecond**?delay in page load can impact conversion rate by**7%**, while a **2-second**?delay can increase bounce rates by a whopping **103%**.
*****
## 頁面測試速度工具:
【Webpagetest ,gtmetric, google page insight, mobile insight】
https://web.dev/measure/
https://tools.pingdom.com/
https://gtmetrix.com/
https://www.webpagetest.org/
參考:[一定要看的WordPress網站性能及速度優化建議 | WordPress大學 (wpdaxue.com)](https://www.wpdaxue.com/wordpress-performance-and-speed-optimization.html)
*****
## 性能監控工具:
1,[Tideways](https://tideways.com/),這個是非常推薦,必殺技
https://magecloud.agency/how-to-improve-magento-2-performance-profiling-with-tideways/
注意:在面板環境安裝tideways,可能出現加載模塊不行的問題,檢查phpinfo 有沒有 extension=tideways.so ,參考 : [Install on CentOS/Red Hat/Fedora :: Documentation (tideways.com)](https://support.tideways.com/documentation/setup/installation/centos-redhat-fedora.html)

2,magento性能監控插件 [MGT Developer Toolbar - Installation | MGT-COMMERCE | Knowledge Base](https://www.mgt-commerce.com/docs/mgt-developer-toolbar/magento2/installation) 必須安裝的:

*****
*****
*****
## 性能提高方法:
1,magento性能優化指南[普通版]:
[Speed up Magento - The Ultimate Guide (Updated 2021) (goivvy.com)](https://www.goivvy.com/blog/speed-up-magento)
[Magento 2 Slow? 10 Steps to Speed Up Magento 2 - Magento 2 development – Mageplaza](https://www.mageplaza.com/devdocs/speed-up-magento-2.html)
https://magefan.com/blog/speed-up-magento-2
2,二八定律就是解決核心問題[核心版]:
[Magento Performance Optimization Guide | Toptal](https://www.toptal.com/magento/magento-performance-optimization-guide)
[Magento 2 performance problem profiling and optimization | by Yegor Shytikov | Medium](https://yegorshytikov.medium.com/magento-2-performance-problem-profiling-and-optimization-3a2054fe0768)
https://onilab.com/blog/magento-2-performance-speed-optimization-guide/
3,我認為最好的2個blog:
Yegor Shytikov: [Magento 2 performance problem profiling and optimization | by Yegor Shytikov | Medium](https://yegorshytikov.medium.com/magento-2-performance-problem-profiling-and-optimization-3a2054fe0768)
Onilab Blog: [Magento 2 Speed Optimization: 32 Fixes to Fast Performance | Onilab Blog](https://onilab.com/blog/magento-2-performance-speed-optimization-guide/)
*****
4,我的優化經驗3點:
```
1,要安裝 性能監控工具(如tideways),一邊測試性能,一邊觀察實際瓶頸。
2,從瓶頸出發解決核心問題,同時不能過度優化,達到基本標準則可。
3,要先治療病人,再給它蓋上外套。先解決核心性能問題,再用緩存之類。
```
*****
## 優化流程:
優化前,用 chrome瀏覽器的 lighthouse測試一下:

顯然很多改善空間,觀察 瓶頸,首先要解決瓶頸:

**瓶頸:Reduce initial server response time**
*****
再用Gtmetrix測試一下:

**重點解決:** 首字符響應時間過長,TTFB 達 1秒
**注意:** TTFB 要用相應國家地區去測試才更準確, 首要測試標準: chrome lighthouse.
*****
我們現在知道要解決的問題在于 TTFB, 那么現在就是 想辦法解決。安裝 性能監控工具,看底層 是哪里造成 這個性能問題:


如上圖所示,可以馬上看到 造成卡慢的原因:1,complie的問題,2,sql執行過多的問題。解決它,然后測試看看情況。
*****
## complie的問題:
opcache complied問題
問題:https://tideways.com/profiler/blog/fine-tune-your-opcache-configuration-to-avoid-caching-suprises
```
解決1:opcache調節性能
```
方法參考:https://segmentfault.com/a/1190000002523558?utm_source=tag-newest
```
解決2:執行 composer dump-autoload -o
```
*****
## SQL執行過多的解決方法:
**1,mysql性能優化:**
1.1 myisam 轉為 innodb,提高并發
1.2 Sql 300條太多, 要檢查是不是可以減少 ,例如去掉沒用的模塊。
1.3 Mysql 的自身 性能調高:

**2,加上對象緩存 Redis**
參考: https://www.mageplaza.com/devdocs/redis-cache-magento-2.html
Redis 主要可以用在3個地方: 1,標準緩存,2,頁面緩存,3,session管理

*****
*****
## 知識:關于各層緩存外套(先醫治,再外套):
* 1.對象緩存。我們將在下面對此進行更詳細的介紹,但是對象緩存涉及存儲數據庫查詢,以便下次需要數據時,無需查詢數據庫即可從緩存中傳遞數據。
* 2.頁面緩存。頁面緩存涉及存儲頁面的整個HTML,以便在后續視圖中可以生成和顯示內容(包括文件和數據庫查詢),而無需WordPress每次都這樣做。
* 3.操作碼緩存。操作碼緩存涉及在每個請求之間編譯PHP代碼。為了執行PHP代碼,PHP編譯器必須先編譯代碼,然后生成可執行代碼供服務器執行。操作碼緩存已編譯的代碼。
* 4.CDN緩存。內容分發網絡(CDN)使用世界各地的邊緣服務器來存儲靜態網站文件(即CSS,JavaScript和媒體文件),以便更快地將其分發給與主機服務器地理上相距遙遠的用戶。
*****
*****
## 加入靜態緩存,頁面緩存
毫無疑問,對于網站來說,靜態緩存,頁面緩存 是解決性能的**最厲害的武器**,這里推薦使用magento內置的Full Page Cache 。
Full Page Cache 有3種方案:1,file文件緩存,2,redis緩存,3,varnish緩存(推薦)
開通 full page cache 后:

參考:https://inviqa.com/blog/how-full-page-cache-works-magento-commerce
*****
*****
*****
*****
*****
## magento 多余插件模塊優化
我覺得這是最重要的優化方法:
[Make Magento 2 small again | | integer\_net (integer-net.com)](https://www.integer-net.com/make-magento-2-small-again/)
沒用的插件模塊可以下架,例如如下:
```
<?php
return [
'modules' => [
'Magento_AdminAnalytics' => 1,
'Magento_Store' => 1,
'Magento_AdobeIms' => 0,
'Magento_AdobeImsApi' => 0,
'Magento_AdobeStockAdminUi' => 0,
'Magento_MediaGallery' => 1,
'Magento_AdobeStockAssetApi' => 0,
'Magento_AdobeStockClient' => 0,
'Magento_AdobeStockClientApi' => 0,
'Magento_AdobeStockImage' => 0,
'Magento_AdobeStockImageAdminUi' => 0,
'Magento_AdobeStockImageApi' => 0,
'Magento_AdvancedPricingImportExport' => 1,
'Magento_Directory' => 1,
'Magento_Amqp' => 0,
'Magento_AmqpStore' => 0,
'Magento_Config' => 1,
'Magento_Theme' => 1,
'Magento_Backend' => 1,
'Magento_Variable' => 1,
'Magento_Eav' => 1,
'Magento_Customer' => 1,
'Magento_AuthorizenetGraphQl' => 1,
'Magento_Search' => 1,
'Magento_Backup' => 1,
'Magento_AdminNotification' => 1,
'Magento_BraintreeGraphQl' => 0,
'Magento_Indexer' => 1,
'Magento_Authorization' => 1,
'Magento_BundleImportExport' => 1,
'Magento_Rule' => 1,
'Magento_CacheInvalidate' => 1,
'Magento_Cms' => 1,
'Magento_Catalog' => 1,
'Magento_Quote' => 1,
'Magento_Security' => 1,
'Magento_GraphQl' => 1,
'Magento_EavGraphQl' => 1,
'Magento_StoreGraphQl' => 1,
'Magento_CatalogImportExport' => 1,
'Magento_SalesSequence' => 1,
'Magento_CatalogInventory' => 1,
'Magento_CatalogRule' => 1,
'Magento_Msrp' => 1,
'Magento_CatalogRuleGraphQl' => 1,
'Magento_CatalogSearch' => 1,
'Magento_Bundle' => 1,
'Magento_Payment' => 1,
'Magento_CatalogUrlRewrite' => 1,
'Magento_CatalogGraphQl' => 1,
'Magento_Widget' => 1,
'Magento_Sales' => 1,
'Magento_Checkout' => 1,
'Magento_CheckoutAgreementsGraphQl' => 1,
'Magento_Vault' => 1,
'Magento_CmsGraphQl' => 1,
'Magento_Downloadable' => 1,
'Magento_CmsUrlRewrite' => 1,
'Magento_CmsUrlRewriteGraphQl' => 1,
'Magento_ComposerRootUpdatePlugin' => 1,
'Magento_User' => 1,
'Magento_ConfigurableProduct' => 1,
'Magento_UrlRewrite' => 1,
'Magento_QuoteGraphQl' => 1,
'Magento_ConfigurableProductSales' => 1,
'Magento_GroupedProduct' => 1,
'Magento_Contact' => 1,
'Magento_Cookie' => 1,
'Magento_Cron' => 1,
'Magento_Csp' => 1,
'Magento_CurrencySymbol' => 1,
'Magento_Captcha' => 1,
'Magento_Integration' => 1,
'Magento_DownloadableGraphQl' => 1,
'Magento_CustomerGraphQl' => 1,
'Magento_CustomerImportExport' => 1,
'Magento_SampleData' => 1,
'Magento_Deploy' => 1,
'Magento_Developer' => 1,
'Magento_Dhl' => 0,
'Magento_AsynchronousOperations' => 1,
'Magento_DirectoryGraphQl' => 1,
'Magento_Tax' => 1,
'Magento_CustomerDownloadableGraphQl' => 1,
'Magento_ImportExport' => 1,
'Magento_Weee' => 1,
'Magento_CardinalCommerce' => 1,
'Magento_CatalogCustomerGraphQl' => 1,
'Magento_AdvancedSearch' => 1,
'Magento_Elasticsearch' => 0,
'Magento_Elasticsearch7' => 0,
'Magento_Email' => 1,
'Magento_EncryptionKey' => 1,
'Magento_Fedex' => 0,
'Magento_GiftMessage' => 1,
'Magento_GoogleAdwords' => 0,
'Magento_GoogleAnalytics' => 1,
'Magento_Ui' => 1,
'Magento_GoogleShoppingAds' => 0,
'Magento_BundleGraphQl' => 1,
'Magento_PageCache' => 1,
'Magento_GroupedCatalogInventory' => 1,
'Magento_GroupedImportExport' => 1,
'Magento_CatalogSampleData' => 1,
'Magento_GroupedProductGraphQl' => 1,
'Magento_GroupedProductSampleData' => 1,
'Magento_DownloadableImportExport' => 1,
'Magento_CatalogRuleConfigurable' => 1,
'Magento_InstantPurchase' => 1,
'Magento_Analytics' => 1,
'Magento_Inventory' => 0,
'Magento_InventoryAdminUi' => 0,
'Magento_InventoryAdvancedCheckout' => 0,
'Magento_InventoryApi' => 0,
'Magento_InventoryBundleProduct' => 0,
'Magento_InventoryBundleProductAdminUi' => 0,
'Magento_InventoryCatalog' => 0,
'Magento_InventorySales' => 0,
'Magento_InventoryCatalogAdminUi' => 0,
'Magento_InventoryCatalogApi' => 0,
'Magento_InventoryCatalogSearch' => 0,
'Magento_InventoryConfigurableProduct' => 0,
'Magento_InventoryConfigurableProductAdminUi' => 0,
'Magento_InventoryConfigurableProductIndexer' => 0,
'Magento_InventoryConfiguration' => 0,
'Magento_InventoryConfigurationApi' => 0,
'Magento_InventoryDistanceBasedSourceSelection' => 0,
'Magento_InventoryDistanceBasedSourceSelectionAdminUi' => 0,
'Magento_InventoryDistanceBasedSourceSelectionApi' => 0,
'Magento_InventoryElasticsearch' => 0,
'Magento_InventoryExportStockApi' => 0,
'Magento_InventoryIndexer' => 0,
'Magento_InventorySalesApi' => 0,
'Magento_InventoryGroupedProduct' => 0,
'Magento_InventoryGroupedProductAdminUi' => 0,
'Magento_InventoryGroupedProductIndexer' => 0,
'Magento_InventoryImportExport' => 0,
'Magento_InventoryCache' => 0,
'Magento_InventoryLowQuantityNotification' => 0,
'Magento_Reports' => 1,
'Magento_InventoryLowQuantityNotificationApi' => 0,
'Magento_InventoryMultiDimensionalIndexerApi' => 0,
'Magento_InventoryProductAlert' => 0,
'Magento_InventoryRequisitionList' => 0,
'Magento_InventoryReservations' => 0,
'Magento_InventoryReservationCli' => 0,
'Magento_InventoryReservationsApi' => 0,
'Magento_InventoryExportStock' => 0,
'Magento_InventorySalesAdminUi' => 0,
'Magento_CatalogInventoryGraphQl' => 1,
'Magento_InventorySalesFrontendUi' => 0,
'Magento_InventorySetupFixtureGenerator' => 0,
'Magento_InventoryShipping' => 0,
'Magento_Shipping' => 1,
'Magento_InventorySourceDeductionApi' => 0,
'Magento_InventorySourceSelection' => 0,
'Magento_InventorySourceSelectionApi' => 0,
'Magento_LayeredNavigation' => 1,
'Magento_Marketplace' => 0,
'Magento_AdobeStockAsset' => 0,
'Magento_MediaGalleryApi' => 1,
'Magento_MediaStorage' => 1,
'Magento_MessageQueue' => 1,
'Magento_BundleSampleData' => 1,
'Magento_MsrpConfigurableProduct' => 1,
'Magento_MsrpGroupedProduct' => 1,
'Magento_DownloadableSampleData' => 1,
'Magento_Multishipping' => 1,
'Magento_MysqlMq' => 1,
'Magento_NewRelicReporting' => 1,
'Magento_Newsletter' => 1,
'Magento_OfflinePayments' => 1,
'Magento_SalesRule' => 1,
'Magento_OfflineShipping' => 1,
'Magento_GraphQlCache' => 1,
'Magento_Authorizenet' => 1,
'Magento_Paypal' => 1,
'Magento_PaypalCaptcha' => 1,
'Magento_PaypalGraphQl' => 1,
'MSP_ReCaptcha' => 1,
'Magento_Persistent' => 1,
'Magento_ProductAlert' => 1,
'Magento_ConfigurableSampleData' => 1,
'Magento_ProductVideo' => 1,
'Magento_AuthorizenetAcceptjs' => 1,
'Magento_QuoteAnalytics' => 1,
'Magento_ConfigurableProductGraphQl' => 1,
'Magento_RelatedProductGraphQl' => 1,
'Magento_ReleaseNotification' => 1,
'Magento_InventoryLowQuantityNotificationAdminUi' => 0,
'Magento_RequireJs' => 1,
'Magento_Review' => 1,
'Magento_ReviewAnalytics' => 1,
'Magento_ReviewSampleData' => 1,
'Magento_Robots' => 1,
'Magento_Rss' => 1,
'Magento_ThemeSampleData' => 1,
'Magento_ConfigurableImportExport' => 1,
'Magento_SalesAnalytics' => 1,
'Magento_SalesGraphQl' => 1,
'Magento_SalesInventory' => 1,
'Magento_OfflineShippingSampleData' => 1,
'Magento_CatalogRuleSampleData' => 1,
'Magento_TaxSampleData' => 1,
'Magento_AuthorizenetCardinal' => 1,
'Magento_SalesRuleSampleData' => 1,
'Magento_Elasticsearch6' => 1,
'Magento_CustomerAnalytics' => 1,
'Magento_SendFriend' => 1,
'Magento_SendFriendGraphQl' => 1,
'Magento_InventoryShippingAdminUi' => 0,
'Magento_Signifyd' => 1,
'Magento_Sitemap' => 1,
'Magento_InventoryGraphQl' => 0,
'Magento_UrlRewriteGraphQl' => 1,
'Magento_Webapi' => 1,
'Magento_SwaggerWebapi' => 1,
'Magento_SwaggerWebapiAsync' => 1,
'Magento_Swatches' => 1,
'Magento_SwatchesGraphQl' => 1,
'Magento_SwatchesLayeredNavigation' => 1,
'Magento_SwatchesSampleData' => 1,
'Magento_MsrpSampleData' => 1,
'Magento_TaxGraphQl' => 1,
'Magento_TaxImportExport' => 1,
'Magento_CustomerSampleData' => 1,
'Magento_CatalogCmsGraphQl' => 1,
'Magento_ThemeGraphQl' => 1,
'Magento_CmsSampleData' => 1,
'Magento_Tinymce3' => 1,
'Magento_Translation' => 1,
'Magento_GoogleOptimizer' => 0,
'Magento_Ups' => 0,
'Magento_SalesSampleData' => 1,
'Magento_CatalogUrlRewriteGraphQl' => 1,
'Magento_CatalogAnalytics' => 1,
'Magento_Usps' => 1,
'Magento_CheckoutAgreements' => 1,
'Magento_Braintree' => 1,
'Magento_VaultGraphQl' => 1,
'Magento_Version' => 1,
'Magento_Swagger' => 1,
'Magento_WebapiAsync' => 1,
'Magento_WebapiSecurity' => 1,
'Magento_ProductLinksSampleData' => 1,
'Magento_WeeeGraphQl' => 1,
'Magento_CatalogWidget' => 1,
'Magento_WidgetSampleData' => 1,
'Magento_Wishlist' => 1,
'Magento_WishlistAnalytics' => 1,
'Magento_WishlistGraphQl' => 1,
'Magento_WishlistSampleData' => 1,
'Amazon_Core' => 0,
'Amazon_Login' => 0,
'Amazon_Payment' => 0,
'Dotdigitalgroup_Email' => 0,
'Dotdigitalgroup_Chat' => 0,
'Klarna_Core' => 0,
'Klarna_Ordermanagement' => 0,
'Klarna_Kp' => 0,
'Magento_PaypalReCaptcha' => 1,
'MSP_TwoFactorAuth' => 1,
'Mageplaza_Core' => 1,
'Mageplaza_AbandonedCart' => 1,
'Mageplaza_DeliveryTime' => 1,
'Mageplaza_GeoIP' => 1,
'Mageplaza_Osc' => 1,
'Mageplaza_Smtp' => 0,
'Mgt_DeveloperToolbar' => 1,
'Temando_ShippingRemover' => 1,
'Vertex_Tax' => 0,
'Vertex_AddressValidation' => 0,
'Yotpo_Yotpo' => 0
]
];
```
=>0 ,設置為0,減少80個模塊,性能當然會提升。
*****
### 多庫存管理【inventory】插件處理
問題的參考文檔:
[https://magento.stackexchange.com/questions/298020/magento-cataloginventory-observer-quantityvalidatorobserver-slow-performance](https://magento.stackexchange.com/questions/298020/magento-cataloginventory-observer-quantityvalidatorobserver-slow-performance)
[https://docs.magento.com/user-guide/catalog/inventory.html](https://docs.magento.com/user-guide/catalog/inventory.html)
[https://yegorshytikov.medium.com/magento-2-multi-source-inventory-performance-4da92d8cf3cf](https://yegorshytikov.medium.com/magento-2-multi-source-inventory-performance-4da92d8cf3cf)
解決問題的參考辦法:https://magento.stackexchange.com/questions/322134/after-upgrade-magento-from-2-3-4-to-2-4-cannot-instantiate-interface-magento-inv
[https://yegorshytikov.medium.com/magento-2-performance-problem-profiling-and-optimization-3a2054fe0768](https://yegorshytikov.medium.com/magento-2-performance-problem-profiling-and-optimization-3a2054fe0768):
```
php bin/magento module:disable -f Magento_Inventory Magento_InventoryAdminUi Magento_InventoryApi Magento_InventoryBundleProduct Magento_InventoryBundleProductAdminUi Magento_InventoryCatalog Magento_InventorySales Magento_InventoryCatalogAdminUi Magento_InventoryCatalogApi Magento_InventoryCatalogSearch Magento_InventoryConfigurableProduct Magento_InventoryConfigurableProductAdminUi Magento_InventoryConfigurableProductIndexer Magento_InventoryConfiguration Magento_InventoryConfigurationApi Magento_InventoryGroupedProduct Magento_InventoryGroupedProductAdminUi Magento_InventoryGroupedProductIndexer Magento_InventoryImportExport Magento_InventoryIndexer Magento_InventoryLowQuantityNotification Magento_InventoryLowQuantityNotificationAdminUi Magento_InventoryLowQuantityNotificationApi Magento_InventoryMultiDimensionalIndexerApi Magento_InventoryProductAlert Magento_InventoryReservations Magento_InventoryReservationsApi Magento_InventoryCache Magento_InventorySalesAdminUi Magento_InventorySalesApi Magento_InventorySalesFrontendUi Magento_InventoryShipping Magento_InventorySourceDeductionApi Magento_InventorySourceSelection Magento_InventorySourceSelectionApi Magento_InventoryShippingAdminUi Magento_InventoryDistanceBasedSourceSelectionAdminUi Magento_InventoryDistanceBasedSourceSelectionApi Magento_InventoryElasticsearch Magento_InventoryExportStockApi Magento_InventoryReservationCli Magento_InventoryExportStock Magento_CatalogInventoryGraphQl Magento_InventorySetupFixtureGenerator Magento_InventoryAdvancedCheckout Magento_InventoryDistanceBasedSourceSelection Magento_InventoryRequisitionList Magento_InventoryGraphQl Magento_InventoryBundleImportExport Magento_InventoryBundleProductIndexer Magento_InventoryInStorePickupApi Magento_InventoryInStorePickupAdminUi Magento_InventoryInStorePickup Magento_InventoryInStorePickupGraphQl Magento_InventoryInStorePickupShippingApi Magento_InventoryInStorePickupQuoteGraphQl Magento_InventoryInStorePickupSales Magento_InventoryInStorePickupSalesApi Magento_InventoryInStorePickupQuote Magento_InventoryInStorePickupShipping Magento_InventoryInStorePickupShippingAdminUi Magento_InventoryInStorePickupFrontend Magento_InventoryInStorePickupMultishipping Magento_InventoryInStorePickupSalesAdminUi Magento_InventoryInStorePickupWebapiExtension
```
或者:
```
php bin/magento module:status | grep Magento_Inventory | grep -v List | grep -v None | grep -v -e '^$' | xargs php bin/magento module:disable
php bin/magento s:upgrade
php bin/magento s:s:deploy -f
php bin/magento c:flush
php bin/magento i:reindex
composer dumpautoload
```
結果:可以對比一下,應該sql大幅減少。
*****
## 其他的性能檢查和調優:
**1,設置 redis session**
**2,開通 Zend OPcache**

**3,PHP性能調整:**
寶塔面板——軟件管理——PHP管理——性能調整,根據服務器內存數量和自己建站的數量進行并發數量設置。
**4,升級PHP**
php8 :

[The Definitive PHP 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, and 8.0 Benchmarks (2021) (kinsta.com)](https://kinsta.com/blog/php-benchmarks/)
**5,Mysql 修改為 mysql8 (選)**
**6,檢查插件,經常是它造成問題**
解:選擇更好的插件
*****
## **最后,看看優化結果:**

沒有問題了,頁面性能問題,已經解決。
*****
*****