Variable for time it took to build

Is there a variable for time it took to build the website? Something like ‘Built in 20ms’. I found a bunch topics asking for the variable for when the site was built but not how long it took to build.


Well, you can run hugo --templateMetrics to get the total build time in microseconds but it’s unstructured and just appears on stdout.

So, you could build a simple script with sed or awk to pull the figure from std-out, and pass it to an environment variable, before building hugo to a folder for deployment.

Then, you could grab what you pulled in your template like this:

 {{ with getenv "MY_BUILD_TIME" }}{{ . }}{{ end }}
1 Like

Basically this:

export MY_BUILD_TIME="$(hugo --templateMetrics | grep "Total in")"

… gets you that string in the MY_BUILD_TIME env var. It’s not perfect because, that string might change, but it will do in a pinch.

Assumes *nix or mac.


Wow, thanks for the quick reply! That looks like the best possible solution, I’ll try it out tomorrow and let you know how it goes. Thanks!

I asked this question a long time ago before realizing it was impossible to have this information available for your pages in Hugo.
Simply because Hugo only get this information when it is done building your pages’ html files where the information needed to be included.

Thumbs up to @RickCogley’s solution which get the info from a previous built! Never thought of that :slight_smile:

1 Like

Yeah it’s a little inefficient but, it’s possible to run it once to get the var, then run the actual hugo command to build as you need. You could also probably make a manifest json file, and stick it in there for reference.

1 Like

Just note that the “template metrics” flags (esp. the hint flag) is adding work to the build.

1 Like

I’m not great with bash so excuse the multi line code but the following code in my did the job:

buildTime="$(hugo | grep "Total in")"
sed -i "s/BUILDTIME/${buildTime}/g" public/index.html 
rsync -avz --delete public/ ${USER}@${HOST}:~/${DIR}
exit 0

The above code replaces the word BUILDTIME in my index.html with ‘Total in XXms’ before deploying the website to a server. I’m sure it can be done in a more elegant way but this works for me and it doesn’t affect the performance of the build either. Thanks!

The following code only gives you the time without the ‘Total in’. Once again this can be improved:

buildTime="$(hugo | grep "Total in")"
timeOnly="$(echo $buildTime | cut -c9-14)"
sed -i "s/BUILDTIME/${timeOnly}/g" public/index.html 
rsync -avz --delete public/ ${USER}@${HOST}:~/${DIR}
exit 0