Skip to content

Commit 79ab950

Browse files
committed
[Test][UserEvents] Add ManagedEvent scenario
1 parent 5e94c05 commit 79ab950

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Diagnostics.Tracing;
7+
using System.Threading;
8+
using Tracing.UserEvents.Tests.Common;
9+
using Microsoft.Diagnostics.Tracing;
10+
11+
namespace Tracing.UserEvents.Tests.ManagedEvent
12+
{
13+
public class ManagedEvent
14+
{
15+
public static void ManagedEventTracee()
16+
{
17+
long startTimestamp = Stopwatch.GetTimestamp();
18+
long targetTicks = Stopwatch.Frequency; // 1s
19+
20+
while (Stopwatch.GetTimestamp() - startTimestamp < targetTicks)
21+
{
22+
ManagedUserEventSource.Log.SampleEvent("SampleWork");
23+
Thread.Sleep(100);
24+
}
25+
}
26+
27+
private static readonly Func<EventPipeEventSource, bool> s_traceValidator = source =>
28+
{
29+
bool sampleEventFound = false;
30+
31+
source.Dynamic.All += (TraceEvent e) =>
32+
{
33+
if (!string.Equals(e.ProviderName, "ManagedUserEvent", StringComparison.OrdinalIgnoreCase))
34+
{
35+
return;
36+
}
37+
38+
if (e.EventName is null)
39+
{
40+
return;
41+
}
42+
43+
sampleEventFound = true;
44+
};
45+
46+
source.Process();
47+
48+
if (!sampleEventFound)
49+
{
50+
Console.Error.WriteLine("The trace did not contain the expected managed event.");
51+
}
52+
53+
return sampleEventFound;
54+
};
55+
56+
public static int Main(string[] args)
57+
{
58+
if (args.Length > 0 && args[0].Equals("tracee", StringComparison.OrdinalIgnoreCase))
59+
{
60+
ManagedEventTracee();
61+
return 0;
62+
}
63+
64+
return UserEventsTestRunner.Run("managedevent", typeof(ManagedEvent).Assembly.Location, s_traceValidator);
65+
}
66+
}
67+
68+
[EventSource(Name = "ManagedUserEvent")]
69+
internal sealed class ManagedUserEventSource : EventSource
70+
{
71+
public static readonly ManagedUserEventSource Log = new ManagedUserEventSource();
72+
73+
[Event(1)]
74+
public void SampleEvent(string requestName) => WriteEvent(1, requestName);
75+
}
76+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<CLRTestTargetUnsupported Condition="'$(TargetOS)' != 'linux' or ('$(TargetArchitecture)' != 'x64' and '$(TargetArchitecture)' != 'arm64')">true</CLRTestTargetUnsupported>
4+
<RequiresProcessIsolation>true</RequiresProcessIsolation>
5+
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="$(MSBuildProjectName).cs" />
10+
<ProjectReference Include="../common/userevents_common.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Content Include="managedevent.script">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
<TargetPath>managedevent.script</TargetPath>
17+
</Content>
18+
</ItemGroup>
19+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let manageduserevent_flags = new_dotnet_provider_flags();
2+
record_dotnet_provider("ManagedUserEvent", 0x0, 5, manageduserevent_flags);

0 commit comments

Comments
 (0)