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 // See the LICENSE file in the project root for more information.
6 using System
.Collections
.Generic
;
7 using System
.Diagnostics
;
9 using Mono
.Profiler
.Log
;
11 namespace MonoTests
.Mono
.Profiler
.Log
{
13 sealed class ProfilerTestRun
{
15 sealed class ProfilerTestVisitor
: LogEventVisitor
{
17 public ProfilerTestRun Run { get; }
19 public List
<LogEvent
> Events { get; }
= new List
<LogEvent
> ();
21 public ProfilerTestVisitor (ProfilerTestRun run
)
26 public override void VisitBefore (LogEvent ev
)
32 public string Name { get; }
34 public string Options { get; }
36 readonly string _output
;
38 static volatile int _id
;
40 static string _testAssemblyPath
;
41 static Process _currentProcess
;
43 public ProfilerTestRun (string name
, string options
)
45 _testAssemblyPath
= Path
.Combine (Path
.GetDirectoryName (System
.Reflection
.Assembly
.GetExecutingAssembly ().Location
), "log-profiler-test.exe");
46 _currentProcess
= Process
.GetCurrentProcess();
49 _output
= $"test-{_id++}.mlpd";
52 public void Run (Action
<IReadOnlyList
<LogEvent
>> action
)
55 var events
= ParseFile ();
62 using (var proc
= new Process ()) {
63 proc
.StartInfo
= new ProcessStartInfo
{
64 UseShellExecute
= false,
65 FileName
= _currentProcess
.MainModule
.FileName
,
66 Arguments
= $"--debug --profile=log:nodefaults,output=\"{_output}\",{Options} {_testAssemblyPath} {Name}",
72 if (proc
.ExitCode
!= 0)
73 throw new Exception ($"Profiler test process exited with code: {proc.ExitCode}");
77 IReadOnlyList
<LogEvent
> ParseFile ()
79 var visitor
= new ProfilerTestVisitor (this);
81 using (var stream
= new LogStream (File
.OpenRead (_output
)))
82 new LogProcessor (stream
, null, visitor
).Process ();
84 return visitor
.Events
;