性能 Performance
病毒掃描
病毒掃描器是系統保護的重要組件,但對於像 Hugo 這樣頻繁讀寫磁碟的應用程式來說,性能影響可能會非常嚴重。例如,使用 Microsoft Defender Antivirus 時,某些網站的建構時間可能會增加 400% 或更多。
在構建網站之前,病毒掃描器已經對專案目錄中的檔案進行過評估。在構建網站時再次掃描它們是多餘的。為了提升性能,將 Hugo 的可執行檔加入到病毒掃描器的處理排除清單中。
例如,使用 Microsoft Defender Antivirus:
開始 > 設定 > 隱私與安全 > Windows 安全性 > 開啟 Windows 安全性 > 病毒與威脅防護 > 管理設定 > 新增或移除排除項目 > 新增排除項目 > 處理程序
然後輸入 hugo.exe
並按下 新增 按鈕。
其他病毒掃描器也有類似的排除機制,請參閱其各自的文件。
範本度量
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/single.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 _default/list.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 _default/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 _default/_markup/render-codeblock.html
230.490038ms 8.865001ms 177.4615ms 0 0 0 26 shortcodes/template.html
176.921913ms 176.921913ms 176.921913ms 0 0 0 1 examples.tmpl
163.951469ms 14.904679ms 70.267953ms 0 0 0 11 articles/list.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 _default/single.html
146.785804ms 146.785804ms 146.785804ms 0 0 0 1 _default/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 _default/home.html
從左到右,欄位代表:
- 累計時長
- 執行範本所花費的累計時間。
- 平均時長
- 執行範本的平均時間。
- 最大時長
- 執行範本的最大時間。
- 快取潛力
- 以百分比顯示,任何具有 100% 快取潛力的部分範本應該使用
partialCached
函數來呼叫,而不是使用partial
函數。請參見下面的 快取 部分。 - 快取百分比
- 渲染範本被快取的次數除以範本執行的次數。
- 快取次數
- 渲染範本被快取的次數。
- 總次數
- 範本執行的總次數。
- 範本
- 範本的路徑,從佈局目錄相對表示。
快取
某些部分範本(如側邊欄或選單)在網站構建過程中會多次執行。根據範本中的內容和所需的輸出,範本可能會受益於快取,從而減少執行次數。 partialCached
範本函數為部分範本提供了快取功能。
計時器
使用 debug.Timer
函數來確定一段程式碼的執行時間,這對於找出範本中的性能瓶頸非常有用。詳細請參見 文檔。