1 Star 0 Fork 0

Trister/universal-translator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
benchmarks_test.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
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)
}
}
})
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Trister/universal-translator.git
git@gitee.com:Trister/universal-translator.git
Trister
universal-translator
universal-translator
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385