Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for SuppressInstrumentation #1049

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/trace/benchmark/DummyActivityProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <copyright file="DummyActivityProcessor.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using OpenTelemetry.Trace;

internal class DummyActivityProcessor : ActivityProcessor
{
}
139 changes: 2 additions & 137 deletions docs/trace/benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,147 +14,12 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
public static void Main()
{
var summary = BenchmarkRunner.Run<TraceBenchmark>();
}

[MemoryDiagnoser]
public class TraceBenchmark
{
private readonly ActivitySource sourceWithNoListener = new ActivitySource("Benchmark.NoListener");
private readonly ActivitySource sourceWithPropagationDataListner = new ActivitySource("Benchmark.PropagationDataListner");
private readonly ActivitySource sourceWithAllDataListner = new ActivitySource("Benchmark.AllDataListner");
private readonly ActivitySource sourceWithAllDataAndRecordedListner = new ActivitySource("Benchmark.AllDataAndRecordedListner");
private readonly ActivitySource sourceWithOneProcessor = new ActivitySource("Benchmark.OneProcessor");
private readonly ActivitySource sourceWithTwoProcessors = new ActivitySource("Benchmark.TwoProcessors");
private readonly ActivitySource sourceWithThreeProcessors = new ActivitySource("Benchmark.ThreeProcessors");

public TraceBenchmark()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithPropagationDataListner.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.PropagationData,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.PropagationData,
});

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithAllDataListner.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.AllData,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.AllData,
});

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithAllDataAndRecordedListner.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.AllDataAndRecorded,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.AllDataAndRecorded,
});

Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(this.sourceWithOneProcessor.Name)
.AddProcessor(new DummyActivityProcessor())
.Build();

Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(this.sourceWithTwoProcessors.Name)
.AddProcessor(new DummyActivityProcessor())
.AddProcessor(new DummyActivityProcessor())
.Build();

Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddSource(this.sourceWithThreeProcessors.Name)
.AddProcessor(new DummyActivityProcessor())
.AddProcessor(new DummyActivityProcessor())
.AddProcessor(new DummyActivityProcessor())
.Build();
}

[Benchmark]
public void NoListener()
{
using (var activity = this.sourceWithNoListener.StartActivity("Benchmark"))
{
// this activity won't be created as there is no listener
}
}

[Benchmark]
public void PropagationDataListner()
{
using (var activity = this.sourceWithPropagationDataListner.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void AllDataListner()
{
using (var activity = this.sourceWithAllDataListner.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void AllDataAndRecordedListner()
{
using (var activity = this.sourceWithAllDataAndRecordedListner.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void OneProcessor()
{
using (var activity = this.sourceWithOneProcessor.StartActivity("Benchmark"))
{
}
}

[Benchmark]
public void TwoProcessors()
{
using (var activity = this.sourceWithTwoProcessors.StartActivity("Benchmark"))
{
}
}

[Benchmark]
public void ThreeProcessors()
{
using (var activity = this.sourceWithThreeProcessors.StartActivity("Benchmark"))
{
}
}
}

internal class DummyActivityProcessor : ActivityProcessor
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this so you can run like:

dotnet run --configuration Release --filter SuppressInstrumentationBenchmarks

or

dotnet run --configuration Release --filter TraceBenchmarks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanwest FYI the benchmark project in /test/ is already set up for that. We should combine these two benchmark projects at some point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep noticed that, that's where I copied the pattern. Put these next to @reyang's other benchmarks for now since they are similar, but we can discuss a better place once we're through with analysis.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping back from #1039 (comment).

}
}
123 changes: 123 additions & 0 deletions docs/trace/benchmark/SuppressInstrumentationBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// <copyright file="SuppressInstrumentationBenchmarks.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using OpenTelemetry;

[MemoryDiagnoser]
public class SuppressInstrumentationBenchmarks
{
private readonly ActivitySource sourceWithSuppressInstrumentation = new ActivitySource("Benchmark.SuppressInstrumentation");
private readonly ActivitySource sourceWithNoneListener = new ActivitySource("Benchmark.NoneListener");
private readonly ActivitySource sourceWithAllDataAndRecordedListner = new ActivitySource("Benchmark.AllDataAndRecordedListner");

public SuppressInstrumentationBenchmarks()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithSuppressInstrumentation.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => { return Sdk.SuppressInstrumentation ? ActivityDataRequest.None : ActivityDataRequest.AllDataAndRecorded; },
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => { return Sdk.SuppressInstrumentation ? ActivityDataRequest.None : ActivityDataRequest.AllDataAndRecorded; },
});

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithNoneListener.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.None,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.None,
});

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = null,
ActivityStopped = null,
ShouldListenTo = (activitySource) => activitySource.Name == this.sourceWithAllDataAndRecordedListner.Name,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.AllDataAndRecorded,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.AllDataAndRecorded,
});
}

[Benchmark]
public void SuppressInstrumentationFalse()
{
using (var activity = this.sourceWithSuppressInstrumentation.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void SuppressInstrumentationTrue()
{
using (SuppressInstrumentationScope.Begin())
{
using (var activity = this.sourceWithSuppressInstrumentation.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}
}

[Benchmark]
public void SuppressInstrumentationTrueNested()
{
using (SuppressInstrumentationScope.Begin())
{
using (SuppressInstrumentationScope.Begin())
{
using (var activity = this.sourceWithSuppressInstrumentation.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}
}
}

[Benchmark]
public void NoneListener()
{
using (var activity = this.sourceWithNoneListener.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void AllDataAndRecordedListener1()
{
using (var activity = this.sourceWithAllDataAndRecordedListner.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}

[Benchmark]
public void AllDataAndRecordedListener2()
{
var b = Sdk.SuppressInstrumentation;
using (var activity = this.sourceWithAllDataAndRecordedListner.StartActivity("Benchmark"))
{
// this activity will be created and feed into an ActivityListener that simply drops everything on the floor
}
}
}
Loading