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 DiagnosticSourceSubscriber #1391

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// <copyright file="DiagnosticSourceSubscriberBenchmark.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;
using System.Collections.Generic;
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using OpenTelemetry.Instrumentation;

namespace Benchmarks.Instrumentation
{
[InProcess]
[MemoryDiagnoser]
public class DiagnosticSourceSubscriberBenchmark
{
[Params(1, 2)]
public int SubscriberCount;

[Params(false, true)]
public bool UseIsEnabledFilter;

private const string SourceName = "MySource";

private readonly DiagnosticListener listener = new DiagnosticListener(SourceName);
private readonly List<DiagnosticSourceSubscriber> subscribers = new List<DiagnosticSourceSubscriber>();
private readonly Func<string, object, object, bool> isEnabledFilter = (name, arg1, arg2) => ((EventPayload)arg1).Data == "Data";

[GlobalSetup]
public void GlobalSetup()
{
for (var i = 0; i < this.SubscriberCount; ++i)
{
var subscriber = new DiagnosticSourceSubscriber(
new TestListener(),
this.UseIsEnabledFilter ? this.isEnabledFilter : null);

this.subscribers.Add(subscriber);
subscriber.Subscribe();
}
}

[GlobalCleanup]
public void GlobalCleanup()
{
foreach (var subscriber in this.subscribers)
{
subscriber.Dispose();
}
}

[Benchmark]
public void WriteDiagnosticSourceEvent()
{
var payload = new EventPayload("Data");
this.listener.Write("SomeEvent", payload);
}

private struct EventPayload
{
public EventPayload(string data)
{
this.Data = data;
}

public string Data { get; }
}

private class TestListener : ListenerHandler
{
public TestListener()
: base(DiagnosticSourceSubscriberBenchmark.SourceName)
{
}

public override bool SupportsNullActivity => true;
}
}
}