代码拉取完成,页面将自动刷新
package ut
import (
"testing"
"github.com/go-playground/locales/en"
)
func BenchmarkBasicTranslation(b *testing.B) {
en := en.New()
ut := New(en, en)
loc, found := ut.FindTranslator("en")
if !found {
b.Fatalf("Expected '%t' Got '%t'", true, found)
}
translations := []struct {
key interface{}
trans string
expected error
override bool
}{
{
key: "welcome",
trans: "Welcome to the site",
expected: nil,
},
{
key: "welcome-user",
trans: "Welcome to the site {0}",
expected: nil,
},
{
key: "welcome-user2",
trans: "Welcome to the site {0}, your location is {1}",
expected: nil,
},
}
for _, tt := range translations {
if err := loc.Add(tt.key, tt.trans, tt.override); err != nil {
b.Fatalf("adding translation '%s' failed with key '%s'", tt.trans, tt.key)
}
}
var err error
b.ResetTimer()
b.Run("", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err = loc.T("welcome"); err != nil {
b.Error(err)
}
}
})
b.Run("Parallel", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err = loc.T("welcome"); err != nil {
b.Error(err)
}
}
})
})
b.Run("With1Param", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
b.Error(err)
}
}
})
b.Run("ParallelWith1Param", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
b.Error(err)
}
}
})
})
b.Run("With2Param", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
b.Error(err)
}
}
})
b.Run("ParallelWith2Param", func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
b.Error(err)
}
}
})
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。