aboutsummaryrefslogtreecommitdiffhomepage
path: root/hugolib/pages_test.go
blob: 30e9e59d2d3d2da633045be80f471d28a0a62952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package hugolib

import (
	"fmt"
	"math/rand"
	"testing"

	"github.com/gohugoio/hugo/resources/page"

	qt "github.com/frankban/quicktest"
)

func newPagesPrevNextTestSite(t testing.TB, numPages int) *sitesBuilder {
	categories := []string{"blue", "green", "red", "orange", "indigo", "amber", "lime"}
	cat1, cat2 := categories[rand.Intn(len(categories))], categories[rand.Intn(len(categories))]
	categoriesSlice := fmt.Sprintf("[%q,%q]", cat1, cat2)
	pageTemplate := `
---
title: "Page %d"
weight: %d
categories: %s
---

`
	b := newTestSitesBuilder(t)

	for i := 1; i <= numPages; i++ {
		b.WithContent(fmt.Sprintf("page%d.md", i), fmt.Sprintf(pageTemplate, i, rand.Intn(numPages), categoriesSlice))
	}

	return b
}

func TestPagesPrevNext(t *testing.T) {
	b := newPagesPrevNextTestSite(t, 100)
	b.Build(BuildCfg{SkipRender: true})

	pages := b.H.Sites[0].RegularPages()

	b.Assert(pages, qt.HasLen, 100)

	for _, p := range pages {
		msg := qt.Commentf("w=%d", p.Weight())
		b.Assert(pages.Next(p), qt.Equals, p.Next(), msg)
		b.Assert(pages.Prev(p), qt.Equals, p.Prev(), msg)
	}
}

func BenchmarkPagesPrevNext(b *testing.B) {
	type Variant struct {
		name         string
		preparePages func(pages page.Pages) page.Pages
		run          func(p page.Page, pages page.Pages)
	}

	shufflePages := func(pages page.Pages) page.Pages {
		rand.Shuffle(len(pages), func(i, j int) { pages[i], pages[j] = pages[j], pages[i] })
		return pages
	}

	for _, variant := range []Variant{
		{".Next", nil, func(p page.Page, pages page.Pages) { p.Next() }},
		{".Prev", nil, func(p page.Page, pages page.Pages) { p.Prev() }},
		{"Pages.Next", nil, func(p page.Page, pages page.Pages) { pages.Next(p) }},
		{"Pages.Prev", nil, func(p page.Page, pages page.Pages) { pages.Prev(p) }},
		{"Pages.Shuffled.Next", shufflePages, func(p page.Page, pages page.Pages) { pages.Next(p) }},
		{"Pages.Shuffled.Prev", shufflePages, func(p page.Page, pages page.Pages) { pages.Prev(p) }},
		{"Pages.ByTitle.Next", func(pages page.Pages) page.Pages { return pages.ByTitle() }, func(p page.Page, pages page.Pages) { pages.Next(p) }},
	} {
		for _, numPages := range []int{300, 5000} {
			b.Run(fmt.Sprintf("%s-pages-%d", variant.name, numPages), func(b *testing.B) {
				b.StopTimer()
				builder := newPagesPrevNextTestSite(b, numPages)
				builder.Build(BuildCfg{SkipRender: true})
				pages := builder.H.Sites[0].RegularPages()
				if variant.preparePages != nil {
					pages = variant.preparePages(pages)
				}
				b.StartTimer()
				for i := 0; i < b.N; i++ {
					p := pages[rand.Intn(len(pages))]
					variant.run(p, pages)
				}
			})
		}
	}
}

func BenchmarkPagePageCollections(b *testing.B) {
	type Variant struct {
		name string
		run  func(p page.Page)
	}

	for _, variant := range []Variant{
		{".Pages", func(p page.Page) { p.Pages() }},
		{".RegularPages", func(p page.Page) { p.RegularPages() }},
		{".RegularPagesRecursive", func(p page.Page) { p.RegularPagesRecursive() }},
	} {
		for _, numPages := range []int{300, 5000} {
			b.Run(fmt.Sprintf("%s-%d", variant.name, numPages), func(b *testing.B) {
				b.StopTimer()
				builder := newPagesPrevNextTestSite(b, numPages)
				builder.Build(BuildCfg{SkipRender: true})
				var pages page.Pages
				for _, p := range builder.H.Sites[0].Pages() {
					if !p.IsPage() {
						pages = append(pages, p)
					}
				}
				b.StartTimer()
				for i := 0; i < b.N; i++ {
					p := pages[rand.Intn(len(pages))]
					variant.run(p)
				}
			})
		}
	}
}