Skip to content

Commit

Permalink
changed the benchmarks to subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
OrHayat committed Jun 23, 2024
1 parent a4fc913 commit 5488db6
Showing 1 changed file with 37 additions and 108 deletions.
145 changes: 37 additions & 108 deletions sdk/resource/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
)

const conflict = 0.5
Expand Down Expand Up @@ -80,37 +79,17 @@ func BenchmarkMergeResource_16(b *testing.B) {
benchmarkMergeResource(b, 16)
}

type instantDetector struct{}
type testDetector struct {
durationMilliSecond time.Duration
}

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

type fastDetector struct{}

func (f fastDetector) Detect(_ context.Context) (*resource.Resource, error) {
time.Sleep(time.Millisecond)
return resource.NewSchemaless(), nil
}

type mediumDetector struct{}

func (f mediumDetector) Detect(_ context.Context) (*resource.Resource, error) {
time.Sleep(time.Millisecond * 30)
return resource.NewSchemaless(semconv.ServerAddress("localhost")), nil
}

type slowDetector struct{}

func (f slowDetector) Detect(_ context.Context) (*resource.Resource, error) {
time.Sleep(time.Millisecond * 500)
return resource.NewSchemaless(semconv.ServerAddress("localhost"), semconv.MessageID(rand.Int())), nil
}

var _ resource.Detector = &fakeDetector{}

func benchmarkOverhead(ctx context.Context, b *testing.B, testedDetector resource.Detector, n int) {
detectors := []resource.Detector{}
for i := 0; i < n; i++ {
Expand All @@ -124,88 +103,38 @@ func benchmarkOverhead(ctx context.Context, b *testing.B, testedDetector resourc
}
}

func BenchmarkNewResourceOverHead_1(b *testing.B) {
benchmarkOverhead(context.Background(), b, instantDetector{}, 1)
}

func BenchmarkNewResourceOverHead_2(b *testing.B) {
benchmarkOverhead(context.Background(), b, instantDetector{}, 2)
}

func BenchmarkNewResourceOverHead_4(b *testing.B) {
benchmarkOverhead(context.Background(), b, instantDetector{}, 4)
}

func BenchmarkNewResourceOverHead_8(b *testing.B) {
benchmarkOverhead(context.Background(), b, instantDetector{}, 8)
}

func BenchmarkNewResourceOverHead_16(b *testing.B) {
benchmarkOverhead(context.Background(), b, instantDetector{}, 16)
}

// fast

func BenchmarkFastDetector_1(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 1)
}

func BenchmarkFastDetector_2(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 2)
}

func BenchmarkFastDetector_4(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 4)
}

func BenchmarkFastDetector_8(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 8)
}

func BenchmarkFastDetector_16(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 16)
}

// medium.
func BenchmarkMediumDetector_1(b *testing.B) {
benchmarkOverhead(context.Background(), b, mediumDetector{}, 1)
}

func BenchmarkMediumDetector_2(b *testing.B) {
benchmarkOverhead(context.Background(), b, mediumDetector{}, 2)
}

func BenchmarkMediumDetector_4(b *testing.B) {
benchmarkOverhead(context.Background(), b, mediumDetector{}, 4)
}

func BenchmarkMediumDetector_8(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 8)
}

func BenchmarkMediumDetector_16(b *testing.B) {
benchmarkOverhead(context.Background(), b, fastDetector{}, 16)
}

// slow.
func BenchmarkSlowDetector_1(b *testing.B) {
benchmarkOverhead(context.Background(), b, slowDetector{}, 1)
}

func BenchmarkSlowDetector_2(b *testing.B) {
benchmarkOverhead(context.Background(), b, slowDetector{}, 2)
}

func BenchmarkSlowDetector_4(b *testing.B) {
benchmarkOverhead(context.Background(), b, slowDetector{}, 4)
}

func BenchmarkSlowDetector_8(b *testing.B) {
benchmarkOverhead(context.Background(), b, slowDetector{}, 8)
}

func BenchmarkSlowDetector_16(b *testing.B) {
benchmarkOverhead(context.Background(), b, slowDetector{}, 16)
func BenchmarkDetectors(b *testing.B) {
type detectorType struct {
name string
resource.Detector
}
detectors := []detectorType{
{
name: "instantDetector",
Detector: testDetector{durationMilliSecond: 0},
},
{
name: "fastDetector",
Detector: testDetector{durationMilliSecond: 1},
},
{
name: "mediumDetector",
Detector: testDetector{durationMilliSecond: 30},
},
{
name: "slowDetector",
Detector: testDetector{durationMilliSecond: 500},
},
}
for _, detectorType := range detectors {
b.Run(detectorType.name, func(b *testing.B) {
for _, numberOfDetectors := range []int{1, 2, 4, 8, 16} {
b.Run(fmt.Sprintf("%d-detectors", numberOfDetectors), func(b *testing.B) {
benchmarkOverhead(context.Background(), b, testDetector{durationMilliSecond: 0}, numberOfDetectors)
})
}
})
}
}

func BenchmarkDefaultResource(b *testing.B) {
Expand Down

0 comments on commit 5488db6

Please sign in to comment.