benchmarks_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package en
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-playground/locales/currency"
  6. )
  7. func BenchmarkFmtNumber(b *testing.B) {
  8. trans := New()
  9. f64 := float64(1234567.43)
  10. precision := uint64(2)
  11. b.ResetTimer()
  12. b.Run("", func(b *testing.B) {
  13. for i := 0; i < b.N; i++ {
  14. trans.FmtNumber(f64, precision)
  15. }
  16. })
  17. b.Run("Parallel", func(b *testing.B) {
  18. b.RunParallel(func(pb *testing.PB) {
  19. for pb.Next() {
  20. trans.FmtNumber(f64, precision)
  21. }
  22. })
  23. })
  24. }
  25. func BenchmarkFmtPercent(b *testing.B) {
  26. trans := New()
  27. f64 := float64(97.05)
  28. precision := uint64(2)
  29. b.ResetTimer()
  30. b.Run("", func(b *testing.B) {
  31. for i := 0; i < b.N; i++ {
  32. trans.FmtPercent(f64, precision)
  33. }
  34. })
  35. b.Run("Parallel", func(b *testing.B) {
  36. b.RunParallel(func(pb *testing.PB) {
  37. for pb.Next() {
  38. trans.FmtPercent(f64, precision)
  39. }
  40. })
  41. })
  42. }
  43. func BenchmarkFmtCurrency(b *testing.B) {
  44. trans := New()
  45. f64 := float64(1234567.43)
  46. precision := uint64(2)
  47. b.ResetTimer()
  48. b.Run("", func(b *testing.B) {
  49. for i := 0; i < b.N; i++ {
  50. trans.FmtCurrency(f64, precision, currency.USD)
  51. }
  52. })
  53. b.Run("Parallel", func(b *testing.B) {
  54. b.RunParallel(func(pb *testing.PB) {
  55. for pb.Next() {
  56. trans.FmtCurrency(f64, precision, currency.USD)
  57. }
  58. })
  59. })
  60. }
  61. func BenchmarkFmtAccounting(b *testing.B) {
  62. trans := New()
  63. f64 := float64(1234567.43)
  64. precision := uint64(2)
  65. b.ResetTimer()
  66. b.Run("", func(b *testing.B) {
  67. for i := 0; i < b.N; i++ {
  68. trans.FmtAccounting(f64, precision, currency.USD)
  69. }
  70. })
  71. b.Run("Parallel", func(b *testing.B) {
  72. b.RunParallel(func(pb *testing.PB) {
  73. for pb.Next() {
  74. trans.FmtAccounting(f64, precision, currency.USD)
  75. }
  76. })
  77. })
  78. }
  79. func BenchmarkFmtDate(b *testing.B) {
  80. trans := New()
  81. t := time.Now()
  82. b.ResetTimer()
  83. b.Run("FmtDateShort", func(b *testing.B) {
  84. for i := 0; i < b.N; i++ {
  85. trans.FmtDateShort(t)
  86. }
  87. })
  88. b.Run("FmtDateShortParallel", func(b *testing.B) {
  89. b.RunParallel(func(pb *testing.PB) {
  90. for pb.Next() {
  91. trans.FmtDateShort(t)
  92. }
  93. })
  94. })
  95. b.Run("FmtDateMedium", func(b *testing.B) {
  96. for i := 0; i < b.N; i++ {
  97. trans.FmtDateMedium(t)
  98. }
  99. })
  100. b.Run("FmtDateMediumParallel", func(b *testing.B) {
  101. b.RunParallel(func(pb *testing.PB) {
  102. for pb.Next() {
  103. trans.FmtDateMedium(t)
  104. }
  105. })
  106. })
  107. b.Run("FmtDateLong", func(b *testing.B) {
  108. for i := 0; i < b.N; i++ {
  109. trans.FmtDateLong(t)
  110. }
  111. })
  112. b.Run("FmtDateLongParallel", func(b *testing.B) {
  113. b.RunParallel(func(pb *testing.PB) {
  114. for pb.Next() {
  115. trans.FmtDateLong(t)
  116. }
  117. })
  118. })
  119. b.Run("FmtDateFull", func(b *testing.B) {
  120. for i := 0; i < b.N; i++ {
  121. trans.FmtDateFull(t)
  122. }
  123. })
  124. b.Run("FmtDateFullParallel", func(b *testing.B) {
  125. b.RunParallel(func(pb *testing.PB) {
  126. for pb.Next() {
  127. trans.FmtDateFull(t)
  128. }
  129. })
  130. })
  131. }
  132. func BenchmarkFmtTime(b *testing.B) {
  133. trans := New()
  134. t := time.Now()
  135. b.ResetTimer()
  136. b.Run("FmtTimeShort", func(b *testing.B) {
  137. for i := 0; i < b.N; i++ {
  138. trans.FmtTimeShort(t)
  139. }
  140. })
  141. b.Run("FmtTimeShortParallel", func(b *testing.B) {
  142. b.RunParallel(func(pb *testing.PB) {
  143. for pb.Next() {
  144. trans.FmtTimeShort(t)
  145. }
  146. })
  147. })
  148. b.Run("FmtTimeMedium", func(b *testing.B) {
  149. for i := 0; i < b.N; i++ {
  150. trans.FmtTimeMedium(t)
  151. }
  152. })
  153. b.Run("FmtTimeMediumParallel", func(b *testing.B) {
  154. b.RunParallel(func(pb *testing.PB) {
  155. for pb.Next() {
  156. trans.FmtTimeMedium(t)
  157. }
  158. })
  159. })
  160. b.Run("FmtTimeLong", func(b *testing.B) {
  161. for i := 0; i < b.N; i++ {
  162. trans.FmtTimeLong(t)
  163. }
  164. })
  165. b.Run("FmtTimeLongParallel", func(b *testing.B) {
  166. b.RunParallel(func(pb *testing.PB) {
  167. for pb.Next() {
  168. trans.FmtTimeLong(t)
  169. }
  170. })
  171. })
  172. b.Run("FmtTimeFull", func(b *testing.B) {
  173. for i := 0; i < b.N; i++ {
  174. trans.FmtTimeFull(t)
  175. }
  176. })
  177. b.Run("FmtTimeFullParallel", func(b *testing.B) {
  178. b.RunParallel(func(pb *testing.PB) {
  179. for pb.Next() {
  180. trans.FmtTimeFull(t)
  181. }
  182. })
  183. })
  184. }