性能
病毒扫描
病毒扫描程序是系统保护的重要组成部分,但对于像 Hugo 这样频繁读写磁盘的应用程序来说,其性能影响可能非常严重。例如,使用 Microsoft Defender Antivirus,某些站点的构建时间可能会增加 400% 或更多。
在构建站点之前,您的病毒扫描程序已经评估了项目目录中的文件。在构建站点时再次扫描它们是多余的。为了提高性能,请将 Hugo 的可执行文件添加到病毒扫描程序的进程排除列表中。
例如,使用 Microsoft Defender Antivirus:
开始 > 设置 > 隐私和安全 > Windows 安全性 > 打开 Windows 安全性 > 病毒和威胁防护 > 管理设置 > 添加或删除排除项 > 添加排除项 > 进程
然后输入 hugo.exe
并按 添加 按钮。
其他病毒扫描程序具有类似的排除机制。请参阅其各自的文档。
模板指标
Hugo 速度很快,但效率低下的模板会影响性能。启用模板指标来确定哪些模板花费的时间最多,并确定缓存机会:
hugo --templateMetrics --templateMetricsHints
结果将如下所示:
模板指标:
累积时间 平均时间 最大时间 缓存潜力 缓存百分比 缓存次数 总次数 模板
---------- -------- -------- --------- ------- ------ ----- --------
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
函数来调用。请参见下面的 缓存 部分。 - 缓存百分比
- 渲染的模板被缓存的次数除以模板被执行的次数。
- 缓存次数
- 渲染的模板被缓存的次数。
- 总次数
- 模板被执行的次数。
- 模板
- 模板相对于 layouts 目录的路径。
缓存
某些部分模板(例如侧边栏或菜单)在站点构建期间会被执行多次。根据部分模板中的内容和所需的输出,模板可能会受益于缓存以减少执行次数。 partialCached
模板函数为部分模板提供了缓存功能。
计时器
使用 debug.Timer
函数确定代码块的执行时间,这对于查找模板中的性能瓶颈很有用。请参见 详情 。