-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update Go benchmark to include Go templates
- Loading branch information
Showing
3 changed files
with
101 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ package testhtml | |
|
||
import ( | ||
"context" | ||
"html/template" | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func BenchmarkCurrent(b *testing.B) { | ||
func BenchmarkTempl(b *testing.B) { | ||
b.ReportAllocs() | ||
t := Render(Person{ | ||
Name: "Luiz Bonfa", | ||
|
@@ -24,15 +25,44 @@ func BenchmarkCurrent(b *testing.B) { | |
} | ||
} | ||
|
||
var html = `<div><h1>Luiz Bonfa</h1><div style="font-family: 'sans-serif'" id="test" data-contents="something with "quotes" and a <tag>"><div>email:<a href="mailto: [email protected]">[email protected]</a></div></div></div><hr noshade><hr optionA optionB optionC="other"><hr noshade>` | ||
var goTemplate = template.Must(template.New("example").Parse(`<div> | ||
<h1>{{.Name}}</h1> | ||
<div style="font-family: 'sans-serif'" id="test" data-contents="something with "quotes" and a <tag>"> | ||
<div> | ||
email:<a href="mailto: {{.Email}}">{{.Email}}</a></div> | ||
</div> | ||
</div> | ||
<hr noshade> | ||
<hr optionA optionB optionC="other"> | ||
<hr noshade> | ||
`)) | ||
|
||
func BenchmarkGoTemplate(b *testing.B) { | ||
w := new(strings.Builder) | ||
person := Person{ | ||
Name: "Luiz Bonfa", | ||
Email: "[email protected]", | ||
} | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
err := goTemplate.Execute(w, person) | ||
if err != nil { | ||
b.Errorf("failed to render: %v", err) | ||
} | ||
w.Reset() | ||
} | ||
} | ||
|
||
const html = `<div><h1>Luiz Bonfa</h1><div style="font-family: 'sans-serif'" id="test" data-contents="something with "quotes" and a <tag>"><div>email:<a href="mailto: [email protected]">[email protected]</a></div></div></div><hr noshade><hr optionA optionB optionC="other"><hr noshade>` | ||
|
||
func BenchmarkIOWriteString(b *testing.B) { | ||
b.ReportAllocs() | ||
w := new(strings.Builder) | ||
for i := 0; i < b.N; i++ { | ||
w := new(strings.Builder) | ||
_, err := io.WriteString(w, html) | ||
if err != nil { | ||
b.Errorf("failed to render: %v", err) | ||
} | ||
w.Reset() | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.