...
1 package ut
2
3 import (
4 "testing"
5
6 "github.com/go-playground/locales/en"
7 )
8
9 func BenchmarkBasicTranslation(b *testing.B) {
10
11 en := en.New()
12 ut := New(en, en)
13 loc, found := ut.FindTranslator("en")
14 if !found {
15 b.Fatalf("Expected '%t' Got '%t'", true, found)
16 }
17
18 translations := []struct {
19 key interface{}
20 trans string
21 expected error
22 override bool
23 }{
24 {
25 key: "welcome",
26 trans: "Welcome to the site",
27 expected: nil,
28 },
29 {
30 key: "welcome-user",
31 trans: "Welcome to the site {0}",
32 expected: nil,
33 },
34 {
35 key: "welcome-user2",
36 trans: "Welcome to the site {0}, your location is {1}",
37 expected: nil,
38 },
39 }
40
41 for _, tt := range translations {
42 if err := loc.Add(tt.key, tt.trans, tt.override); err != nil {
43 b.Fatalf("adding translation '%s' failed with key '%s'", tt.trans, tt.key)
44 }
45 }
46
47 var err error
48
49 b.ResetTimer()
50
51 b.Run("", func(b *testing.B) {
52 for i := 0; i < b.N; i++ {
53 if _, err = loc.T("welcome"); err != nil {
54 b.Error(err)
55 }
56 }
57 })
58
59 b.Run("Parallel", func(b *testing.B) {
60
61 b.RunParallel(func(pb *testing.PB) {
62
63 for pb.Next() {
64 if _, err = loc.T("welcome"); err != nil {
65 b.Error(err)
66 }
67 }
68 })
69 })
70
71 b.Run("With1Param", func(b *testing.B) {
72 for i := 0; i < b.N; i++ {
73 if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
74 b.Error(err)
75 }
76 }
77 })
78
79 b.Run("ParallelWith1Param", func(b *testing.B) {
80
81 b.RunParallel(func(pb *testing.PB) {
82
83 for pb.Next() {
84 if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
85 b.Error(err)
86 }
87 }
88 })
89 })
90
91 b.Run("With2Param", func(b *testing.B) {
92 for i := 0; i < b.N; i++ {
93 if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
94 b.Error(err)
95 }
96 }
97 })
98
99 b.Run("ParallelWith2Param", func(b *testing.B) {
100
101 b.RunParallel(func(pb *testing.PB) {
102
103 for pb.Next() {
104 if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
105 b.Error(err)
106 }
107 }
108 })
109 })
110 }
111
View as plain text