Skip to content

Commit 5cf0aa2

Browse files
committed
Add benchmark for sort and reverse
1 parent aca0f60 commit 5cf0aa2

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

bench.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,29 @@
77
# - Do the same for master
88
# - then compare the two runs with benchcmp
99

10-
if [ $# -ne 2 ]
10+
benchFilter=".*"
11+
12+
if (( $# < 2 ));
1113
then
12-
echo "USAGE: ./bench.sh <git-branch> <package-to-bench>"
14+
echo "USAGE: ./bench.sh <git-branch> <package-to-bench> (and <benchmark filter> (regexp, optional))"
1315
exit 1
1416
fi
1517

1618

19+
20+
if [ $# -eq 3 ]; then
21+
benchFilter=$3
22+
fi
23+
24+
1725
BRANCH=$1
1826
PACKAGE=$2
1927

2028
git checkout $BRANCH
21-
go test -test.run=NONE -bench=".*" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-$BRANCH.txt
29+
go test -test.run=NONE -bench="$benchFilter" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-$BRANCH.txt
2230

2331
git checkout master
24-
go test -test.run=NONE -bench=".*" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-master.txt
32+
go test -test.run=NONE -bench="$benchFilter" -test.benchmem=true ./$PACKAGE > /tmp/bench-$PACKAGE-master.txt
2533

2634

2735
benchcmp /tmp/bench-$PACKAGE-master.txt /tmp/bench-$PACKAGE-$BRANCH.txt

hugolib/pageSort_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package hugolib
2+
3+
import (
4+
"fmt"
5+
"github.com/stretchr/testify/assert"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/spf13/hugo/source"
10+
)
11+
12+
func TestPageSortReverse(t *testing.T) {
13+
p := createSortTestPages(10)
14+
assert.Equal(t, 0, p[0].FuzzyWordCount)
15+
assert.Equal(t, 9, p[9].FuzzyWordCount)
16+
p = p.Reverse()
17+
assert.Equal(t, 9, p[0].FuzzyWordCount)
18+
assert.Equal(t, 1, p[9].FuzzyWordCount)
19+
}
20+
21+
func BenchmarkSortByWeightAndReverse(b *testing.B) {
22+
23+
p := createSortTestPages(300)
24+
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
p = p.ByWeight().Reverse()
28+
}
29+
30+
}
31+
32+
func createSortTestPages(num int) Pages {
33+
pages := make(Pages, num)
34+
35+
for i := 0; i < num; i++ {
36+
pages[i] = &Page{
37+
Node: Node{
38+
URLPath: URLPath{
39+
Section: "z",
40+
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
41+
},
42+
Site: &SiteInfo{
43+
BaseURL: "http://base/",
44+
},
45+
},
46+
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
47+
}
48+
w := 5
49+
if i%2 == 0 {
50+
w = 10
51+
}
52+
pages[i].FuzzyWordCount = i
53+
pages[i].Weight = w
54+
}
55+
56+
return pages
57+
}

0 commit comments

Comments
 (0)