Debian Codegraph @

docs/content/en/troubleshooting/performance.md

raw
1 ---
2 title: Performance
3 description: Tools and suggestions for evaluating and improving performance.
4 categories: []
5 keywords: []
6 aliases: [/troubleshooting/build-performance/]
7 ---
8
9 ## Virus scanning
10
11 Virus scanners are an essential component of system protection, but the performance impact can be severe for applications like Hugo that frequently read and write to disk. For example, with Microsoft Defender Antivirus, build times for some sites may increase by 400% or more.
12
13 Before building a site, your virus scanner has already evaluated the files in your project directory. Scanning them again while building the site is superfluous. To improve performance, add Hugo's executable to your virus scanner's process exclusion list.
14
15 For example, with Microsoft Defender Antivirus:
16
17 **Start** > **Settings** > **Privacy & security** > **Windows Security** > **Open Windows Security** > **Virus & threat protection** > **Manage settings** > **Add or remove exclusions** > **Add an exclusion** > **Process**
18
19 Then type `hugo.exe` add press the **Add** button.
20
21 > [!NOTE]
22 > Virus scanning exclusions are common, but use caution when changing these settings. See the [Microsoft Defender Antivirus documentation][] for details.
23
24 Other virus scanners have similar exclusion mechanisms. See their respective documentation.
25
26 ## Template metrics
27
28 Hugo is fast, but inefficient templates impede performance. Enable template metrics to determine which templates take the most time, and to identify caching opportunities:
29
30 ```sh
31 hugo build --templateMetrics --templateMetricsHints
32 ```
33
34 The result will look something like this:
35
36 ```text
37 Template Metrics:
38
39 cumulative average maximum cache percent cached total
40 duration duration duration potential cached count count template
41 ---------- -------- -------- --------- ------- ------ ----- --------
42 36.037476822s 135.990478ms 225.765245ms 11 0 0 265 _partials/head.html
43 35.920040902s 164.018451ms 233.475072ms 0 0 0 219 articles/page.html
44 34.163268129s 128.917992ms 224.816751ms 23 0 0 265 _partials/head/meta/opengraph.html
45 1.041227437s 3.92916ms 186.303376ms 47 0 0 265 _partials/head/meta/schema.html
46 805.628827ms 27.780304ms 114.678523ms 0 0 0 29 section.html
47 624.08354ms 15.221549ms 108.420729ms 8 0 0 41 _partials/utilities/render-page-collection.html
48 545.968801ms 775.523µs 105.045775ms 0 0 0 704 summary.html
49 334.680981ms 1.262947ms 127.412027ms 100 0 0 265 _partials/head/js.html
50 272.763205ms 2.050851ms 24.371757ms 0 0 0 133 _markup/render-codeblock.html
51 163.951469ms 14.904679ms 70.267953ms 0 0 0 11 articles/section.html
52 153.07021ms 577.623µs 73.593597ms 100 0 0 265 _partials/head/init.html
53 150.910984ms 150.910984ms 150.910984ms 0 0 0 1 page.html
54 146.785804ms 146.785804ms 146.785804ms 0 0 0 1 contact.html
55 115.364617ms 115.364617ms 115.364617ms 0 0 0 1 authors/term.html
56 87.392071ms 329.781µs 10.687132ms 100 0 0 265 _partials/head/css.html
57 86.803122ms 86.803122ms 86.803122ms 0 0 0 1 home.html
58 ```
59
60 From left to right, the columns represent:
61
62 cumulative duration
63 : The cumulative time spent executing the template.
64
65 average duration
66 : The average time spent executing the template.
67
68 maximum duration
69 : The maximum time spent executing the template.
70
71 cache potential
72 : Displayed as a percentage, any _partial_ template with a 100% cache potential should be called with the [`partialCached`][] function instead of the [`partial`][] function. See the [caching](#caching) section below.
73
74 > [!WARNING]
75 > A 100% cache potential is calculated by comparing each execution's rendered output to the first execution's output, so it cannot detect side effects such as calls to [`warnf`][] or [`errorf`][], or execution order dependencies such as a conditional based on [`IsHome`][]. A _partial_ template that produces no visible output but performs validation, logging, or conditional logic based on context can show 100% cache potential even though caching it would suppress that behavior for all but the first invocation. Review the template's logic before switching it to `partialCached`.
76
77 percent cached
78 : The number of times the rendered templated was cached divided by the number of times the template was executed.
79
80 cached count
81 : The number of times the rendered templated was cached.
82
83 total count
84 : The number of times the template was executed.
85
86 template
87 : The path to the template, relative to the `layouts` directory.
88
89 > [!NOTE]
90 > Hugo builds pages in parallel where multiple pages are generated simultaneously. Because of this parallelism, the sum of "cumulative duration" values is usually greater than the actual time it takes to build a site.
91
92 ## Caching
93
94 Some _partial_ templates such as sidebars or menus are executed many times during a site build. Depending on the content within the _partial_ template and the desired output, the template may benefit from caching to reduce the number of executions. The [`partialCached`][] function provides caching capabilities for _partial_ templates.
95
96 > [!NOTE]
97 > Note that you can create cached variants of each _partial_ template by passing additional arguments to `partialCached` beyond the initial context. See the `partialCached` documentation for more details.
98
99 ## Timers
100
101 Use the [`debug.Timer`][] function to determine execution time for a block of code, useful for finding performance bottlenecks in templates.
102
103 [Microsoft Defender Antivirus documentation]: https://support.microsoft.com/en-us/topic/how-to-add-a-file-type-or-process-exclusion-to-windows-security-e524cbc2-3975-63c2-f9d1-7c2eb5331e53
104 [`debug.Timer`]: /functions/debug/timer/
105 [`errorf`]: /functions/fmt/errorf/
106 [`IsHome`]: /methods/page/ishome/
107 [`partialCached`]: /functions/partials/includecached/
108 [`partial`]: /functions/partials/include/
109 [`warnf`]: /functions/fmt/warnf/