...

Source file src/golang.org/x/text/secure/precis/benchmark_test.go

Documentation: golang.org/x/text/secure/precis

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

View as plain text