summaryrefslogtreecommitdiff
path: root/test/bench/go1/regexp_test.go
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2013-03-23 11:28:53 +0100
committerMichael Stapelberg <michael@stapelberg.de>2013-03-23 11:28:53 +0100
commitb39e15dde5ec7b96c15da9faf4ab5892501c1aae (patch)
tree718cede1f6ca97d082c6c40b7dc3f4f6148253c0 /test/bench/go1/regexp_test.go
parent04b08da9af0c450d645ab7389d1467308cfc2db8 (diff)
downloadgolang-upstream/1.1_hg20130323.tar.gz
Imported Upstream version 1.1~hg20130323upstream/1.1_hg20130323
Diffstat (limited to 'test/bench/go1/regexp_test.go')
-rw-r--r--test/bench/go1/regexp_test.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/bench/go1/regexp_test.go b/test/bench/go1/regexp_test.go
new file mode 100644
index 000000000..3ce9f3a2c
--- /dev/null
+++ b/test/bench/go1/regexp_test.go
@@ -0,0 +1,59 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package go1
+
+import (
+ "math/rand"
+ "regexp"
+ "testing"
+)
+
+// benchmark based on regexp/exec_test.go
+
+var regexpText []byte
+
+func makeRegexpText(n int) []byte {
+ rand.Seed(0) // For reproducibility.
+ if len(regexpText) >= n {
+ return regexpText[:n]
+ }
+ regexpText = make([]byte, n)
+ for i := range regexpText {
+ if rand.Intn(30) == 0 {
+ regexpText[i] = '\n'
+ } else {
+ regexpText[i] = byte(rand.Intn(0x7E+1-0x20) + 0x20)
+ }
+ }
+ return regexpText
+}
+
+func benchmark(b *testing.B, re string, n int) {
+ r := regexp.MustCompile(re)
+ t := makeRegexpText(n)
+ b.ResetTimer()
+ b.SetBytes(int64(n))
+ for i := 0; i < b.N; i++ {
+ if r.Match(t) {
+ b.Fatal("match!")
+ }
+ }
+}
+
+const (
+ easy0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
+ easy1 = "A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$"
+ medium = "[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
+ hard = "[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$"
+)
+
+func BenchmarkRegexpMatchEasy0_32(b *testing.B) { benchmark(b, easy0, 32<<0) }
+func BenchmarkRegexpMatchEasy0_1K(b *testing.B) { benchmark(b, easy0, 1<<10) }
+func BenchmarkRegexpMatchEasy1_32(b *testing.B) { benchmark(b, easy1, 32<<0) }
+func BenchmarkRegexpMatchEasy1_1K(b *testing.B) { benchmark(b, easy1, 1<<10) }
+func BenchmarkRegexpMatchMedium_32(b *testing.B) { benchmark(b, medium, 1<<0) }
+func BenchmarkRegexpMatchMedium_1K(b *testing.B) { benchmark(b, medium, 1<<10) }
+func BenchmarkRegexpMatchHard_32(b *testing.B) { benchmark(b, hard, 32<<0) }
+func BenchmarkRegexpMatchHard_1K(b *testing.B) { benchmark(b, hard, 1<<10) }