benchmarks_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package ut
  2. import "testing"
  3. func BenchmarkBasicTranslation(b *testing.B) {
  4. ut, _ := New("en", "en")
  5. loc := ut.FindTranslator("en")
  6. loc.Add("welcome", "Welcome to the site")
  7. loc.Add("welcome-user", "Welcome to the site {0}")
  8. loc.Add("welcome-user2", "Welcome to the site {0}, your location is {1}")
  9. b.Run("", func(b *testing.B) {
  10. for i := 0; i < b.N; i++ {
  11. loc.T("welcome")
  12. }
  13. })
  14. b.Run("Parallel", func(b *testing.B) {
  15. b.RunParallel(func(pb *testing.PB) {
  16. for pb.Next() {
  17. loc.T("welcome")
  18. }
  19. })
  20. })
  21. b.Run("With1Param", func(b *testing.B) {
  22. for i := 0; i < b.N; i++ {
  23. loc.T("welcome-user", "Joeybloggs")
  24. }
  25. })
  26. b.Run("ParallelWith1Param", func(b *testing.B) {
  27. b.RunParallel(func(pb *testing.PB) {
  28. for pb.Next() {
  29. loc.T("welcome-user", "Joeybloggs")
  30. }
  31. })
  32. })
  33. b.Run("With2Param", func(b *testing.B) {
  34. for i := 0; i < b.N; i++ {
  35. loc.T("welcome-user2", "Joeybloggs", "/dev/tty0")
  36. }
  37. })
  38. b.Run("ParallelWith2Param", func(b *testing.B) {
  39. b.RunParallel(func(pb *testing.PB) {
  40. for pb.Next() {
  41. loc.T("welcome-user2", "Joeybloggs", "/dev/tty0")
  42. }
  43. })
  44. })
  45. }