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

0 Answered Questions

golang copy files takes too much time

1 Answered Questions

[SOLVED] No such template "xxx" in Golang HTML Web Apps

1 Answered Questions

[SOLVED] How to use "html/template" and "text/template" at the same time in Golang

1 Answered Questions

[SOLVED] Golang template generate values from array of strings error

2 Answered Questions

[SOLVED] Golang template function returns blank page

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

2 Answered Questions

[SOLVED] Golang templates for web application

  • 2016-11-10 12:11:14
  • avrono
  • 742 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
  • 8886 View
  • 9 Score
  • 4 Answer
  • Tags:   http browser go server

1 Answered Questions

[SOLVED] golang templates processing and generics

  • 2017-09-05 10:10:30
  • Sankar
  • 239 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
  • 553 View
  • 1 Score
  • 2 Answer
  • Tags:   go go-templates

4 Answered Questions

Sponsored Content