Exemplo n.º 1
0
func doBench(b *testing.B, f func(b *testing.B, p *Profile, s string)) {
	for _, bp := range benchProfiles {
		for _, d := range benchData {
			testtext.Bench(b, bp.name+"/"+d.name, func(b *testing.B) {
				f(b, bp.p, d.str)
			})
		}
	}
}
Exemplo n.º 2
0
func doBench(b *testing.B, t Transformer) {
	for _, bc := range []struct{ name, data string }{
		{"ascii", testtext.ASCII},
		{"3byte", testtext.ThreeByteUTF8},
	} {
		dst := make([]byte, 2*len(bc.data))
		src := []byte(bc.data)

		testtext.Bench(b, bc.name+"/transform", func(b *testing.B) {
			b.SetBytes(int64(len(src)))
			for i := 0; i < b.N; i++ {
				t.Transform(dst, src, true)
			}
		})
		src = t.Bytes(src)
		t.Reset()
		testtext.Bench(b, bc.name+"/span", func(b *testing.B) {
			b.SetBytes(int64(len(src)))
			for i := 0; i < b.N; i++ {
				t.Span(src, true)
			}
		})
	}
}
Exemplo n.º 3
0
func BenchmarkCasers(b *testing.B) {
	for _, s := range []struct{ name, text string }{
		{"ascii", txtASCII},
		{"nonASCII", txtNonASCII},
		{"short", "При"},
	} {
		src := []byte(s.text)
		// Measure case mappings in bytes package for comparison.
		for _, f := range []struct {
			name string
			fn   func(b []byte) []byte
		}{
			{"lower", bytes.ToLower},
			{"title", bytes.ToTitle},
			{"upper", bytes.ToUpper},
		} {
			testtext.Bench(b, path.Join(s.name, "bytes", f.name), func(b *testing.B) {
				b.SetBytes(int64(len(src)))
				for i := 0; i < b.N; i++ {
					f.fn(src)
				}
			})
		}
		for _, t := range []struct {
			name  string
			caser transform.SpanningTransformer
		}{
			{"fold/default", Fold()},
			{"upper/default", Upper(language.Und)},
			{"lower/sigma", Lower(language.Und)},
			{"lower/simple", Lower(language.Und, HandleFinalSigma(false))},
			{"title/sigma", Title(language.Und)},
			{"title/simple", Title(language.Und, HandleFinalSigma(false))},
		} {
			c := Caser{t.caser}
			dst := make([]byte, len(src))
			testtext.Bench(b, path.Join(s.name, t.name, "transform"), func(b *testing.B) {
				b.SetBytes(int64(len(src)))
				for i := 0; i < b.N; i++ {
					c.Reset()
					c.Transform(dst, src, true)
				}
			})
			// No need to check span for simple cases, as they will be the same
			// as sigma.
			if strings.HasSuffix(t.name, "/simple") {
				continue
			}
			spanSrc := c.Bytes(src)
			testtext.Bench(b, path.Join(s.name, t.name, "span"), func(b *testing.B) {
				c.Reset()
				if n, _ := c.Span(spanSrc, true); n < len(spanSrc) {
					b.Fatalf("spanner is not recognizing text %q as done (at %d)", spanSrc, n)
				}
				b.SetBytes(int64(len(spanSrc)))
				for i := 0; i < b.N; i++ {
					c.Reset()
					c.Span(spanSrc, true)
				}
			})
		}
	}
}
Exemplo n.º 4
0
func doBench(b *testing.B, fn func(b *testing.B, data string)) {
	for _, d := range benchData {
		testtext.Bench(b, d.name, func(b *testing.B) { fn(b, d.data) })
	}
}