aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorM. J. Fromberger <michael.j.fromberger@gmail.com>2019-05-24 14:23:41 -0700
committerM. J. Fromberger <michael.j.fromberger@gmail.com>2019-05-24 14:23:41 -0700
commitbe8f082bd5c59daac34010c687cfb430ad4dbb1c (patch)
treebf8265fb4611d40205ac23280a6f8fda24779926
parent1c383f975d4f1a56e8637f26dca14a4313bf239c (diff)
downloadgo-creachadair-shell-be8f082bd5c59daac34010c687cfb430ad4dbb1c.tar.gz
Add a rough benchmark for shell.Split.
-rw-r--r--bench_test.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/bench_test.go b/bench_test.go
new file mode 100644
index 0000000..23c86a8
--- /dev/null
+++ b/bench_test.go
@@ -0,0 +1,74 @@
+package shell_test
+
+import (
+ "bytes"
+ "fmt"
+ "math"
+ "math/rand"
+ "testing"
+
+ "bitbucket.org/creachadair/shell"
+)
+
+var input string
+
+// Generate a long random string with balanced quotations for perf testing.
+func init() {
+ var buf bytes.Buffer
+
+ src := rand.NewSource(12345)
+ r := rand.New(src)
+
+ const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 \t\n\n\n"
+ pick := func(f float64) byte {
+ pos := math.Ceil(f*float64(len(alphabet))) - 1
+ return alphabet[int(pos)]
+ }
+
+ const inputLen = 100000
+ var quote struct {
+ q byte
+ n int
+ }
+ for i := 0; i < inputLen; i++ {
+ if quote.n == 0 {
+ q := r.Float64()
+ if q < .1 {
+ quote.q = '"'
+ quote.n = r.Intn(256)
+ buf.WriteByte('"')
+ continue
+ } else if q < .15 {
+ quote.q = '\''
+ quote.n = r.Intn(256)
+ buf.WriteByte('\'')
+ continue
+ }
+ }
+ buf.WriteByte(pick(r.Float64()))
+ if quote.n > 0 {
+ quote.n--
+ if quote.n == 0 {
+ buf.WriteByte(quote.q)
+ }
+ }
+ }
+ input = buf.String()
+}
+
+func BenchmarkSplit(b *testing.B) {
+ var lens []int
+ for i := 1; i < len(input); i *= 4 {
+ lens = append(lens, i)
+ }
+ lens = append(lens, len(input))
+
+ b.ResetTimer()
+ for _, n := range lens {
+ b.Run(fmt.Sprintf("len_%d", n), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ shell.Split(input[:n])
+ }
+ })
+ }
+}