Skip to content

Commit 5d0aa10

Browse files
promalertlastzero
authored andcommitted
refactor: use b.Loop() to simplify the code and improve performance
Signed-off-by: promalert <[email protected]>
1 parent a8f4286 commit 5d0aa10

File tree

12 files changed

+24
-24
lines changed

12 files changed

+24
-24
lines changed

pkg/clean/ascii_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func TestASCII(t *testing.T) {
2626
}
2727

2828
func BenchmarkASCII(b *testing.B) {
29-
for n := 0; n < b.N; n++ {
29+
for b.Loop() {
3030
ASCII("https://docs.photoprism.app/getting-started 👍/config-options/#file-converters")
3131
}
3232
}
3333

3434
func BenchmarkASCIIEmpty(b *testing.B) {
35-
for n := 0; n < b.N; n++ {
35+
for b.Loop() {
3636
ASCII("")
3737
}
3838
}

pkg/clean/header_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func TestHeader(t *testing.T) {
2727
}
2828

2929
func BenchmarkHeader(b *testing.B) {
30-
for n := 0; n < b.N; n++ {
30+
for b.Loop() {
3131
Header("https://..docs.photoprism.app/gettin\\g-started/config-options/\tfile-converters")
3232
}
3333
}
3434

3535
func BenchmarkHeaderEmpty(b *testing.B) {
36-
for n := 0; n < b.N; n++ {
36+
for b.Loop() {
3737
Header("")
3838
}
3939
}

pkg/clean/search_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func TestSearchQuery(t *testing.T) {
4848
func BenchmarkSearchQuery_Complex(b *testing.B) {
4949
s := "Jens AND Mander and me Or Kitty WITH flowers IN the park AT noon | img% json OR BILL!\n"
5050
b.ReportAllocs()
51-
for i := 0; i < b.N; i++ {
51+
for b.Loop() {
5252
_ = SearchQuery(s)
5353
}
5454
}
5555

5656
func BenchmarkSearchQuery_Short(b *testing.B) {
5757
s := "cat and dog"
5858
b.ReportAllocs()
59-
for i := 0; i < b.N; i++ {
59+
for b.Loop() {
6060
_ = SearchQuery(s)
6161
}
6262
}
@@ -65,7 +65,7 @@ func BenchmarkSearchQuery_LongNoOps(b *testing.B) {
6565
// No tokens to replace, primarily tests normalization + trim.
6666
s := strings.Repeat("alpha beta gamma ", 50)
6767
b.ReportAllocs()
68-
for i := 0; i < b.N; i++ {
68+
for b.Loop() {
6969
_ = SearchQuery(s)
7070
}
7171
}

pkg/clean/uri_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func TestUri(t *testing.T) {
2626
}
2727

2828
func BenchmarkUri(b *testing.B) {
29-
for n := 0; n < b.N; n++ {
29+
for b.Loop() {
3030
Uri("https://docs.photoprism.app/getting-started/config-options/#file-converters")
3131
}
3232
}
3333

3434
func BenchmarkUriEmpty(b *testing.B) {
35-
for n := 0; n < b.N; n++ {
35+
for b.Loop() {
3636
Uri("")
3737
}
3838
}

pkg/fs/fastwalk/fastwalk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ var benchDir = flag.String("benchdir", runtime.GOROOT(), "The directory to scan
233233

234234
func BenchmarkFastWalk(b *testing.B) {
235235
b.ReportAllocs()
236-
for i := 0; i < b.N; i++ {
236+
for b.Loop() {
237237
err := fastwalk.Walk(*benchDir, func(path string, typ os.FileMode) error { return nil })
238238
if err != nil {
239239
b.Fatal(err)

pkg/http/header/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func TestCacheControlMaxAge(t *testing.T) {
2727
}
2828

2929
func BenchmarkTestCacheControlMaxAge(b *testing.B) {
30-
for n := 0; n < b.N; n++ {
30+
for b.Loop() {
3131
_ = CacheControlMaxAge(DurationYear, false)
3232
}
3333
}
3434

3535
func BenchmarkTestCacheControlMaxAgeImmutable(b *testing.B) {
36-
for n := 0; n < b.N; n++ {
36+
for b.Loop() {
3737
_ = CacheControlMaxAge(DurationYear, false) + ", " + CacheControlImmutable
3838
}
3939
}

pkg/list/bench_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func BenchmarkContainsAny_LargeOverlap(b *testing.B) {
3333
bList[i] = a[i*4]
3434
}
3535
b.ReportAllocs()
36-
for i := 0; i < b.N; i++ {
36+
for b.Loop() {
3737
if !ContainsAny(a, bList) {
3838
b.Fatalf("expected overlap")
3939
}
@@ -44,7 +44,7 @@ func BenchmarkContainsAny_Disjoint(b *testing.B) {
4444
a := makeStrings("a", 5000)
4545
bList := makeStrings("b", 5000)
4646
b.ReportAllocs()
47-
for i := 0; i < b.N; i++ {
47+
for b.Loop() {
4848
if ContainsAny(a, bList) {
4949
b.Fatalf("expected disjoint")
5050
}
@@ -56,7 +56,7 @@ func BenchmarkJoin_Large(b *testing.B) {
5656
j := append(makeStrings("y", 5000), a[:1000]...) // 1000 duplicates
5757
j = shuffleEveryK(j, 7)
5858
b.ReportAllocs()
59-
for i := 0; i < b.N; i++ {
59+
for b.Loop() {
6060
out := Join(a, j)
6161
if len(out) != 10000 {
6262
b.Fatalf("unexpected length: %d", len(out))

pkg/rnd/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestIsJoinToken(t *testing.T) {
166166
}
167167

168168
func BenchmarkJoinToken(b *testing.B) {
169-
for n := 0; n < b.N; n++ {
169+
for b.Loop() {
170170
JoinToken()
171171
}
172172
}

pkg/txt/clip_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ func TestClip(t *testing.T) {
2929
func BenchmarkClipRunesASCII(b *testing.B) {
3030
s := strings.Repeat("abc def ghi ", 20) // ASCII
3131
b.ReportAllocs()
32-
for i := 0; i < b.N; i++ {
32+
for b.Loop() {
3333
_ = Clip(s, 50)
3434
}
3535
}
3636

3737
func BenchmarkClipRunesUTF8(b *testing.B) {
3838
s := strings.Repeat("Grüße 世", 20) // non-ASCII runes
3939
b.ReportAllocs()
40-
for i := 0; i < b.N; i++ {
40+
for b.Loop() {
4141
_ = Clip(s, 50)
4242
}
4343
}

pkg/txt/contains_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ func TestContainsAlnumLower(t *testing.T) {
115115
func BenchmarkContainsNumber(b *testing.B) {
116116
s := "The quick brown fox jumps over 13 lazy dogs"
117117
b.ReportAllocs()
118-
for i := 0; i < b.N; i++ {
118+
for b.Loop() {
119119
_ = ContainsNumber(s)
120120
}
121121
}
122122

123123
func BenchmarkSortCaseInsensitive(b *testing.B) {
124124
words := []string{"Zebra", "apple", "Banana", "cherry", "Apricot", "banana", "zebra", "Cherry"}
125125
b.ReportAllocs()
126-
for i := 0; i < b.N; i++ {
126+
for b.Loop() {
127127
w := append([]string(nil), words...)
128128
SortCaseInsensitive(w)
129129
}

0 commit comments

Comments
 (0)