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