benchmark_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build go1.7
  5. package precis
  6. import (
  7. "testing"
  8. "golang.org/x/text/internal/testtext"
  9. )
  10. var benchData = []struct{ name, str string }{
  11. {"ASCII", "Malvolio"},
  12. {"NotNormalized", "abcdefg\u0301\u031f"},
  13. {"Arabic", "دبي"},
  14. {"Hangul", "동일조건변경허락"},
  15. }
  16. var benchProfiles = []struct {
  17. name string
  18. p *Profile
  19. }{
  20. {"FreeForm", NewFreeform()},
  21. {"Nickname", Nickname},
  22. {"OpaqueString", OpaqueString},
  23. {"UsernameCaseMapped", UsernameCaseMapped},
  24. {"UsernameCasePreserved", UsernameCasePreserved},
  25. }
  26. func doBench(b *testing.B, f func(b *testing.B, p *Profile, s string)) {
  27. for _, bp := range benchProfiles {
  28. for _, d := range benchData {
  29. testtext.Bench(b, bp.name+"/"+d.name, func(b *testing.B) {
  30. f(b, bp.p, d.str)
  31. })
  32. }
  33. }
  34. }
  35. func BenchmarkString(b *testing.B) {
  36. doBench(b, func(b *testing.B, p *Profile, s string) {
  37. for i := 0; i < b.N; i++ {
  38. p.String(s)
  39. }
  40. })
  41. }
  42. func BenchmarkBytes(b *testing.B) {
  43. doBench(b, func(b *testing.B, p *Profile, s string) {
  44. src := []byte(s)
  45. b.ResetTimer()
  46. for i := 0; i < b.N; i++ {
  47. p.Bytes(src)
  48. }
  49. })
  50. }
  51. func BenchmarkAppend(b *testing.B) {
  52. doBench(b, func(b *testing.B, p *Profile, s string) {
  53. src := []byte(s)
  54. dst := make([]byte, 0, 4096)
  55. b.ResetTimer()
  56. for i := 0; i < b.N; i++ {
  57. p.Append(dst, src)
  58. }
  59. })
  60. }
  61. func BenchmarkTransform(b *testing.B) {
  62. doBench(b, func(b *testing.B, p *Profile, s string) {
  63. src := []byte(s)
  64. dst := make([]byte, 2*len(s))
  65. t := p.NewTransformer()
  66. b.ResetTimer()
  67. for i := 0; i < b.N; i++ {
  68. t.Transform(dst, src, true)
  69. }
  70. })
  71. }