Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active November 19, 2020 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/83159657122ad8f1df43cc4faff28151 to your computer and use it in GitHub Desktop.
Save itn3000/83159657122ad8f1df43cc4faff28151 to your computer and use it in GitHub Desktop.
small benchmark for Activity overhead
using System;
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
namespace activitybench
{
class BenchEventObserver : IObserver<KeyValuePair<string, object>>
{
public void OnCompleted()
{
}
public void OnError(Exception error)
{
}
public void OnNext(KeyValuePair<string, object> value)
{
}
}
class BenchListenerObserver : IObserver<DiagnosticListener>
{
public void OnCompleted()
{
}
public void OnError(Exception error)
{
}
public void OnNext(DiagnosticListener value)
{
if(value.Name.Equals("ActivityBench.Diagnostics", StringComparison.Ordinal))
{
value.Subscribe(new BenchEventObserver());
}
}
}
[MemoryDiagnoser]
[ShortRunJob]
public class ActivityBench
{
private static readonly DiagnosticListener _Source = new DiagnosticListener("ActivityBench.Diagnostics");
private static readonly DiagnosticListener _Source_Disabled = new DiagnosticListener("ActivityBench.Diagnostic_Disabled");
private static readonly ActivitySource _ActivitySource = new ActivitySource("ActivityBench.Activity");
private static readonly ActivitySource _ActivitySource_Disabled = new ActivitySource("ActivityBench.Activity_Disabled");
private static readonly ActivitySource _ActivitySource_Propagation = new ActivitySource("ActivityBench.Activity_Propagation");
private static readonly ActivitySource _ActivitySource_None = new ActivitySource("ActivityBench.Activity_None");
private static readonly ActivitySource _ActivitySource_AllDataAndRecorded = new ActivitySource("ActivityBench.Activity_AllDataAndRecorded");
private static readonly ActivityListener _Listener = CreateListener(ActivitySamplingResult.AllData, "ActivityBench.Activity");
private static readonly ActivityListener _Listener_Propagation = CreateListener(ActivitySamplingResult.PropagationData, "ActivityBench.Activity_Propagation");
private static readonly ActivityListener _Listener_None = CreateListener(ActivitySamplingResult.None, "ActivityBench.Activity_None");
private static readonly ActivityListener _Listener_AllDataAndRecorded = CreateListener(ActivitySamplingResult.AllDataAndRecorded, "ActivityBench.Activity_AllDataAndRecorded");
static ActivityListener CreateListener(ActivitySamplingResult samplingResult, string name)
{
return new ActivityListener()
{
Sample = (ref ActivityCreationOptions<ActivityContext> options) => samplingResult,
SampleUsingParentId = (ref ActivityCreationOptions<string> options) => samplingResult,
ActivityStarted = (act) => { return; },
ActivityStopped = (act) => { return; },
ShouldListenTo = (src) => src.Name.Equals(name),
};
}
[GlobalSetup]
public void Setup()
{
DiagnosticListener.AllListeners.Subscribe(new BenchListenerObserver());
ActivitySource.AddActivityListener(_Listener);
ActivitySource.AddActivityListener(_Listener_Propagation);
ActivitySource.AddActivityListener(_Listener_None);
ActivitySource.AddActivityListener(_Listener_AllDataAndRecorded);
}
[Benchmark]
public void CreateActivityOnly()
{
new Activity("createonly");
}
[Benchmark]
public void DiagnosticSource_Disabled()
{
Activity activity = null;
if(_Source_Disabled.IsEnabled())
{
activity = _Source_Disabled.StartActivity(new Activity("op1"), null);
}
if(_Source_Disabled.IsEnabled())
{
_Source_Disabled.StopActivity(activity, null);
}
}
[Benchmark]
public void DiagnosticSource_Enabled()
{
Activity activity = null;
if(_Source.IsEnabled())
{
activity = _Source.StartActivity(new Activity("op2"), null);
}
if(_Source.IsEnabled())
{
_Source.StopActivity(activity, null);
}
}
[Benchmark]
public void ActivitySource_Disabled()
{
using(var act = _ActivitySource_Disabled.StartActivity("op3", ActivityKind.Internal))
{
}
}
[Benchmark]
public void ActivitySource_None()
{
using(var act = _ActivitySource_None.StartActivity("op6"))
{
}
}
[Benchmark]
public void ActivitySource_Propagation()
{
using(var act = _ActivitySource_Propagation.StartActivity("op5"))
{
}
}
[Benchmark]
public void ActivitySource_AllData()
{
using(var act = _ActivitySource.StartActivity("op4", ActivityKind.Internal))
{
}
}
[Benchmark]
public void ActivitySource_AllDataAndRecorded()
{
using(var act = _ActivitySource_AllDataAndRecorded.StartActivity("op7"))
{
}
}
}
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run();
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment