性能
病毒扫描
病毒扫描程序是系统保护的基本组件,但对于像 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 函数确定代码块的执行时间,这对于查找模板中的性能瓶颈非常有用。请参阅 详细信息。