| 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 |
|
| 5 |
package precis |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
var benchData = []struct{ name, str string }{ |
| 12 |
{"ASCII", "Malvolio"}, |
| 13 |
{"NotNormalized", "abcdefg\u0301\u031f"}, |
| 14 |
{"Arabic", "دبي"}, |
| 15 |
{"Hangul", "동일조건변경허락"}, |
| 16 |
} |
| 17 |
|
| 18 |
var benchProfiles = []struct { |
| 19 |
name string |
| 20 |
p *Profile |
| 21 |
}{ |
| 22 |
{"FreeForm", NewFreeform()}, |
| 23 |
{"Nickname", Nickname}, |
| 24 |
{"OpaqueString", OpaqueString}, |
| 25 |
{"UsernameCaseMapped", UsernameCaseMapped}, |
| 26 |
{"UsernameCasePreserved", UsernameCasePreserved}, |
| 27 |
} |
| 28 |
|
| 29 |
func doBench(b *testing.B, f func(b *testing.B, p *Profile, s string)) { |
| 30 |
for _, bp := range benchProfiles { |
| 31 |
for _, d := range benchData { |
| 32 |
b.Run(bp.name+"/"+d.name, func(b *testing.B) { |
| 33 |
f(b, bp.p, d.str) |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func BenchmarkString(b *testing.B) { |
| 40 |
doBench(b, func(b *testing.B, p *Profile, s string) { |
| 41 |
for i := 0; i < b.N; i++ { |
| 42 |
p.String(s) |
| 43 |
} |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
func BenchmarkBytes(b *testing.B) { |
| 48 |
doBench(b, func(b *testing.B, p *Profile, s string) { |
| 49 |
src := []byte(s) |
| 50 |
b.ResetTimer() |
| 51 |
for i := 0; i < b.N; i++ { |
| 52 |
p.Bytes(src) |
| 53 |
} |
| 54 |
}) |
| 55 |
} |
| 56 |
|
| 57 |
func BenchmarkAppend(b *testing.B) { |
| 58 |
doBench(b, func(b *testing.B, p *Profile, s string) { |
| 59 |
src := []byte(s) |
| 60 |
dst := make([]byte, 0, 4096) |
| 61 |
b.ResetTimer() |
| 62 |
for i := 0; i < b.N; i++ { |
| 63 |
p.Append(dst, src) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
|
| 68 |
func BenchmarkTransform(b *testing.B) { |
| 69 |
doBench(b, func(b *testing.B, p *Profile, s string) { |
| 70 |
src := []byte(s) |
| 71 |
dst := make([]byte, 2*len(s)) |
| 72 |
t := p.NewTransformer() |
| 73 |
b.ResetTimer() |
| 74 |
for i := 0; i < b.N; i++ { |
| 75 |
t.Transform(dst, src, true) |
| 76 |
} |
| 77 |
}) |
| 78 |
} |