Skip to content

Commit ad074e2

Browse files
committed
regexp: use Run for benchmark
Change-Id: I4d19e3221d3789d4c460b421b2d1484253778068 Reviewed-on: https://go-review.googlesource.com/23429 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Marcel van Lohuizen <[email protected]>
1 parent 88ae649 commit ad074e2

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

src/regexp/exec_test.go

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -658,57 +658,42 @@ func makeText(n int) []byte {
658658
return text
659659
}
660660

661-
func benchmark(b *testing.B, re string, n int) {
662-
r := MustCompile(re)
663-
t := makeText(n)
664-
b.ResetTimer()
665-
b.SetBytes(int64(n))
666-
for i := 0; i < b.N; i++ {
667-
if r.Match(t) {
668-
b.Fatal("match!")
661+
func BenchmarkMatch(b *testing.B) {
662+
for _, data := range benchData {
663+
r := MustCompile(data.re)
664+
for _, size := range benchSizes {
665+
t := makeText(size.n)
666+
b.Run(data.name+"/"+size.name, func(b *testing.B) {
667+
b.SetBytes(int64(size.n))
668+
for i := 0; i < b.N; i++ {
669+
if r.Match(t) {
670+
b.Fatal("match!")
671+
}
672+
}
673+
})
669674
}
670675
}
671676
}
672677

673-
const (
674-
easy0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
675-
easy0i = "(?i)ABCDEFGHIJklmnopqrstuvwxyz$"
676-
easy1 = "A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$"
677-
medium = "[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
678-
hard = "[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
679-
hard1 = "ABCD|CDEF|EFGH|GHIJ|IJKL|KLMN|MNOP|OPQR|QRST|STUV|UVWX|WXYZ"
680-
)
678+
var benchData = []struct{ name, re string }{
679+
{"Easy0", "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
680+
{"Easy0i", "(?i)ABCDEFGHIJklmnopqrstuvwxyz$"},
681+
{"Easy1", "A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$"},
682+
{"Medium", "[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
683+
{"Hard", "[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
684+
{"Hard1", "ABCD|CDEF|EFGH|GHIJ|IJKL|KLMN|MNOP|OPQR|QRST|STUV|UVWX|WXYZ"},
685+
}
681686

682-
func BenchmarkMatchEasy0_32(b *testing.B) { benchmark(b, easy0, 32<<0) }
683-
func BenchmarkMatchEasy0_1K(b *testing.B) { benchmark(b, easy0, 1<<10) }
684-
func BenchmarkMatchEasy0_32K(b *testing.B) { benchmark(b, easy0, 32<<10) }
685-
func BenchmarkMatchEasy0_1M(b *testing.B) { benchmark(b, easy0, 1<<20) }
686-
func BenchmarkMatchEasy0_32M(b *testing.B) { benchmark(b, easy0, 32<<20) }
687-
func BenchmarkMatchEasy0i_32(b *testing.B) { benchmark(b, easy0i, 32<<0) }
688-
func BenchmarkMatchEasy0i_1K(b *testing.B) { benchmark(b, easy0i, 1<<10) }
689-
func BenchmarkMatchEasy0i_32K(b *testing.B) { benchmark(b, easy0i, 32<<10) }
690-
func BenchmarkMatchEasy0i_1M(b *testing.B) { benchmark(b, easy0i, 1<<20) }
691-
func BenchmarkMatchEasy0i_32M(b *testing.B) { benchmark(b, easy0i, 32<<20) }
692-
func BenchmarkMatchEasy1_32(b *testing.B) { benchmark(b, easy1, 32<<0) }
693-
func BenchmarkMatchEasy1_1K(b *testing.B) { benchmark(b, easy1, 1<<10) }
694-
func BenchmarkMatchEasy1_32K(b *testing.B) { benchmark(b, easy1, 32<<10) }
695-
func BenchmarkMatchEasy1_1M(b *testing.B) { benchmark(b, easy1, 1<<20) }
696-
func BenchmarkMatchEasy1_32M(b *testing.B) { benchmark(b, easy1, 32<<20) }
697-
func BenchmarkMatchMedium_32(b *testing.B) { benchmark(b, medium, 32<<0) }
698-
func BenchmarkMatchMedium_1K(b *testing.B) { benchmark(b, medium, 1<<10) }
699-
func BenchmarkMatchMedium_32K(b *testing.B) { benchmark(b, medium, 32<<10) }
700-
func BenchmarkMatchMedium_1M(b *testing.B) { benchmark(b, medium, 1<<20) }
701-
func BenchmarkMatchMedium_32M(b *testing.B) { benchmark(b, medium, 32<<20) }
702-
func BenchmarkMatchHard_32(b *testing.B) { benchmark(b, hard, 32<<0) }
703-
func BenchmarkMatchHard_1K(b *testing.B) { benchmark(b, hard, 1<<10) }
704-
func BenchmarkMatchHard_32K(b *testing.B) { benchmark(b, hard, 32<<10) }
705-
func BenchmarkMatchHard_1M(b *testing.B) { benchmark(b, hard, 1<<20) }
706-
func BenchmarkMatchHard_32M(b *testing.B) { benchmark(b, hard, 32<<20) }
707-
func BenchmarkMatchHard1_32(b *testing.B) { benchmark(b, hard1, 32<<0) }
708-
func BenchmarkMatchHard1_1K(b *testing.B) { benchmark(b, hard1, 1<<10) }
709-
func BenchmarkMatchHard1_32K(b *testing.B) { benchmark(b, hard1, 32<<10) }
710-
func BenchmarkMatchHard1_1M(b *testing.B) { benchmark(b, hard1, 1<<20) }
711-
func BenchmarkMatchHard1_32M(b *testing.B) { benchmark(b, hard1, 32<<20) }
687+
var benchSizes = []struct {
688+
name string
689+
n int
690+
}{
691+
{"32", 32},
692+
{"1K", 1 << 10},
693+
{"32K", 32 << 10},
694+
{"1M", 1 << 20},
695+
{"32M", 32 << 20},
696+
}
712697

713698
func TestLongest(t *testing.T) {
714699
re, err := Compile(`a(|b)`)

0 commit comments

Comments
 (0)