hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 5cf0aa2fc0cebbb51227ba0b23e0906088bf3027
parent aca0f6044878bdcdcfec63497759e3b8fce49512
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Tue, 21 Jul 2015 21:01:56 +0200

Add benchmark for sort and reverse

Diffstat:
Mbench.sh | 16++++++++++++----
Ahugolib/pageSort_test.go | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 69 insertions(+), 4 deletions(-)
diff --git a/bench.sh b/bench.sh
@@ -7,21 +7,29 @@
 # - Do the same for master
 # - then compare the two runs with benchcmp
 
-if [ $# -ne 2 ]
+benchFilter=".*"
+
+if (( $# < 2 ));
   then
-    echo "USAGE: ./bench.sh <git-branch> <package-to-bench>"
+    echo "USAGE: ./bench.sh <git-branch> <package-to-bench> (and <benchmark filter> (regexp, optional))"
     exit 1
 fi
 
 
+
+if [ $# -eq 3 ]; then
+  benchFilter=$3
+fi
+
+
 BRANCH=$1
 PACKAGE=$2
 
 git checkout $BRANCH
-go test -test.run=NONE -bench=".*" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-$BRANCH.txt
+go test -test.run=NONE -bench="$benchFilter" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-$BRANCH.txt
 
 git checkout master
-go test -test.run=NONE -bench=".*" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-master.txt
+go test -test.run=NONE -bench="$benchFilter" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-master.txt
 
 
 benchcmp /tmp/bench-$PACKAGE-master.txt /tmp/bench-$PACKAGE-$BRANCH.txt 
\ No newline at end of file
diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go
@@ -0,0 +1,57 @@
+package hugolib
+
+import (
+	"fmt"
+	"github.com/stretchr/testify/assert"
+	"path/filepath"
+	"testing"
+
+	"github.com/spf13/hugo/source"
+)
+
+func TestPageSortReverse(t *testing.T) {
+	p := createSortTestPages(10)
+	assert.Equal(t, 0, p[0].FuzzyWordCount)
+	assert.Equal(t, 9, p[9].FuzzyWordCount)
+	p = p.Reverse()
+	assert.Equal(t, 9, p[0].FuzzyWordCount)
+	assert.Equal(t, 1, p[9].FuzzyWordCount)
+}
+
+func BenchmarkSortByWeightAndReverse(b *testing.B) {
+
+	p := createSortTestPages(300)
+
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		p = p.ByWeight().Reverse()
+	}
+
+}
+
+func createSortTestPages(num int) Pages {
+	pages := make(Pages, num)
+
+	for i := 0; i < num; i++ {
+		pages[i] = &Page{
+			Node: Node{
+				URLPath: URLPath{
+					Section: "z",
+					URL:     fmt.Sprintf("http://base/x/y/p%d.html", i),
+				},
+				Site: &SiteInfo{
+					BaseURL: "http://base/",
+				},
+			},
+			Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
+		}
+		w := 5
+		if i%2 == 0 {
+			w = 10
+		}
+		pages[i].FuzzyWordCount = i
+		pages[i].Weight = w
+	}
+
+	return pages
+}