It takes too much time when using "template" package to generate a dynamic web page to client in Golang
Asked Answered
S

1

8

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.

Subhuman answered 11/2, 2015 at 10:15 Comment(2)
I think we need more information to be able to give a good answer.Imtiaz
You can try to profile it to see what it is doing by adding import _ "net/http/pprof" . See blog.golang.org/profiling-go-programs for more information.Amylolysis
D
18

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)
    }
}
Dilution answered 11/2, 2015 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.