| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package identity_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
"github.com/gohugoio/hugo/identity" |
| 22 |
"github.com/gohugoio/hugo/identity/identitytesting" |
| 23 |
) |
| 24 |
|
| 25 |
func BenchmarkIdentityManager(b *testing.B) { |
| 26 |
createIds := func(num int) []identity.Identity { |
| 27 |
ids := make([]identity.Identity, num) |
| 28 |
for i := range num { |
| 29 |
name := fmt.Sprintf("id%d", i) |
| 30 |
ids[i] = &testIdentity{base: name, name: name} |
| 31 |
} |
| 32 |
return ids |
| 33 |
} |
| 34 |
|
| 35 |
b.Run("identity.NewManager", func(b *testing.B) { |
| 36 |
for b.Loop() { |
| 37 |
m := identity.NewManager() |
| 38 |
if m == nil { |
| 39 |
b.Fatal("manager is nil") |
| 40 |
} |
| 41 |
} |
| 42 |
}) |
| 43 |
|
| 44 |
b.Run("Add many", func(b *testing.B) { |
| 45 |
const size = 1000 |
| 46 |
ids := createIds(size) |
| 47 |
im := identity.NewManager() |
| 48 |
|
| 49 |
b.ResetTimer() |
| 50 |
for i := 0; b.Loop(); i++ { |
| 51 |
im.AddIdentity(ids[i%size]) |
| 52 |
} |
| 53 |
|
| 54 |
b.StopTimer() |
| 55 |
}) |
| 56 |
|
| 57 |
b.Run("Nop StringIdentity const", func(b *testing.B) { |
| 58 |
const id = identity.StringIdentity("test") |
| 59 |
for b.Loop() { |
| 60 |
identity.NopManager.AddIdentity(id) |
| 61 |
} |
| 62 |
}) |
| 63 |
|
| 64 |
b.Run("Nop StringIdentity const other package", func(b *testing.B) { |
| 65 |
for b.Loop() { |
| 66 |
identity.NopManager.AddIdentity(identitytesting.TestIdentity) |
| 67 |
} |
| 68 |
}) |
| 69 |
|
| 70 |
b.Run("Nop StringIdentity var", func(b *testing.B) { |
| 71 |
id := identity.StringIdentity("test") |
| 72 |
for b.Loop() { |
| 73 |
identity.NopManager.AddIdentity(id) |
| 74 |
} |
| 75 |
}) |
| 76 |
|
| 77 |
b.Run("Nop pointer identity", func(b *testing.B) { |
| 78 |
id := &testIdentity{base: "a", name: "b"} |
| 79 |
for b.Loop() { |
| 80 |
identity.NopManager.AddIdentity(id) |
| 81 |
} |
| 82 |
}) |
| 83 |
|
| 84 |
b.Run("Nop Anonymous", func(b *testing.B) { |
| 85 |
for b.Loop() { |
| 86 |
identity.NopManager.AddIdentity(identity.Anonymous) |
| 87 |
} |
| 88 |
}) |
| 89 |
} |
| 90 |
|
| 91 |
func BenchmarkIsNotDependent(b *testing.B) { |
| 92 |
runBench := func(b *testing.B, id1, id2 identity.Identity) { |
| 93 |
for b.Loop() { |
| 94 |
isNotDependent(id1, id2) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
newNestedManager := func(depth, count int) identity.Manager { |
| 99 |
m1 := identity.NewManager() |
| 100 |
for range depth { |
| 101 |
m2 := identity.NewManager() |
| 102 |
m1.AddIdentity(m2) |
| 103 |
for j := range count { |
| 104 |
id := fmt.Sprintf("id%d", j) |
| 105 |
m2.AddIdentity(&testIdentity{id, id, "", ""}) |
| 106 |
} |
| 107 |
m1 = m2 |
| 108 |
} |
| 109 |
return m1 |
| 110 |
} |
| 111 |
|
| 112 |
type depthCount struct { |
| 113 |
depth int |
| 114 |
count int |
| 115 |
} |
| 116 |
|
| 117 |
for _, dc := range []depthCount{{10, 5}} { |
| 118 |
b.Run(fmt.Sprintf("Nested not found %d %d", dc.depth, dc.count), func(b *testing.B) { |
| 119 |
im := newNestedManager(dc.depth, dc.count) |
| 120 |
id1 := identity.StringIdentity("idnotfound") |
| 121 |
b.ResetTimer() |
| 122 |
runBench(b, im, id1) |
| 123 |
}) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestIdentityManager(t *testing.T) { |
| 128 |
c := qt.New(t) |
| 129 |
|
| 130 |
newNestedManager := func() identity.Manager { |
| 131 |
m1 := identity.NewManager() |
| 132 |
m2 := identity.NewManager() |
| 133 |
m3 := identity.NewManager() |
| 134 |
m1.AddIdentity( |
| 135 |
testIdentity{"base", "id1", "", "pe1"}, |
| 136 |
testIdentity{"base2", "id2", "eq1", ""}, |
| 137 |
m2, |
| 138 |
m3, |
| 139 |
) |
| 140 |
|
| 141 |
m2.AddIdentity(testIdentity{"base4", "id4", "", ""}) |
| 142 |
|
| 143 |
return m1 |
| 144 |
} |
| 145 |
|
| 146 |
c.Run("Anonymous", func(c *qt.C) { |
| 147 |
im := newNestedManager() |
| 148 |
c.Assert(im.GetIdentity(), qt.Equals, identity.Anonymous) |
| 149 |
im.AddIdentity(identity.Anonymous) |
| 150 |
c.Assert(isNotDependent(identity.Anonymous, identity.Anonymous), qt.IsTrue) |
| 151 |
}) |
| 152 |
|
| 153 |
c.Run("GenghisKhan", func(c *qt.C) { |
| 154 |
c.Assert(isNotDependent(identity.GenghisKhan, identity.GenghisKhan), qt.IsTrue) |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
type testIdentity struct { |
| 159 |
base string |
| 160 |
name string |
| 161 |
|
| 162 |
idEq string |
| 163 |
idProbablyEq string |
| 164 |
} |
| 165 |
|
| 166 |
func (id testIdentity) Eq(other any) bool { |
| 167 |
ot, ok := other.(testIdentity) |
| 168 |
if !ok { |
| 169 |
return false |
| 170 |
} |
| 171 |
if ot.idEq == "" || id.idEq == "" { |
| 172 |
return false |
| 173 |
} |
| 174 |
return ot.idEq == id.idEq |
| 175 |
} |
| 176 |
|
| 177 |
func (id testIdentity) IdentifierBase() string { |
| 178 |
return id.base |
| 179 |
} |
| 180 |
|
| 181 |
func (id testIdentity) Name() string { |
| 182 |
return id.name |
| 183 |
} |
| 184 |
|
| 185 |
func (id testIdentity) ProbablyEq(other any) bool { |
| 186 |
ot, ok := other.(testIdentity) |
| 187 |
if !ok { |
| 188 |
return false |
| 189 |
} |
| 190 |
if ot.idProbablyEq == "" || id.idProbablyEq == "" { |
| 191 |
return false |
| 192 |
} |
| 193 |
return ot.idProbablyEq == id.idProbablyEq |
| 194 |
} |
| 195 |
|
| 196 |
func isNotDependent(a, b identity.Identity) bool { |
| 197 |
f := identity.NewFinder(identity.FinderConfig{}) |
| 198 |
r := f.Contains(b, a, -1) |
| 199 |
return r == 0 |
| 200 |
} |