| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package fasttemplate
- import (
- "bytes"
- "fmt"
- "io"
- "net/url"
- "strings"
- "testing"
- "text/template"
- )
- var (
- source = "http://{{uid}}.foo.bar.com/?cb={{cb}}{{width}}&width={{width}}&height={{height}}&timeout={{timeout}}&uid={{uid}}&subid={{subid}}&ref={{ref}}"
- result = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http://google.com/aaa/bbb/ccc"
- resultEscaped = "http://aaasdf.foo.bar.com/?cb=12341232&width=1232&height=123&timeout=123123&uid=aaasdf&subid=asdfds&ref=http%3A%2F%2Fgoogle.com%2Faaa%2Fbbb%2Fccc"
- resultBytes = []byte(result)
- resultEscapedBytes = []byte(resultEscaped)
- m = map[string]interface{}{
- "cb": []byte("1234"),
- "width": []byte("1232"),
- "height": []byte("123"),
- "timeout": []byte("123123"),
- "uid": []byte("aaasdf"),
- "subid": []byte("asdfds"),
- "ref": []byte("http://google.com/aaa/bbb/ccc"),
- }
- )
- func map2slice(m map[string]interface{}) []string {
- var a []string
- for k, v := range m {
- a = append(a, "{{"+k+"}}", string(v.([]byte)))
- }
- return a
- }
- func BenchmarkFmtFprintf(b *testing.B) {
- b.RunParallel(func(pb *testing.PB) {
- var w bytes.Buffer
- for pb.Next() {
- fmt.Fprintf(&w,
- "http://%[5]s.foo.bar.com/?cb=%[1]s%[2]s&width=%[2]s&height=%[3]s&timeout=%[4]s&uid=%[5]s&subid=%[6]s&ref=%[7]s",
- m["cb"], m["width"], m["height"], m["timeout"], m["uid"], m["subid"], m["ref"])
- x := w.Bytes()
- if !bytes.Equal(x, resultBytes) {
- b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
- }
- w.Reset()
- }
- })
- }
- func BenchmarkStringsReplace(b *testing.B) {
- mSlice := map2slice(m)
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- x := source
- for i := 0; i < len(mSlice); i += 2 {
- x = strings.Replace(x, mSlice[i], mSlice[i+1], -1)
- }
- if x != result {
- b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
- }
- }
- })
- }
- func BenchmarkStringsReplacer(b *testing.B) {
- mSlice := map2slice(m)
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- r := strings.NewReplacer(mSlice...)
- x := r.Replace(source)
- if x != result {
- b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
- }
- }
- })
- }
- func BenchmarkTextTemplate(b *testing.B) {
- s := strings.Replace(source, "{{", "{{.", -1)
- t, err := template.New("test").Parse(s)
- if err != nil {
- b.Fatalf("Error when parsing template: %s", err)
- }
- mm := make(map[string]string)
- for k, v := range m {
- mm[k] = string(v.([]byte))
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- var w bytes.Buffer
- for pb.Next() {
- if err := t.Execute(&w, mm); err != nil {
- b.Fatalf("error when executing template: %s", err)
- }
- x := w.Bytes()
- if !bytes.Equal(x, resultBytes) {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
- }
- w.Reset()
- }
- })
- }
- func BenchmarkFastTemplateExecuteFunc(b *testing.B) {
- t, err := NewTemplate(source, "{{", "}}")
- if err != nil {
- b.Fatalf("error in template: %s", err)
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- var w bytes.Buffer
- for pb.Next() {
- if _, err := t.ExecuteFunc(&w, testTagFunc); err != nil {
- b.Fatalf("unexpected error: %s", err)
- }
- x := w.Bytes()
- if !bytes.Equal(x, resultBytes) {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
- }
- w.Reset()
- }
- })
- }
- func BenchmarkFastTemplateExecute(b *testing.B) {
- t, err := NewTemplate(source, "{{", "}}")
- if err != nil {
- b.Fatalf("error in template: %s", err)
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- var w bytes.Buffer
- for pb.Next() {
- if _, err := t.Execute(&w, m); err != nil {
- b.Fatalf("unexpected error: %s", err)
- }
- x := w.Bytes()
- if !bytes.Equal(x, resultBytes) {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
- }
- w.Reset()
- }
- })
- }
- func BenchmarkFastTemplateExecuteFuncString(b *testing.B) {
- t, err := NewTemplate(source, "{{", "}}")
- if err != nil {
- b.Fatalf("error in template: %s", err)
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- x := t.ExecuteFuncString(testTagFunc)
- if x != result {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
- }
- }
- })
- }
- func BenchmarkFastTemplateExecuteString(b *testing.B) {
- t, err := NewTemplate(source, "{{", "}}")
- if err != nil {
- b.Fatalf("error in template: %s", err)
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- x := t.ExecuteString(m)
- if x != result {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
- }
- }
- })
- }
- func BenchmarkFastTemplateExecuteTagFunc(b *testing.B) {
- t, err := NewTemplate(source, "{{", "}}")
- if err != nil {
- b.Fatalf("error in template: %s", err)
- }
- mm := make(map[string]interface{})
- for k, v := range m {
- if k == "ref" {
- vv := v.([]byte)
- v = TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(url.QueryEscape(string(vv)))) })
- }
- mm[k] = v
- }
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- var w bytes.Buffer
- for pb.Next() {
- if _, err := t.Execute(&w, mm); err != nil {
- b.Fatalf("unexpected error: %s", err)
- }
- x := w.Bytes()
- if !bytes.Equal(x, resultEscapedBytes) {
- b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultEscapedBytes)
- }
- w.Reset()
- }
- })
- }
- func BenchmarkNewTemplate(b *testing.B) {
- b.RunParallel(func(pb *testing.PB) {
- for pb.Next() {
- _ = New(source, "{{", "}}")
- }
- })
- }
- func BenchmarkTemplateReset(b *testing.B) {
- b.RunParallel(func(pb *testing.PB) {
- t := New(source, "{{", "}}")
- for pb.Next() {
- t.Reset(source, "{{", "}}")
- }
- })
- }
- func BenchmarkTemplateResetExecuteFunc(b *testing.B) {
- b.RunParallel(func(pb *testing.PB) {
- t := New(source, "{{", "}}")
- var w bytes.Buffer
- for pb.Next() {
- t.Reset(source, "{{", "}}")
- t.ExecuteFunc(&w, testTagFunc)
- w.Reset()
- }
- })
- }
- func BenchmarkExecuteFunc(b *testing.B) {
- b.RunParallel(func(pb *testing.PB) {
- var bb bytes.Buffer
- for pb.Next() {
- ExecuteFunc(source, "{{", "}}", &bb, testTagFunc)
- bb.Reset()
- }
- })
- }
- func testTagFunc(w io.Writer, tag string) (int, error) {
- return w.Write(m[tag].([]byte))
- }
|