mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-01 00:54:49 +00:00
Get rid of allocation in the BenchmarkReplaceShortcodeTokens itself
So we can see the real numbers. See #1516
This commit is contained in:
parent
d7588fac21
commit
53a5932fa4
|
@ -349,34 +349,47 @@ func collectAndSortShortcodes(shortcodes map[string]shortcode) []string {
|
||||||
|
|
||||||
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
|
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
|
||||||
|
|
||||||
|
type input struct {
|
||||||
|
in []byte
|
||||||
|
replacements map[string]string
|
||||||
|
expect []byte
|
||||||
|
}
|
||||||
|
|
||||||
data := []struct {
|
data := []struct {
|
||||||
input string
|
input string
|
||||||
replacements map[string]string
|
replacements map[string]string
|
||||||
expect interface{}
|
expect []byte
|
||||||
}{
|
}{
|
||||||
{"Hello {@{@HUGOSHORTCODE-1@}@}.", map[string]string{"{@{@HUGOSHORTCODE-1@}@}": "World"}, "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"}, strings.Repeat("A", 100) + " 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"}, strings.Repeat("A", 500) + " 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"}, strings.Repeat("ABCD ", 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"}, strings.Repeat("A", 500) + " Hello World." + strings.Repeat("BC", 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.")},
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for i, this := range data {
|
|
||||||
results, err := replaceShortcodeTokens([]byte(this.input), "HUGOSHORTCODE", this.replacements)
|
|
||||||
|
|
||||||
if expectSuccess, ok := this.expect.(bool); ok && !expectSuccess {
|
var in []input = make([]input, b.N*len(data))
|
||||||
if err == nil {
|
var cnt = 0
|
||||||
b.Fatalf("[%d] replaceShortcodeTokens didn't return an expected error", i)
|
for i := 0; i < b.N; i++ {
|
||||||
}
|
for _, this := range data {
|
||||||
} else {
|
in[cnt] = input{[]byte(this.input), this.replacements, this.expect}
|
||||||
if err != nil {
|
cnt++
|
||||||
b.Fatalf("[%d] failed: %s", i, err)
|
}
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(results, []byte(this.expect.(string))) {
|
b.ResetTimer()
|
||||||
b.Fatalf("[%d] replaceShortcodeTokens, got \n%q but expected \n%q", i, results, this.expect)
|
cnt = 0
|
||||||
}
|
for i := 0; i < b.N; i++ {
|
||||||
|
for j := range data {
|
||||||
|
currIn := in[cnt]
|
||||||
|
cnt++
|
||||||
|
results, err := replaceShortcodeTokens(currIn.in, "HUGOSHORTCODE", currIn.replacements)
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue