Skip to content

Commit 6eb7c7e

Browse files
committed
[Test][UserEvents] Add ManagedEvent scenario
1 parent 21ea5f6 commit 6eb7c7e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Diagnostics.Tracing;
8+
using System.Threading;
9+
using Tracing.UserEvents.Tests.Common;
10+
using Microsoft.Diagnostics.Tracing;
11+
12+
namespace Tracing.UserEvents.Tests.ManagedEvent
13+
{
14+
public class ManagedEvent
15+
{
16+
public static void ManagedEventTracee()
17+
{
18+
long startTimestamp = Stopwatch.GetTimestamp();
19+
long targetTicks = Stopwatch.Frequency; // 1s
20+
21+
while (Stopwatch.GetTimestamp() - startTimestamp < targetTicks)
22+
{
23+
ManagedUserEventSource.Log.SampleEvent("SampleWork");
24+
Thread.Sleep(100);
25+
}
26+
}
27+
28+
private static readonly Func<EventPipeEventSource, bool> s_traceValidator = source =>
29+
{
30+
bool sampleEventFound = false;
31+
32+
source.Dynamic.All += (TraceEvent e) =>
33+
{
34+
if (!string.Equals(e.ProviderName, "ManagedUserEvent", StringComparison.OrdinalIgnoreCase))
35+
{
36+
return;
37+
}
38+
39+
if (e.EventName is null)
40+
{
41+
return;
42+
}
43+
44+
sampleEventFound = true;
45+
};
46+
47+
source.Process();
48+
49+
if (!sampleEventFound)
50+
{
51+
Console.Error.WriteLine("The trace did not contain the expected managed event.");
52+
}
53+
54+
return sampleEventFound;
55+
};
56+
57+
public static int Main(string[] args)
58+
{
59+
if (args.Length > 0 && args[0].Equals("tracee", StringComparison.OrdinalIgnoreCase))
60+
{
61+
ManagedEventTracee();
62+
return 0;
63+
}
64+
65+
return UserEventsTestRunner.Run("managedevent", typeof(ManagedEvent).Assembly.Location, s_traceValidator);
66+
}
67+
}
68+
69+
[EventSource(Name = "ManagedUserEvent")]
70+
internal sealed class ManagedUserEventSource : EventSource
71+
{
72+
public static readonly ManagedUserEventSource Log = new ManagedUserEventSource();
73+
74+
[Event(1)]
75+
public void SampleEvent(string requestName) => WriteEvent(1, requestName);
76+
}
77+
}
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)