Adding benchmark for transformation module.
authorNoah Campbell <noahcampbell@gmail.com>
Wed, 30 Oct 2013 03:24:29 +0000 (20:24 -0700)
committerNoah Campbell <noahcampbell@gmail.com>
Fri, 1 Nov 2013 16:59:57 +0000 (09:59 -0700)
baseline.txt [new file with mode: 0644]
transform/chain_test.go
transform/nav_test.go
transform/posttrans_test.go

diff --git a/baseline.txt b/baseline.txt
new file mode 100644 (file)
index 0000000..348ec93
--- /dev/null
@@ -0,0 +1,4 @@
+PASS
+BenchmarkChain    10000            101219 ns/op           23919 B/op        222 allocs/op
+BenchmarkTransform        50000             51625 ns/op           11858 B/op        135 allocs/op
+ok     github.com/spf13/hugo/transform 4.172s
index c7105cdad733a8066330e8c013c7cb0180206cab..b479ac1e326aa4aaa432001278c781354b097600 100644 (file)
@@ -16,7 +16,7 @@ func TestChainZeroTransformers(t *testing.T) {
 
 func TestChainOneTransformer(t *testing.T) {
        tr := NewChain(&AbsURL{BaseURL: "http://base"})
-       apply(t, tr, abs_url_tests)
+       apply(t.Errorf, tr, abs_url_tests)
 }
 
 const H5_JS_CONTENT_ABS_URL_WITH_NAV = "<!DOCTYPE html><html><head><script src=\"/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=\"/foobar\">foobar</a>. Follow up</article></body></html>"
@@ -32,5 +32,16 @@ func TestChainTwoTransformer(t *testing.T) {
                &AbsURL{BaseURL: "http://two"},
                &NavActive{Section: "section_1"},
        )
-       apply(t, tr, two_chain_tests)
+       apply(t.Errorf, tr, two_chain_tests)
+}
+
+func BenchmarkChain(b *testing.B) {
+
+       tr := NewChain(
+               &AbsURL{BaseURL: "http://two"},
+               &NavActive{Section: "section_1"},
+       )
+       for i := 0; i < b.N; i++ {
+               apply(b.Errorf, tr, two_chain_tests)
+       }
 }
index 4d578b87a47cd269bdcd47b1fb230ba0fa4f57c6..8ee46f1169aeab06007861efa6f7a8eda95c2414 100644 (file)
@@ -58,3 +58,13 @@ func TestSetNav(t *testing.T) {
                t.Errorf("NavActive.Apply output expected and got:\n%q\n%q", expected, out.String())
        }
 }
+
+func BenchmarkTransform(b *testing.B) {
+       for i := 0; i < b.N; i++ {
+               tr := &NavActive{Section: "section_2"}
+               out := new(bytes.Buffer)
+               if err := tr.Apply(out, strings.NewReader(HTML_WITH_NAV)); err != nil {
+                       b.Errorf("Unexpected error in Apply() for NavActive: %s", err)
+               }
+       }
+}
index fd6bc18b2748b1b97aae1deee01d23927abe8245..103e73a4ae977c914e1a7a7f26db6fed61e0f320 100644 (file)
@@ -21,7 +21,7 @@ func TestAbsUrlify(t *testing.T) {
                BaseURL: "http://base",
        }
 
-       apply(t, tr, abs_url_tests)
+       apply(t.Errorf, tr, abs_url_tests)
 }
 
 type test struct {
@@ -35,15 +35,17 @@ var abs_url_tests = []test{
        {H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
 }
 
-func apply(t *testing.T, tr Transformer, tests []test) {
+type errorf func (string, ...interface{})
+
+func apply(ef errorf, tr Transformer, tests []test) {
        for _, test := range tests {
                out := new(bytes.Buffer)
                err := tr.Apply(out, strings.NewReader(test.content))
                if err != nil {
-                       t.Errorf("Unexpected error: %s", err)
+                       ef("Unexpected error: %s", err)
                }
                if test.expected != string(out.Bytes()) {
-                       t.Errorf("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
+                       ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
                }
        }
 }