template_timing_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. b.ResetTimer()
  108. b.RunParallel(func(pb *testing.PB) {
  109. var w bytes.Buffer
  110. for pb.Next() {
  111. if _, err := t.ExecuteFunc(&w, testTagFunc); err != nil {
  112. b.Fatalf("unexpected error: %s", err)
  113. }
  114. x := w.Bytes()
  115. if !bytes.Equal(x, resultBytes) {
  116. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
  117. }
  118. w.Reset()
  119. }
  120. })
  121. }
  122. func BenchmarkFastTemplateExecute(b *testing.B) {
  123. t, err := NewTemplate(source, "{{", "}}")
  124. if err != nil {
  125. b.Fatalf("error in template: %s", err)
  126. }
  127. b.ResetTimer()
  128. b.RunParallel(func(pb *testing.PB) {
  129. var w bytes.Buffer
  130. for pb.Next() {
  131. if _, err := t.Execute(&w, m); err != nil {
  132. b.Fatalf("unexpected error: %s", err)
  133. }
  134. x := w.Bytes()
  135. if !bytes.Equal(x, resultBytes) {
  136. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultBytes)
  137. }
  138. w.Reset()
  139. }
  140. })
  141. }
  142. func BenchmarkFastTemplateExecuteFuncString(b *testing.B) {
  143. t, err := NewTemplate(source, "{{", "}}")
  144. if err != nil {
  145. b.Fatalf("error in template: %s", err)
  146. }
  147. b.ResetTimer()
  148. b.RunParallel(func(pb *testing.PB) {
  149. for pb.Next() {
  150. x := t.ExecuteFuncString(testTagFunc)
  151. if x != result {
  152. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
  153. }
  154. }
  155. })
  156. }
  157. func BenchmarkFastTemplateExecuteString(b *testing.B) {
  158. t, err := NewTemplate(source, "{{", "}}")
  159. if err != nil {
  160. b.Fatalf("error in template: %s", err)
  161. }
  162. b.ResetTimer()
  163. b.RunParallel(func(pb *testing.PB) {
  164. for pb.Next() {
  165. x := t.ExecuteString(m)
  166. if x != result {
  167. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, result)
  168. }
  169. }
  170. })
  171. }
  172. func BenchmarkFastTemplateExecuteTagFunc(b *testing.B) {
  173. t, err := NewTemplate(source, "{{", "}}")
  174. if err != nil {
  175. b.Fatalf("error in template: %s", err)
  176. }
  177. mm := make(map[string]interface{})
  178. for k, v := range m {
  179. if k == "ref" {
  180. vv := v.([]byte)
  181. v = TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(url.QueryEscape(string(vv)))) })
  182. }
  183. mm[k] = v
  184. }
  185. b.ResetTimer()
  186. b.RunParallel(func(pb *testing.PB) {
  187. var w bytes.Buffer
  188. for pb.Next() {
  189. if _, err := t.Execute(&w, mm); err != nil {
  190. b.Fatalf("unexpected error: %s", err)
  191. }
  192. x := w.Bytes()
  193. if !bytes.Equal(x, resultEscapedBytes) {
  194. b.Fatalf("unexpected result\n%q\nExpected\n%q\n", x, resultEscapedBytes)
  195. }
  196. w.Reset()
  197. }
  198. })
  199. }
  200. func BenchmarkNewTemplate(b *testing.B) {
  201. b.RunParallel(func(pb *testing.PB) {
  202. for pb.Next() {
  203. _ = New(source, "{{", "}}")
  204. }
  205. })
  206. }
  207. func BenchmarkTemplateReset(b *testing.B) {
  208. b.RunParallel(func(pb *testing.PB) {
  209. t := New(source, "{{", "}}")
  210. for pb.Next() {
  211. t.Reset(source, "{{", "}}")
  212. }
  213. })
  214. }
  215. func BenchmarkTemplateResetExecuteFunc(b *testing.B) {
  216. b.RunParallel(func(pb *testing.PB) {
  217. t := New(source, "{{", "}}")
  218. var w bytes.Buffer
  219. for pb.Next() {
  220. t.Reset(source, "{{", "}}")
  221. t.ExecuteFunc(&w, testTagFunc)
  222. w.Reset()
  223. }
  224. })
  225. }
  226. func BenchmarkExecuteFunc(b *testing.B) {
  227. b.RunParallel(func(pb *testing.PB) {
  228. var bb bytes.Buffer
  229. for pb.Next() {
  230. ExecuteFunc(source, "{{", "}}", &bb, testTagFunc)
  231. bb.Reset()
  232. }
  233. })
  234. }
  235. func testTagFunc(w io.Writer, tag string) (int, error) {
  236. return w.Write(m[tag].([]byte))
  237. }