bou.ke/statictemplate@v0.0.0-20180821122055-510411a5e7dd/example/example_test.go (about)

     1  package example_test
     2  
     3  import (
     4  	"bou.ke/statictemplate/example"
     5  	staticTemplate "bou.ke/statictemplate/example/template"
     6  	"bytes"
     7  	"fmt"
     8  	"testing"
     9  	"text/template"
    10  )
    11  
    12  var testData []example.Post
    13  
    14  func init() {
    15  	for i := 0; i < 100; i++ {
    16  		testData = append(testData, example.Post{
    17  			Title: fmt.Sprintf("Post %d", i),
    18  			Body:  "Very good post",
    19  		})
    20  	}
    21  }
    22  
    23  func BenchmarkStaticTemplate(b *testing.B) {
    24  	for n := 0; n < b.N; n++ {
    25  		var b bytes.Buffer
    26  		if err := staticTemplate.Index(&b, testData); err != nil {
    27  			panic(err)
    28  		}
    29  	}
    30  }
    31  
    32  func BenchmarkDynamicTemplate(b *testing.B) {
    33  	t := template.Must(template.ParseGlob("./template/*.tmpl"))
    34  	for n := 0; n < b.N; n++ {
    35  		var b bytes.Buffer
    36  		if err := t.ExecuteTemplate(&b, "index.tmpl", testData); err != nil {
    37  			panic(err)
    38  		}
    39  	}
    40  }