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 EventListener microbenchmarks #1800

Merged
merged 3 commits into from
Feb 8, 2022
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,90 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

namespace System.Diagnostics.Tracing
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_EventListener
{
private enum TestEnum
{
Foo = 123
}

private sealed class BenchmarkEventSource : EventSource
{
public BenchmarkEventSource() : base(nameof(Perf_EventListener)) { }

[Event(1)]
public void EventNoParams() => WriteEvent(1);

[Event(2)]
public void EventIntParams(int arg1, int arg2, int arg3) => WriteEvent(2, arg1, arg2, arg3);

[Event(3)]
public void EventStringParams(string arg1, string arg2, string arg3) => WriteEvent(3, arg1, arg2, arg3);

[Event(4)]
public unsafe void EventMixedParams(int arg1, string arg2, TestEnum arg3)
{
arg2 ??= "";

fixed (char* arg2Ptr = arg2)
{
const int NumEventDatas = 3;
EventData* descrs = stackalloc EventData[NumEventDatas];

descrs[0] = new EventData
{
DataPointer = (IntPtr)(&arg1),
Size = sizeof(int)
};
descrs[1] = new EventData
{
DataPointer = (IntPtr)(arg2Ptr),
Size = (arg2.Length + 1) * sizeof(char)
};
descrs[2] = new EventData
{
DataPointer = (IntPtr)(&arg3),
Size = sizeof(TestEnum)
};

WriteEventCore(4, NumEventDatas, descrs);
}
}
}

private sealed class BenchmarkEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == nameof(Perf_EventListener))
{
EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.None);
}
}

protected override void OnEventWritten(EventWrittenEventArgs eventData) { }
}

private static readonly BenchmarkEventSource _eventSource = new BenchmarkEventSource();
private static readonly BenchmarkEventListener _eventListener = new BenchmarkEventListener();

[Benchmark]
public void WriteEvent_NoParams() => _eventSource.EventNoParams();

[Benchmark]
public void WriteEvent_IntParams() => _eventSource.EventIntParams(1, 2, 3);

[Benchmark]
public void WriteEvent_StringParams() => _eventSource.EventStringParams("foo", "bar", "foobar");

[Benchmark]
public void WriteEvent_MixedParams() => _eventSource.EventMixedParams(123, "foo", TestEnum.Foo);
}
}