hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

pages_test.go (3472B)

    1 package hugolib
    2 
    3 import (
    4 	"fmt"
    5 	"math/rand"
    6 	"testing"
    7 
    8 	"github.com/gohugoio/hugo/resources/page"
    9 
   10 	qt "github.com/frankban/quicktest"
   11 )
   12 
   13 func newPagesPrevNextTestSite(t testing.TB, numPages int) *sitesBuilder {
   14 	categories := []string{"blue", "green", "red", "orange", "indigo", "amber", "lime"}
   15 	cat1, cat2 := categories[rand.Intn(len(categories))], categories[rand.Intn(len(categories))]
   16 	categoriesSlice := fmt.Sprintf("[%q,%q]", cat1, cat2)
   17 	pageTemplate := `
   18 ---
   19 title: "Page %d"
   20 weight: %d
   21 categories: %s
   22 ---
   23 
   24 `
   25 	b := newTestSitesBuilder(t)
   26 
   27 	for i := 1; i <= numPages; i++ {
   28 		b.WithContent(fmt.Sprintf("page%d.md", i), fmt.Sprintf(pageTemplate, i, rand.Intn(numPages), categoriesSlice))
   29 	}
   30 
   31 	return b
   32 }
   33 
   34 func TestPagesPrevNext(t *testing.T) {
   35 	b := newPagesPrevNextTestSite(t, 100)
   36 	b.Build(BuildCfg{SkipRender: true})
   37 
   38 	pages := b.H.Sites[0].RegularPages()
   39 
   40 	b.Assert(pages, qt.HasLen, 100)
   41 
   42 	for _, p := range pages {
   43 		msg := qt.Commentf("w=%d", p.Weight())
   44 		b.Assert(pages.Next(p), qt.Equals, p.Next(), msg)
   45 		b.Assert(pages.Prev(p), qt.Equals, p.Prev(), msg)
   46 	}
   47 }
   48 
   49 func BenchmarkPagesPrevNext(b *testing.B) {
   50 	type Variant struct {
   51 		name         string
   52 		preparePages func(pages page.Pages) page.Pages
   53 		run          func(p page.Page, pages page.Pages)
   54 	}
   55 
   56 	shufflePages := func(pages page.Pages) page.Pages {
   57 		rand.Shuffle(len(pages), func(i, j int) { pages[i], pages[j] = pages[j], pages[i] })
   58 		return pages
   59 	}
   60 
   61 	for _, variant := range []Variant{
   62 		{".Next", nil, func(p page.Page, pages page.Pages) { p.Next() }},
   63 		{".Prev", nil, func(p page.Page, pages page.Pages) { p.Prev() }},
   64 		{"Pages.Next", nil, func(p page.Page, pages page.Pages) { pages.Next(p) }},
   65 		{"Pages.Prev", nil, func(p page.Page, pages page.Pages) { pages.Prev(p) }},
   66 		{"Pages.Shuffled.Next", shufflePages, func(p page.Page, pages page.Pages) { pages.Next(p) }},
   67 		{"Pages.Shuffled.Prev", shufflePages, func(p page.Page, pages page.Pages) { pages.Prev(p) }},
   68 		{"Pages.ByTitle.Next", func(pages page.Pages) page.Pages { return pages.ByTitle() }, func(p page.Page, pages page.Pages) { pages.Next(p) }},
   69 	} {
   70 		for _, numPages := range []int{300, 5000} {
   71 			b.Run(fmt.Sprintf("%s-pages-%d", variant.name, numPages), func(b *testing.B) {
   72 				b.StopTimer()
   73 				builder := newPagesPrevNextTestSite(b, numPages)
   74 				builder.Build(BuildCfg{SkipRender: true})
   75 				pages := builder.H.Sites[0].RegularPages()
   76 				if variant.preparePages != nil {
   77 					pages = variant.preparePages(pages)
   78 				}
   79 				b.StartTimer()
   80 				for i := 0; i < b.N; i++ {
   81 					p := pages[rand.Intn(len(pages))]
   82 					variant.run(p, pages)
   83 				}
   84 			})
   85 		}
   86 	}
   87 }
   88 
   89 func BenchmarkPagePageCollections(b *testing.B) {
   90 	type Variant struct {
   91 		name string
   92 		run  func(p page.Page)
   93 	}
   94 
   95 	for _, variant := range []Variant{
   96 		{".Pages", func(p page.Page) { p.Pages() }},
   97 		{".RegularPages", func(p page.Page) { p.RegularPages() }},
   98 		{".RegularPagesRecursive", func(p page.Page) { p.RegularPagesRecursive() }},
   99 	} {
  100 		for _, numPages := range []int{300, 5000} {
  101 			b.Run(fmt.Sprintf("%s-%d", variant.name, numPages), func(b *testing.B) {
  102 				b.StopTimer()
  103 				builder := newPagesPrevNextTestSite(b, numPages)
  104 				builder.Build(BuildCfg{SkipRender: true})
  105 				var pages page.Pages
  106 				for _, p := range builder.H.Sites[0].Pages() {
  107 					if !p.IsPage() {
  108 						pages = append(pages, p)
  109 					}
  110 				}
  111 				b.StartTimer()
  112 				for i := 0; i < b.N; i++ {
  113 					p := pages[rand.Intn(len(pages))]
  114 					variant.run(p)
  115 				}
  116 			})
  117 		}
  118 	}
  119 }