]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/collections: Add some more benchmarks for where and sort
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 13 Feb 2026 08:53:10 +0000 (09:53 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 13 Feb 2026 09:34:39 +0000 (10:34 +0100)
tpl/collections/collections_integration_test.go
tpl/collections/where_test.go
tpl/tplimpl/templatestore.go

index a1a1485241969c35f157afbd8e1a67b2acd19777..ee003e049912767e2c8386f7e8794ad70c6a6de0 100644 (file)
 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
@@ -555,3 +559,58 @@ tags_weight: 20
                "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))
+                       }
+               }
+       })
+}
index 2caf1e830e6fd65551c9ed114759d33d88c63b53..1822e9435e62dd6dab046ecb42e8b7d95716db3d 100644 (file)
@@ -906,18 +906,42 @@ func BenchmarkWhereOps(b *testing.B) {
 
 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) {
index 944a0953fd36329911e08fd9860d0f8b5c60bfd7..1d4e537fda670439336dda09adf33e233e7cb3ed 100644 (file)
@@ -810,6 +810,16 @@ func (s TemplateStore) WithSiteOpts(opts SiteOptions) *TemplateStore {
        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,
 ) {