性能
病毒掃描
病毒掃描程序是系統保護的基本組件,但對於像 Hugo 這樣頻繁讀寫磁盤的應用程序,性能影響可能很嚴重。例如,使用 Microsoft Defender Antivirus 時,某些網站的構建時間可能增加 400% 或更多。
在構建網站之前,你的病毒掃描程序已經評估過項目目錄中的文件。在構建網站時再次掃描它們是多余的。要提高性能,請將 Hugo 的可執行文件添加到病毒掃描程序的進程排除列表中。
例如,使用 Microsoft Defender Antivirus:
開始 > 設置 > 隱私和安全 > Windows 安全中心 > 打開 Windows 安全中心 > 病毒和威脅防護 > 管理設置 > 添加或刪除排除項 > 添加排除項 > 進程
然後輸入 hugo.exe 並按 添加 按鈕。
病毒掃描排除很常見,但在更改這些設置時要小心。有關詳細信息,請參閱 Microsoft Defender Antivirus 文檔。
其他病毒掃描程序也有類似的排除機制。請參閱各自的文檔。
模板指標
Hugo 很快,但低效的模板會阻礙性能。啟用模板指標以確定哪些模板花費的時間最多,並識別緩存機會:
hugo --templateMetrics --templateMetricsHints結果將如下所示:
Template Metrics:
cumulative average maximum cache percent cached total
duration duration duration potential cached count count template
---------- -------- -------- --------- ------- ------ ----- --------
36.037476822s 135.990478ms 225.765245ms 11 0 0 265 _partials/head.html
35.920040902s 164.018451ms 233.475072ms 0 0 0 219 articles/page.html
34.163268129s 128.917992ms 224.816751ms 23 0 0 265 _partials/head/meta/opengraph.html
1.041227437s 3.92916ms 186.303376ms 47 0 0 265 _partials/head/meta/schema.html
805.628827ms 27.780304ms 114.678523ms 0 0 0 29 section.html
624.08354ms 15.221549ms 108.420729ms 8 0 0 41 _partials/utilities/render-page-collection.html
545.968801ms 775.523µs 105.045775ms 0 0 0 704 summary.html
334.680981ms 1.262947ms 127.412027ms 100 0 0 265 _partials/head/js.html
272.763205ms 2.050851ms 24.371757ms 0 0 0 133 _markup/render-codeblock.html
163.951469ms 14.904679ms 70.267953ms 0 0 0 11 articles/section.html
153.07021ms 577.623µs 73.593597ms 100 0 0 265 _partials/head/init.html
150.910984ms 150.910984ms 150.910984ms 0 0 0 1 page.html
146.785804ms 146.785804ms 146.785804ms 0 0 0 1 contact.html
115.364617ms 115.364617ms 115.364617ms 0 0 0 1 authors/term.html
87.392071ms 329.781µs 10.687132ms 100 0 0 265 _partials/head/css.html
86.803122ms 86.803122ms 86.803122ms 0 0 0 1 home.html從左到右,各列分別表示:
cumulative duration(累計持續時間) :執行模板花費的累計時間。
average duration(平均持續時間) :執行模板花費的平均時間。
maximum duration(最大持續時間) :執行模板花費的最大時間。
cache potential(緩存潛力)
:以百分比顯示,任何具有 100% 緩存潛力的_partial_模板都應該使用 partialCached 函數而不是 partial 函數來調用。請參閱下面的 緩存 部分。
percent cached(緩存百分比) :渲染的模板被緩存的次數除以模板執行的次數。
cached count(緩存計數) :渲染的模板被緩存的次數。
total count(總計數) :模板執行的次數。
template(模板)
:模板的路徑,相對於 layouts 目錄。
Hugo 並行構建頁面,即同時生成多個頁面。由於這種並行性,“cumulative duration"值的總和通常大於構建網站實際花費的時間。
緩存
一些_partial_模板(如側邊欄或菜單)在網站構建期間會被執行多次。根據_partial_模板中的內容和期望的輸出,模板可能受益於緩存以減少執行次數。partialCached 模板函數為_partial_模板提供緩存功能。
請注意,你可以通過向 partialCached 傳遞初始上下文之外的其他參數來創建每個 partial 的緩存變體。有關更多詳細信息,請參閱 partialCached 文檔。
計時器
使用 debug.Timer 函數確定代碼塊的執行時間,這對於查找模板中的性能瓶頸非常有用。請參閱 詳細信息。