refactor: use b.Loop() to simplify the code and improve performance

Signed-off-by: promalert <promalert@outlook.com>
This commit is contained in:
promalert
2025-10-29 16:52:55 +08:00
committed by Michael Mayer
parent a8f4286100
commit 5d0aa1068c
12 changed files with 24 additions and 24 deletions

View File

@@ -26,13 +26,13 @@ func TestASCII(t *testing.T) {
}
func BenchmarkASCII(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
ASCII("https://docs.photoprism.app/getting-started 👍/config-options/#file-converters")
}
}
func BenchmarkASCIIEmpty(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
ASCII("")
}
}

View File

@@ -27,13 +27,13 @@ func TestHeader(t *testing.T) {
}
func BenchmarkHeader(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
Header("https://..docs.photoprism.app/gettin\\g-started/config-options/\tfile-converters")
}
}
func BenchmarkHeaderEmpty(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
Header("")
}
}

View File

@@ -48,7 +48,7 @@ func TestSearchQuery(t *testing.T) {
func BenchmarkSearchQuery_Complex(b *testing.B) {
s := "Jens AND Mander and me Or Kitty WITH flowers IN the park AT noon | img% json OR BILL!\n"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = SearchQuery(s)
}
}
@@ -56,7 +56,7 @@ func BenchmarkSearchQuery_Complex(b *testing.B) {
func BenchmarkSearchQuery_Short(b *testing.B) {
s := "cat and dog"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = SearchQuery(s)
}
}
@@ -65,7 +65,7 @@ func BenchmarkSearchQuery_LongNoOps(b *testing.B) {
// No tokens to replace, primarily tests normalization + trim.
s := strings.Repeat("alpha beta gamma ", 50)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = SearchQuery(s)
}
}

View File

@@ -26,13 +26,13 @@ func TestUri(t *testing.T) {
}
func BenchmarkUri(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
Uri("https://docs.photoprism.app/getting-started/config-options/#file-converters")
}
}
func BenchmarkUriEmpty(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
Uri("")
}
}

View File

@@ -233,7 +233,7 @@ var benchDir = flag.String("benchdir", runtime.GOROOT(), "The directory to scan
func BenchmarkFastWalk(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
err := fastwalk.Walk(*benchDir, func(path string, typ os.FileMode) error { return nil })
if err != nil {
b.Fatal(err)

View File

@@ -27,13 +27,13 @@ func TestCacheControlMaxAge(t *testing.T) {
}
func BenchmarkTestCacheControlMaxAge(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
_ = CacheControlMaxAge(DurationYear, false)
}
}
func BenchmarkTestCacheControlMaxAgeImmutable(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
_ = CacheControlMaxAge(DurationYear, false) + ", " + CacheControlImmutable
}
}

View File

@@ -33,7 +33,7 @@ func BenchmarkContainsAny_LargeOverlap(b *testing.B) {
bList[i] = a[i*4]
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if !ContainsAny(a, bList) {
b.Fatalf("expected overlap")
}
@@ -44,7 +44,7 @@ func BenchmarkContainsAny_Disjoint(b *testing.B) {
a := makeStrings("a", 5000)
bList := makeStrings("b", 5000)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if ContainsAny(a, bList) {
b.Fatalf("expected disjoint")
}
@@ -56,7 +56,7 @@ func BenchmarkJoin_Large(b *testing.B) {
j := append(makeStrings("y", 5000), a[:1000]...) // 1000 duplicates
j = shuffleEveryK(j, 7)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
out := Join(a, j)
if len(out) != 10000 {
b.Fatalf("unexpected length: %d", len(out))

View File

@@ -166,7 +166,7 @@ func TestIsJoinToken(t *testing.T) {
}
func BenchmarkJoinToken(b *testing.B) {
for n := 0; n < b.N; n++ {
for b.Loop() {
JoinToken()
}
}

View File

@@ -29,7 +29,7 @@ func TestClip(t *testing.T) {
func BenchmarkClipRunesASCII(b *testing.B) {
s := strings.Repeat("abc def ghi ", 20) // ASCII
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = Clip(s, 50)
}
}
@@ -37,7 +37,7 @@ func BenchmarkClipRunesASCII(b *testing.B) {
func BenchmarkClipRunesUTF8(b *testing.B) {
s := strings.Repeat("Grüße 世", 20) // non-ASCII runes
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = Clip(s, 50)
}
}

View File

@@ -115,7 +115,7 @@ func TestContainsAlnumLower(t *testing.T) {
func BenchmarkContainsNumber(b *testing.B) {
s := "The quick brown fox jumps over 13 lazy dogs"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = ContainsNumber(s)
}
}
@@ -123,7 +123,7 @@ func BenchmarkContainsNumber(b *testing.B) {
func BenchmarkSortCaseInsensitive(b *testing.B) {
words := []string{"Zebra", "apple", "Banana", "cherry", "Apricot", "banana", "zebra", "Cherry"}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
w := append([]string(nil), words...)
SortCaseInsensitive(w)
}

View File

@@ -46,7 +46,7 @@ func makeLargeText(distinct, repeats int) string {
func BenchmarkWords_Large(b *testing.B) {
s := makeLargeText(200, 200) // ~40k tokens mixed
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = Words(s)
}
}
@@ -54,7 +54,7 @@ func BenchmarkWords_Large(b *testing.B) {
func BenchmarkUniqueKeywords_Large(b *testing.B) {
s := makeLargeText(200, 200)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = UniqueKeywords(s)
}
}
@@ -62,7 +62,7 @@ func BenchmarkUniqueKeywords_Large(b *testing.B) {
func BenchmarkUniqueKeywords_ManyDup(b *testing.B) {
s := makeLargeText(20, 2000) // many repeats, few distinct
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = UniqueKeywords(s)
}
}

File diff suppressed because one or more lines are too long