summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2015-11-13 21:21:03 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2015-11-13 22:37:07 +0100
commit53a5932fa44e915dfa331876de08cd965c54e943 (patch)
treef9cafc204ced437feb923c26e861928e4b626581 /hugolib
parentd7588fac21e9bf3a7d1b8d140dacbe93c38fd1d2 (diff)
Get rid of allocation in the BenchmarkReplaceShortcodeTokens itself
So we can see the real numbers. See #1516
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/shortcode_test.go53
1 files changed, 33 insertions, 20 deletions
diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go
index a8eeca8e7..c3917a00d 100644
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -349,34 +349,47 @@ func collectAndSortShortcodes(shortcodes map[string]shortcode) []string {
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
+ type input struct {
+ in []byte
+ replacements map[string]string
+ expect []byte
+ }
+
data := []struct {
input string
replacements map[string]string
- expect interface{}
+ expect []byte
}{
- {"Hello {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "World"}, "Hello World."},
- {strings.Repeat("A", 100) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, strings.Repeat("A", 100) + " Hello World."},
- {strings.Repeat("A", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, strings.Repeat("A", 500) + " Hello World."},
- {strings.Repeat("ABCD ", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, strings.Repeat("ABCD ", 500) + " Hello World."},
- {strings.Repeat("A", 500) + " {@{@HUGOSHORTCODE-1@}@}." + strings.Repeat("BC", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, strings.Repeat("A", 500) + " Hello World." + strings.Repeat("BC", 500) + " Hello World."},
+ {"Hello {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "World"}, []byte("Hello World.")},
+ {strings.Repeat("A", 100) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, []byte(strings.Repeat("A", 100) + " Hello World.")},
+ {strings.Repeat("A", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, []byte(strings.Repeat("A", 500) + " Hello World.")},
+ {strings.Repeat("ABCD ", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, []byte(strings.Repeat("ABCD ", 500) + " Hello World.")},
+ {strings.Repeat("A", 500) + " {@{@HUGOSHORTCODE-1@}@}." + strings.Repeat("BC", 500) + " {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "Hello World"}, []byte(strings.Repeat("A", 500) + " Hello World." + strings.Repeat("BC", 500) + " Hello World.")},
+ }
+
+ var in []input = make([]input, b.N*len(data))
+ var cnt = 0
+ for i := 0; i < b.N; i++ {
+ for _, this := range data {
+ in[cnt] = input{[]byte(this.input), this.replacements, this.expect}
+ cnt++
+ }
}
+
b.ResetTimer()
+ cnt = 0
for i := 0; i < b.N; i++ {
- for i, this := range data {
- results, err := replaceShortcodeTokens([]byte(this.input), "HUGOSHORTCODE", this.replacements)
+ for j := range data {
+ currIn := in[cnt]
+ cnt++
+ results, err := replaceShortcodeTokens(currIn.in, "HUGOSHORTCODE", currIn.replacements)
- if expectSuccess, ok := this.expect.(bool); ok && !expectSuccess {
- if err == nil {
- b.Fatalf("[%d] replaceShortcodeTokens didn't return an expected error", i)
- }
- } else {
- if err != nil {
- b.Fatalf("[%d] failed: %s", i, err)
- continue
- }
- if !reflect.DeepEqual(results, []byte(this.expect.(string))) {
- b.Fatalf("[%d] replaceShortcodeTokens, got \n%q but expected \n%q", i, results, this.expect)
- }
+ if err != nil {
+ b.Fatalf("[%d] failed: %s", i, err)
+ continue
+ }
+ if len(results) != len(currIn.expect) {
+ b.Fatalf("[%d] replaceShortcodeTokens, got \n%q but expected \n%q", j, results, currIn.expect)
}
}