shithub: hugo

Download patch

ref: 43e48a8989b04f9a434ffa6ff21414adcbb72544
parent: 48b6777ea212073d32c3fb59ea3754d5cf8348de
author: bep <[email protected]>
date: Fri Feb 6 05:00:42 EST 2015

Add benchmark test for StripHTML

go test -test.run=NONE -bench=".*" -test.benchmem=true ./helpers

Old vs new impl (string.Replace vs string.Replacer):

benchmark              old ns/op     new ns/op     delta
BenchmarkStripHTML     10210         6572          -35.63%

benchmark              old allocs     new allocs     delta
BenchmarkStripHTML     6              5              -16.67%

benchmark              old bytes     new bytes     delta
BenchmarkStripHTML     1456          848           -41.76%

--- a/helpers/content_test.go
+++ b/helpers/content_test.go
@@ -7,6 +7,8 @@
 	"testing"
 )
 
+const tstHtmlContent = "<!DOCTYPE html><html><head><script src=\"http://two/foobar.js\"></script></head><body><nav><ul><li hugo-nav=\"section_0\"></li><li hugo-nav=\"section_1\"></li></ul></nav><article>content <a href=\"http://two/foobar\">foobar</a>. Follow up</article><p>This is some text.<br>And some more.</p></body></html>"
+
 func TestStripHTML(t *testing.T) {
 	type test struct {
 		input, expected string
@@ -23,6 +25,13 @@
 		if d.expected != output {
 			t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
 		}
+	}
+}
+
+func BenchmarkStripHTML(b *testing.B) {
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		StripHTML(tstHtmlContent)
 	}
 }