package collections_test
import (
+ "context"
+ "fmt"
"testing"
"github.com/gohugoio/hugo/hugolib"
+ "github.com/gohugoio/hugo/resources/page"
+ "github.com/gohugoio/hugo/tpl/collections"
)
// Issue 9585
"intersect:[]string:[a b c d]",
)
}
+
+func BenchmarkWhereAndSortPages(b *testing.B) {
+ pageTemplate := `
+-- content/page%04d.md --
+---
+title: Page%04d
+---
+`
+
+ files := `
+-- hugo.toml --
+-- layouts/all.html --
+All.
+`
+
+ for i := 0; i < 500; i++ {
+ files += fmt.Sprintf(pageTemplate, i, i)
+ }
+
+ bb := hugolib.Test(b, files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) {
+ conf.BuildCfg = hugolib.BuildCfg{
+ SkipRender: true,
+ }
+ }))
+
+ s := bb.H.Sites[0]
+ seq := s.RegularPages()
+ ns := s.TemplateStore.GetTemplateFuncsNamespace("collections").(*collections.Namespace)
+
+ b.Run("Where", func(b *testing.B) {
+ for b.Loop() {
+ v, err := ns.Where(context.Background(), seq, "Title", "ge", "Page0480")
+ if err != nil {
+ b.Fatal(err)
+ }
+ res := v.(page.Pages)
+ if len(res) != 20 {
+ b.Fatalf("Where didn't return an expected result, got %d", len(res))
+ }
+ }
+ })
+
+ b.Run("Sort", func(b *testing.B) {
+ for b.Loop() {
+ v, err := ns.Sort(context.Background(), s.RegularPages(), "Title", "desc")
+ if err != nil {
+ b.Fatal(err)
+ }
+ res := v.(page.Pages)
+ if len(res) != 500 {
+ b.Fatalf("Sort didn't return an expected result, got %d", len(res))
+ }
+ }
+ })
+}
func BenchmarkWhereMap(b *testing.B) {
ns := newNs()
- seq := map[string]string{}
+ seqString := map[string]string{}
+ seqAny := map[string]any{}
+ seqInt := map[string]int{}
for i := range 1000 {
- seq[fmt.Sprintf("key%d", i)] = "value"
+ seqString[fmt.Sprintf("key%d", i)] = "value"
+ seqAny[fmt.Sprintf("key%d", i)] = "value"
+ seqInt[fmt.Sprintf("key%d", i)] = i
}
- for b.Loop() {
- _, err := ns.Where(context.Background(), seq, "key", "eq", "value")
- if err != nil {
- b.Fatal(err)
+ b.Run("String", func(b *testing.B) {
+ for b.Loop() {
+ _, err := ns.Where(context.Background(), seqString, "key", "eq", "value")
+ if err != nil {
+ b.Fatal(err)
+ }
}
- }
+ })
+
+ b.Run("Int", func(b *testing.B) {
+ for b.Loop() {
+ _, err := ns.Where(context.Background(), seqAny, "key", "eq", 42)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+ })
+
+ b.Run("Any", func(b *testing.B) {
+ for b.Loop() {
+ _, err := ns.Where(context.Background(), seqAny, "key", "eq", "value")
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+ })
}
func BenchmarkWhereSliceOfStructPointersWithMethod(b *testing.B) {
return &s
}
+// GetTemplateFuncsNamespace returns the given template funcs namespace.
+// Used in tests only.
+func (s *TemplateStore) GetTemplateFuncsNamespace(ns string) any {
+ v, err := s.storeSite.opts.TemplateFuncs[ns].(func(cctx context.Context, args ...any) (any, error))(context.Background())
+ if err != nil {
+ panic(fmt.Sprintf("template func namespace %q not found: %s", ns, err))
+ }
+ return v
+}
+
func (s *TemplateStore) findBestMatchGet(key string, category Category,
consider func(candidate *TemplInfo) bool, d1 TemplateDescriptor, dims1 sitesmatrix.VectorProvider, best *bestMatch,
) {