By catric mia


2015-02-11 10:15:02 8 Comments

It is so slow when using template package to generate a dynamic web page to client.

Testing code as below, golang 1.4.1

http.Handle("/js/", (http.FileServer(http.Dir(webpath))))
http.Handle("/css/", (http.FileServer(http.Dir(webpath))))
http.Handle("/img/", (http.FileServer(http.Dir(webpath))))
http.HandleFunc("/test", TestHandler)


func TestHandler(w http.ResponseWriter, r *http.Request) {

    Log.Info("Entering TestHandler ...")
    r.ParseForm()

    filename := NiConfig.webpath + "/test.html"

    t, err := template.ParseFiles(filename)
    if err != nil {
        Log.Error("template.ParseFiles err = %v", err)
    } 

    t.Execute(w, nil)
}

According to the log, I found that it took about 3 seconds in t.Execute(w, nil), I do not know why it uses so much time. I also tried Apache server to test test.html, it responded very fast.

1 comments

@icza 2015-02-11 11:43:26

You should not parse templates every time you serve a request!

There is a significant time delay to read a file, parse its content and build the template. Also since templates do not change (varying parts should be parameters!) you only have to read and parse a template once.
Also parsing and creating the template each time when serving a request generates lots of values in memory which are then thrown away (because they are not reused) giving additional work for the garbage collector.

Parse the templates when your application starts, store it in a variable, and you only have to execute the template when a request comes in. For example:

var t *template.Template

func init() {
    filename := NiConfig.webpath + "/test.html"
    t = template.Must(template.ParseFiles(filename))

    http.HandleFunc("/test", TestHandler)
}

func TestHandler(w http.ResponseWriter, r *http.Request) {
    Log.Info("Entering TestHandler ...")
    // Template is ready, just Execute it
    if err := t.Execute(w, nil); err != nil {
        log.Printf("Failed to execute template: %v", err)
    }
}

@catric mia 2015-02-11 12:41:11

Got it! Thank you so much!

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Golang template function returns blank page

  • 2018-05-16 16:52:20
  • Devasia Joseph
  • 432 View
  • 0 Score
  • 2 Answer
  • Tags:   go go-templates

2 Answered Questions

[SOLVED] Golang templates for web application

  • 2016-11-10 12:11:14
  • avrono
  • 648 View
  • 1 Score
  • 2 Answer
  • Tags:   go go-templates

4 Answered Questions

[SOLVED] How start web server to open page in browser in golang?

  • 2016-09-04 18:49:25
  • Ciasto piekarz
  • 7778 View
  • 8 Score
  • 4 Answer
  • Tags:   http browser go server

2 Answered Questions

[SOLVED] How to set different content types when using HTML/TEMPLATE package in Go

1 Answered Questions

[SOLVED] golang templates processing and generics

  • 2017-09-05 10:10:30
  • Sankar
  • 209 View
  • 0 Score
  • 1 Answer
  • Tags:   templates go

2 Answered Questions

[SOLVED] Get variables dynamically using Golang template

  • 2017-07-28 22:30:44
  • Idir Ouhab Meskine
  • 510 View
  • 1 Score
  • 2 Answer
  • Tags:   go go-templates

4 Answered Questions

0 Answered Questions

HttpRequest.GetResponse() taking too much time

1 Answered Questions

[SOLVED] How to limit client IP address when using golang http package

  • 2016-06-18 12:26:49
  • Will Lee
  • 2440 View
  • 3 Score
  • 1 Answer
  • Tags:   http go

0 Answered Questions

Accept() take too long time to return in golang

  • 2014-09-18 03:12:37
  • frank.lin
  • 205 View
  • 0 Score
  • 0 Answer
  • Tags:   networking go

Sponsored Content