Jump to content

Manual:效能剖析

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page Manual:Profiling and the translation is 42% complete.

效能剖析(Profiling)會在頁面操作期間追蹤程式碼執行狀況,並回報特定函式所佔用的總程式碼執行百分比。 效能分析是一項進階任務,旨在協助開發人員與系統管理員追蹤MediaWiki中需要被優化的效能瓶頸源頭。

PHP profiling

You can use XHProf or XDebug. These PHP extensions provide tracing and profiling capabilities. (Note that the tideways_xhprof fork that adds PHP8 support has since been abandoned. The original XHProf now also supports PHP8.)

Install the xhprof package from PECL.

For XDebug, see XDebug profiling documentation. For command line scripts, you can use php -d xdebug.mode=profile maintenance/{script}. Webgrind can then be used to view profile results.

MediaWiki的自訂剖析器

自 MediaWiki 1.25 版本起,自訂的MediaWiki效能剖析函式的呼叫(wfProfileIn/wfProfileOut)已被廢棄,此後僅執行空操作,並於1.31版本中移除
MediaWiki版本:
1.25

在MediaWiki 1.25中,效能剖析功能已完全重寫,許多先前與效能分析相關的設定皆被移除,改為整合至$wgProfiler的參數中。 值得注意的是,輸出的類型已與類別分離。 有關1.25版本之前的效能剖析相關文件,請參閱本頁面的舊版

$wgProfiler 範例的組態:

<?php
$wgProfiler['class'] = 'ProfilerXhprof';
$wgProfiler['output'] = [ 'ProfilerOutputText' ];
$wgProfiler['visible'] = true;

這些參數的每一個(以及更多)均詳細說明於下方:

class
'ProfilerXhprof'. ProfilerXhprof是MediaWiki 1.25版的新功能,提供基於Xhprof的效能剖析器,可擷取所有函式加上子功能單元的效能分析數據。 那些舊的數值如ProfilerStandard、ProfilerUDP、ProfilerDB等均無法運作。
output
一個或多個 'ProfilerOutputText', 'ProfilerOutputDump', 'ProfilerOutputStats'。[1] 文字輸出資訊的方式有兩種:以HTML註解形式呈現、或置於外觀樣式之後。 UDP 是一種發送至udpprofile常駐程式的格式。 Dump 會產生剖析資訊的dump檔,供xhprof GUI使用(請參閱Vagrant 設定中的xhprofgui角色)。
outputDir
Only applies to 'ProfilerOutputDump' format. Required to specify where the dump files will be stored.
visible
Only applies to 'ProfilerOutputText' format. Whether text is shown after the skin or in an HTML comment.

Maintenance scripts

Maintenance scripts support a --profiler option. This changes the output method of the profiler as well as ensuring its enabled in the CLI. Because this only changes the output method and not the profiler class, you still must have some sort of profiling setup in $wgProfiler.

Troubleshooting

If you get "Fatal error: Uncaught Exception: Neither xhprof nor tideways are installed" then you need to do install and enable php-xhprof. You can install it from PECL on macOS/Linux/Windows, or for Linux (Debian/Ubuntu) from deb.sury.org, or for Fedora Linux from remirepo.net. If using Debian 11 Bullseye or earlier, use sudo apt install php-tideways instead.

Output

Text output looks like:

100.00% 508.972      1 - main()
 96.67% 492.003      1 - MediaWiki::run
 96.61% 491.699      1 - MediaWiki::main
 87.12% 443.434      1 - MediaWiki::performRequest
 84.16% 428.356      1 - SpecialPageFactory::executePath
 84.11% 428.073      1 - SpecialPage::run
 84.10% 428.058      1 - SpecialRecentChanges::execute
 63.82% 324.838      1 - ChangesListSpecialPage::execute
 57.07% 290.495      1 - ChangesListSpecialPage::webOutput
 51.06% 259.877      1 - SpecialRecentChanges::outputChangesList
 44.72% 227.633    397 - Wikimedia\Rdbms\Database::query
 31.07% 158.155    922 - Message::fetchMessage
 30.61% 155.788    865 - MessageCache::get
 29.55% 150.398    398 - Wikimedia\Rdbms\Database::doProfiledQuery
 29.39% 149.570    865 - MessageCache::getMessageFromFallbackChain
 29.26% 148.923    869 - MessageCache::getMessageForLang
 29.06% 147.931    393 - Wikimedia\Rdbms\Database::select
 27.30% 138.962      1 - EnhancedChangesList::endRecentChangesList
 27.30% 138.960      1 - EnhancedChangesList::recentChangesBlock
...

The first column is the time percent; the second column is the time (in ms); the third column is the count; and the fourth column is the name of the method.

See also

Footnotes

  1. 欲知ProfilerOutputDump,請參見git #750e4eb9

Code steward