Skip to content

Commit d971d9f

Browse files
committed
changed the benchmarks to subtests
1 parent bf27a98 commit d971d9f

File tree

1 file changed

+37
-108
lines changed

1 file changed

+37
-108
lines changed

sdk/resource/benchmark_test.go

Lines changed: 37 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"go.opentelemetry.io/otel/attribute"
1414
"go.opentelemetry.io/otel/sdk/resource"
15-
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
1615
)
1716

1817
const conflict = 0.5
@@ -80,37 +79,17 @@ func BenchmarkMergeResource_16(b *testing.B) {
8079
benchmarkMergeResource(b, 16)
8180
}
8281

83-
type instantDetector struct{}
82+
type testDetector struct {
83+
durationMilliSecond time.Duration
84+
}
8485

8586
// instant detector don't do anything
8687
// its benchmark the overhead of the resource.New implementation.
87-
func (f instantDetector) Detect(_ context.Context) (*resource.Resource, error) {
88+
func (f testDetector) Detect(_ context.Context) (*resource.Resource, error) {
89+
time.Sleep(f.durationMilliSecond * time.Millisecond)
8890
return resource.NewSchemaless(), nil
8991
}
9092

91-
type fastDetector struct{}
92-
93-
func (f fastDetector) Detect(_ context.Context) (*resource.Resource, error) {
94-
time.Sleep(time.Millisecond)
95-
return resource.NewSchemaless(), nil
96-
}
97-
98-
type mediumDetector struct{}
99-
100-
func (f mediumDetector) Detect(_ context.Context) (*resource.Resource, error) {
101-
time.Sleep(time.Millisecond * 30)
102-
return resource.NewSchemaless(semconv.ServerAddress("localhost")), nil
103-
}
104-
105-
type slowDetector struct{}
106-
107-
func (f slowDetector) Detect(_ context.Context) (*resource.Resource, error) {
108-
time.Sleep(time.Millisecond * 500)
109-
return resource.NewSchemaless(semconv.ServerAddress("localhost"), semconv.MessageID(rand.Int())), nil
110-
}
111-
112-
var _ resource.Detector = &fakeDetector{}
113-
11493
func benchmarkOverhead(ctx context.Context, b *testing.B, testedDetector resource.Detector, n int) {
11594
detectors := []resource.Detector{}
11695
for i := 0; i < n; i++ {
@@ -124,88 +103,38 @@ func benchmarkOverhead(ctx context.Context, b *testing.B, testedDetector resourc
124103
}
125104
}
126105

127-
func BenchmarkNewResourceOverHead_1(b *testing.B) {
128-
benchmarkOverhead(context.Background(), b, instantDetector{}, 1)
129-
}
130-
131-
func BenchmarkNewResourceOverHead_2(b *testing.B) {
132-
benchmarkOverhead(context.Background(), b, instantDetector{}, 2)
133-
}
134-
135-
func BenchmarkNewResourceOverHead_4(b *testing.B) {
136-
benchmarkOverhead(context.Background(), b, instantDetector{}, 4)
137-
}
138-
139-
func BenchmarkNewResourceOverHead_8(b *testing.B) {
140-
benchmarkOverhead(context.Background(), b, instantDetector{}, 8)
141-
}
142-
143-
func BenchmarkNewResourceOverHead_16(b *testing.B) {
144-
benchmarkOverhead(context.Background(), b, instantDetector{}, 16)
145-
}
146-
147-
// fast
148-
149-
func BenchmarkFastDetector_1(b *testing.B) {
150-
benchmarkOverhead(context.Background(), b, fastDetector{}, 1)
151-
}
152-
153-
func BenchmarkFastDetector_2(b *testing.B) {
154-
benchmarkOverhead(context.Background(), b, fastDetector{}, 2)
155-
}
156-
157-
func BenchmarkFastDetector_4(b *testing.B) {
158-
benchmarkOverhead(context.Background(), b, fastDetector{}, 4)
159-
}
160-
161-
func BenchmarkFastDetector_8(b *testing.B) {
162-
benchmarkOverhead(context.Background(), b, fastDetector{}, 8)
163-
}
164-
165-
func BenchmarkFastDetector_16(b *testing.B) {
166-
benchmarkOverhead(context.Background(), b, fastDetector{}, 16)
167-
}
168-
169-
// medium.
170-
func BenchmarkMediumDetector_1(b *testing.B) {
171-
benchmarkOverhead(context.Background(), b, mediumDetector{}, 1)
172-
}
173-
174-
func BenchmarkMediumDetector_2(b *testing.B) {
175-
benchmarkOverhead(context.Background(), b, mediumDetector{}, 2)
176-
}
177-
178-
func BenchmarkMediumDetector_4(b *testing.B) {
179-
benchmarkOverhead(context.Background(), b, mediumDetector{}, 4)
180-
}
181-
182-
func BenchmarkMediumDetector_8(b *testing.B) {
183-
benchmarkOverhead(context.Background(), b, fastDetector{}, 8)
184-
}
185-
186-
func BenchmarkMediumDetector_16(b *testing.B) {
187-
benchmarkOverhead(context.Background(), b, fastDetector{}, 16)
188-
}
189-
190-
// slow.
191-
func BenchmarkSlowDetector_1(b *testing.B) {
192-
benchmarkOverhead(context.Background(), b, slowDetector{}, 1)
193-
}
194-
195-
func BenchmarkSlowDetector_2(b *testing.B) {
196-
benchmarkOverhead(context.Background(), b, slowDetector{}, 2)
197-
}
198-
199-
func BenchmarkSlowDetector_4(b *testing.B) {
200-
benchmarkOverhead(context.Background(), b, slowDetector{}, 4)
201-
}
202-
203-
func BenchmarkSlowDetector_8(b *testing.B) {
204-
benchmarkOverhead(context.Background(), b, slowDetector{}, 8)
205-
}
206-
207-
func BenchmarkSlowDetector_16(b *testing.B) {
208-
benchmarkOverhead(context.Background(), b, slowDetector{}, 16)
106+
func BenchmarkDetectors(b *testing.B) {
107+
type detectorType struct {
108+
name string
109+
resource.Detector
110+
}
111+
detectors := []detectorType{
112+
{
113+
name: "instantDetector",
114+
Detector: testDetector{durationMilliSecond: 0},
115+
},
116+
{
117+
name: "fastDetector",
118+
Detector: testDetector{durationMilliSecond: 1},
119+
},
120+
{
121+
name: "mediumDetector",
122+
Detector: testDetector{durationMilliSecond: 30},
123+
},
124+
{
125+
name: "slowDetector",
126+
Detector: testDetector{durationMilliSecond: 500},
127+
},
128+
}
129+
for _, detectorType := range detectors {
130+
b.Run(detectorType.name, func(b *testing.B) {
131+
for _, numberOfDetectors := range []int{1, 2, 4, 8, 16} {
132+
b.Run(fmt.Sprintf("%d-detectors", numberOfDetectors), func(b *testing.B) {
133+
benchmarkOverhead(context.Background(), b, testDetector{durationMilliSecond: 0}, numberOfDetectors)
134+
})
135+
}
136+
})
137+
}
209138
}
210139

211140
func BenchmarkDefaultResource(b *testing.B) {

0 commit comments

Comments
 (0)