template_timing_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package fasttemplate
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/url"
  7. "strings"
  8. "testing"
  9. "text/template"
  10. )
  11. var (
  12. source = "http://{{uid}}.foo.bar.com/?cb={{cb}}{{width}}&width={{width}}&height={{height}}&timeout={{timeout}}&uid={{uid}}&subid={{subid}}&ref={{ref}}"
  13. 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"
  14. 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"
  15. resultBytes = []byte(result)
  16. resultEscapedBytes = []byte(resultEscaped)
  17. m = map[string]interface{}{
  18. "cb": []byte("1234"),
  19. "width": []byte("1232"),
  20. "height": []byte("123"),
  21. "timeout": []byte("123123"),
  22. "uid": []byte("aaasdf"),
  23. "subid": []byte("asdfds"),
  24. "ref": []byte("http://google.com/aaa/bbb/ccc"),
  25. }
  26. )
  27. func map2slice(m map[string]interface{}) []string {
  28. var a []string
  29. for k, v := range m {
  30. a = append(a, "{{"+k+"}}", string(v.([]byte)))
  31. }
  32. return a
  33. }
  34. func BenchmarkFmtFprintf(b *testing.B) {
  35. b.RunParallel(func(pb *testing.PB) {
  36. var w bytes.Buffer
  37. for pb.Next() {
  38. fmt.Fprintf(&w,
  39. "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",
  40. m["cb"], m["width"], m["height"], m["timeout"], m["uid"], m["subid"], m["ref"])
  41. x := w.Bytes()
  42. if !bytes.Equal(x, resultBytes) {
  43. b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
  44. }
  45. w.Reset()
  46. }
  47. })
  48. }
  49. func BenchmarkStringsReplace(b *testing.B) {
  50. mSlice := map2slice(m)
  51. b.ResetTimer()
  52. b.RunParallel(func(pb *testing.PB) {
  53. for pb.Next() {
  54. x := source
  55. for i := 0; i < len(mSlice); i += 2 {
  56. x = strings.Replace(x, mSlice[i], mSlice[i+1], -1)
  57. }
  58. if x != result {
  59. b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
  60. }
  61. }
  62. })
  63. }
  64. func BenchmarkStringsReplacer(b *testing.B) {
  65. mSlice := map2slice(m)
  66. b.ResetTimer()
  67. b.RunParallel(func(pb *testing.PB) {
  68. for pb.Next() {
  69. r := strings.NewReplacer(mSlice...)
  70. x := r.Replace(source)
  71. if x != result {
  72. b.Fatalf("Unexpected result\n%q\nExpected\n%q\n", x, result)
  73. }
  74. }
  75. })
  76. }
  77. func BenchmarkTextTemplate(b *testing.B) {
  78. s := strings.Replace(source, "{{", "{{.", -1)
  79. t, err := template.New("test").Parse(s)
  80. if err != nil {
  81. b.Fatalf("Error when parsing template: %s", err)
  82. }
  83. mm := make(map[string]string)
  84. for k, v := range m {
  85. mm[k] = string(v.([]byte))
  86. }
  87. b.ResetTimer()
  88. b.RunParallel(func(pb *testing.PB) {
  89. var w bytes.Buffer
  90. for pb.Next() {
  91. if err := t.Execute(&w, mm); err != nil {
  92. b.Fatalf("error when executing template: %s", err)
  93. }
  94. x := w.Bytes()
  95. if !bytes.Equal(x, resultBytes) {
  96. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
  97. }
  98. w.Reset()
  99. }
  100. })
  101. }
  102. func BenchmarkFastTemplateExecuteFunc(b *testing.B) {
  103. t, err := NewTemplate(source, "{{", "}}")
  104. if err != nil {
  105. b.Fatalf("error in template: %s", err)
  106. }
  107. f := func(w io.Writer, tag string) (int, error) {
  108. return w.Write(m[tag].([]byte))
  109. }
  110. b.ResetTimer()
  111. b.RunParallel(func(pb *testing.PB) {
  112. var w bytes.Buffer
  113. for pb.Next() {
  114. if _, err := t.ExecuteFunc(&w, f); err != nil {
  115. b.Fatalf("unexpected error: %s", err)
  116. }
  117. x := w.Bytes()
  118. if !bytes.Equal(x, resultBytes) {
  119. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
  120. }
  121. w.Reset()
  122. }
  123. })
  124. }
  125. func BenchmarkFastTemplateExecute(b *testing.B) {
  126. t, err := NewTemplate(source, "{{", "}}")
  127. if err != nil {
  128. b.Fatalf("error in template: %s", err)
  129. }
  130. b.ResetTimer()
  131. b.RunParallel(func(pb *testing.PB) {
  132. var w bytes.Buffer
  133. for pb.Next() {
  134. if _, err := t.Execute(&w, m); err != nil {
  135. b.Fatalf("unexpected error: %s", err)
  136. }
  137. x := w.Bytes()
  138. if !bytes.Equal(x, resultBytes) {
  139. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
  140. }
  141. w.Reset()
  142. }
  143. })
  144. }
  145. func BenchmarkFastTemplateExecuteFuncString(b *testing.B) {
  146. t, err := NewTemplate(source, "{{", "}}")
  147. if err != nil {
  148. b.Fatalf("error in template: %s", err)
  149. }
  150. f := func(w io.Writer, tag string) (int, error) {
  151. return w.Write(m[tag].([]byte))
  152. }
  153. b.ResetTimer()
  154. b.RunParallel(func(pb *testing.PB) {
  155. for pb.Next() {
  156. x := t.ExecuteFuncString(f)
  157. if x != result {
  158. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
  159. }
  160. }
  161. })
  162. }
  163. func BenchmarkFastTemplateExecuteString(b *testing.B) {
  164. t, err := NewTemplate(source, "{{", "}}")
  165. if err != nil {
  166. b.Fatalf("error in template: %s", err)
  167. }
  168. b.ResetTimer()
  169. b.RunParallel(func(pb *testing.PB) {
  170. for pb.Next() {
  171. x := t.ExecuteString(m)
  172. if x != result {
  173. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
  174. }
  175. }
  176. })
  177. }
  178. func BenchmarkFastTemplateExecuteTagFunc(b *testing.B) {
  179. t, err := NewTemplate(source, "{{", "}}")
  180. if err != nil {
  181. b.Fatalf("error in template: %s", err)
  182. }
  183. mm := make(map[string]interface{})
  184. for k, v := range m {
  185. if k == "ref" {
  186. vv := v.([]byte)
  187. v = TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(url.QueryEscape(string(vv)))) })
  188. }
  189. mm[k] = v
  190. }
  191. b.ResetTimer()
  192. b.RunParallel(func(pb *testing.PB) {
  193. var w bytes.Buffer
  194. for pb.Next() {
  195. if _, err := t.Execute(&w, mm); err != nil {
  196. b.Fatalf("unexpected error: %s", err)
  197. }
  198. x := w.Bytes()
  199. if !bytes.Equal(x, resultEscapedBytes) {
  200. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultEscapedBytes)
  201. }
  202. w.Reset()
  203. }
  204. })
  205. }
  206. func BenchmarkNewTemplate(b *testing.B) {
  207. b.RunParallel(func(pb *testing.PB) {
  208. for pb.Next() {
  209. _ = New(source, "{{", "}}")
  210. }
  211. })
  212. }
  213. func BenchmarkTemplateReset(b *testing.B) {
  214. b.RunParallel(func(pb *testing.PB) {
  215. t := New(source, "{{", "}}")
  216. for pb.Next() {
  217. t.Reset(source, "{{", "}}")
  218. }
  219. })
  220. }