Debian Codegraph @

go/analysis/passes/modernize/testdata/src/bloop/bloop_test.go.golden

raw
1 //go:build go1.24
2
3 package bloop
4
5 import (
6 "sync"
7 "testing"
8 )
9
10 func BenchmarkA(b *testing.B) {
11 println("slow")
12
13 for b.Loop() { // want "b.N can be modernized using b.Loop.."
14 }
15 }
16
17 func BenchmarkB(b *testing.B) {
18 // setup
19 {
20 b.StopTimer()
21 println("slow")
22 b.StartTimer()
23 }
24
25 for i := range b.N { // Nope. Should we change this to "for i := 0; b.Loop(); i++"?
26 print(i)
27 }
28
29 b.StopTimer()
30 println("slow")
31 }
32
33 func BenchmarkC(b *testing.B) {
34 // setup
35 {
36
37 println("slow")
38
39 }
40
41 for b.Loop() { // want "b.N can be modernized using b.Loop.."
42 println("no uses of i")
43 }
44
45 b.StopTimer()
46 println("slow")
47 }
48
49 func BenchmarkD(b *testing.B) {
50 for i := 0; b.Loop(); i++ { // want "b.N can be modernized using b.Loop.."
51 println(i)
52 }
53 }
54
55 func BenchmarkE(b *testing.B) {
56 b.Run("sub", func(b *testing.B) {
57 b.StopTimer() // not deleted
58 println("slow")
59 b.StartTimer() // not deleted
60
61 // ...
62 })
63
64 for b.Loop() { // want "b.N can be modernized using b.Loop.."
65 println("no uses of i")
66 }
67
68 b.StopTimer()
69 println("slow")
70 }
71
72 func BenchmarkF(b *testing.B) {
73 var wg sync.WaitGroup
74 wg.Add(1)
75 go func() {
76 defer wg.Done()
77 for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
78 }
79 }()
80 wg.Wait()
81 }
82
83 func BenchmarkG(b *testing.B) {
84 var wg sync.WaitGroup
85 poster := func() {
86 for i := 0; i < b.N; i++ { // nope: b.N accessed from a FuncLit
87 }
88 wg.Done()
89 }
90 wg.Add(2)
91 for i := 0; i < 2; i++ {
92 go poster()
93 }
94 wg.Wait()
95 }
96
97 func BenchmarkH(b *testing.B) {
98 var wg sync.WaitGroup
99 wg.Add(1)
100 go func() {
101 defer wg.Done()
102 for range b.N { // nope: b.N accessed from a FuncLit
103 }
104 }()
105 wg.Wait()
106 }
107
108 func BenchmarkI(b *testing.B) {
109 for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
110 }
111 for i := 0; i < b.N; i++ { // nope: b.N accessed more than once in benchmark
112 }
113 }
114
115 func BenchmarkJ(b *testing.B) {
116 var wg sync.WaitGroup
117 ch := make(chan int, 10)
118 wg.Add(1)
119 go func() {
120 for i := 0; i < b.N; i++ {
121 <-ch
122 }
123 wg.Done()
124 }()
125 b.ResetTimer()
126 for i := 0; i < b.N; i++ { // nope: multiple b.N loops in same function
127 ch <- i
128 }
129 b.StopTimer()
130 wg.Wait()
131 }