forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
143 lines (127 loc) · 4.63 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Tests.Framework;
using Tests.Framework.Integration;
using Tests.Framework.ManagedElasticsearch;
using Tests.Framework.ManagedElasticsearch.Clusters;
using Tests.Framework.Profiling;
using Tests.Framework.Profiling.Memory;
using Tests.Framework.Profiling.Performance;
using Tests.Framework.Profiling.Timeline;
namespace Tests
{
// Microsoft test sdk injects a Main method for some reason into program
// (Relates to this issue: https://youtrack.jetbrains.com/issue/RSRP-464233)
// (See also https://github.com/elastic/elasticsearch-net/pull/2793)
// We provide an alternative StartupObject as part of the csproj for e.g `dotnet run` or executable output
// That will run the benchmarking/profiling.
public class Program { }
public class BenchmarkProgram
{
static BenchmarkProgram()
{
var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
if ((currentDirectory.Name == "Debug" || currentDirectory.Name == "Release") && currentDirectory.Parent.Name == "bin")
{
SdkPath = new DirectoryInfo(
Path.Combine(
Directory.GetCurrentDirectory(),
$@".\..\..\..\..\build\tools\{SelfProfileSdkDirectory}")).FullName;
OutputPath = new DirectoryInfo(
Path.Combine(
Directory.GetCurrentDirectory(),
$@".\..\..\..\..\build\output\profiling")).FullName;
}
else
{
SdkPath = new DirectoryInfo(
Path.Combine(
Directory.GetCurrentDirectory(),
$@".\build\tools\{SelfProfileSdkDirectory}")).FullName;
OutputPath = new DirectoryInfo(
Path.Combine(
Directory.GetCurrentDirectory(),
$@".\build\output\profiling")).FullName;
}
}
public static string InputDirPath { get; }
public static string OutputDirPath { get; }
private const string SelfProfileSdkDirectory = "dottrace-selfprofile";
private static string SdkPath { get; }
private static string OutputPath { get; }
public static void Main(string[] args)
{
if (args.Length == 0)
Console.WriteLine("Must specify at least one argument: TestAssemblyPath, Profile or Benchmark ");
var arguments = args.Skip(1).ToArray();
if (args[0].Equals("Profile", StringComparison.OrdinalIgnoreCase))
{
#if DOTNETCORE
Console.Error.WriteLine("DotTrace Profiling is not currently supported on .NET Core");
return;
#else
var configuration = ProfileConfiguration.Parse(arguments);
Console.WriteLine("Running Profiling with the following:");
Console.WriteLine($"- SdkPath: {SdkPath}");
Console.WriteLine($"- OutputPath: {OutputPath}");
Console.WriteLine($"- Classes: [{(configuration.ClassNames.Any() ? string.Join(",", configuration.ClassNames) : "*All*")}]");
using (var cluster = new ProfilingCluster())
{
foreach (var profilingFactory in CreateProfilingFactory(cluster))
{
profilingFactory.Run(configuration);
profilingFactory.RunAsync(configuration).Wait();
}
}
#endif
}
else if (args[0].Equals("Benchmark", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Running Benchmarking.");
if (args.Count() > 1 && args[1].Equals("non-interactive", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Running in Non-Interactive mode.");
foreach (var benchmarkType in GetBenchmarkTypes())
{
BenchmarkRunner.Run(benchmarkType);
}
return;
}
Console.WriteLine("Running in Interactive mode.");
var benchmarkSwitcher = new BenchmarkSwitcher(GetBenchmarkTypes());
benchmarkSwitcher.Run(arguments);
}
}
#if !DOTNETCORE
private static IEnumerable<IProfileFactory> CreateProfilingFactory(ClusterBase cluster)
{
yield return new PerformanceProfileFactory(SdkPath, OutputPath, cluster, Assembly.GetExecutingAssembly(), new ColoredConsoleWriter());
yield return new TimelineProfileFactory(SdkPath, OutputPath, cluster, Assembly.GetExecutingAssembly(), new ColoredConsoleWriter());
yield return new MemoryProfileFactory(SdkPath, OutputPath, cluster, Assembly.GetExecutingAssembly(), new ColoredConsoleWriter());
}
#endif
private static Type[] GetBenchmarkTypes()
{
IEnumerable<Type> types;
try
{
types = typeof(Program).Assembly().GetTypes();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types.Where(t => t != null);
}
return types
.Where(t => t.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Any(m => m.GetCustomAttributes(typeof(BenchmarkAttribute), false).Any()))
.OrderBy(t => t.Namespace)
.ThenBy(t => t.Name)
.ToArray();
}
}
}