private static void GenerateProgramFile(string projectDir, Benchmark benchmark) { var isVoid = benchmark.Target.Method.ReturnType == typeof(void); var operationsPerInvoke = benchmark.Target.OperationsPerInvoke; var targetTypeNamespace = benchmark.Target.Type.Namespace; var targetMethodReturnTypeNamespace = benchmark.Target.Method.ReturnType == typeof(void) ? "System" : benchmark.Target.Method.ReturnType.Namespace; var targetTypeName = benchmark.Target.Type.FullName.Replace('+', '.'); var targetMethodName = benchmark.Target.Method.Name; var targetMethodReturnType = isVoid ? "void" : benchmark.Target.Method.ReturnType.GetCorrectTypeName(); var targetMethodResultHolder = isVoid ? "" : $"private {targetMethodReturnType} value;"; var targetMethodHoldValue = isVoid ? "" : "value = "; var targetMethodDelegateType = isVoid ? "Action " : $"Func<{targetMethodReturnType}> "; var idleImplementation = isVoid ? "" : $"return default({targetMethodReturnType});"; string runBenchmarkTemplate = ""; switch (benchmark.Task.Configuration.Mode) { case BenchmarkMode.SingleRun: runBenchmarkTemplate = GetTemplate("BenchmarkSingleRun.txt"); break; case BenchmarkMode.Throughput: runBenchmarkTemplate = GetTemplate("BenchmarkThroughput.txt"); break; } var contentTemplate = GetTemplate("BenchmarkProgram.txt"); var content = contentTemplate. Replace("$RunBenchmarkContent$", runBenchmarkTemplate). Replace("$OperationsPerInvoke$", operationsPerInvoke.ToInvariantString()). Replace("$TargetTypeNamespace$", targetTypeNamespace). Replace("$TargetMethodReturnTypeNamespace$", targetMethodReturnTypeNamespace). Replace("$TargetTypeName$", targetTypeName). Replace("$TargetMethodName$", targetMethodName). Replace("$TargetMethodResultHolder$", targetMethodResultHolder). Replace("$TargetMethodDelegateType$", targetMethodDelegateType). Replace("$TargetMethodHoldValue$", targetMethodHoldValue). Replace("$TargetMethodReturnType$", targetMethodReturnType). Replace("$IdleImplementation$", idleImplementation). Replace("$AdditionalLogic$", benchmark.Target.AdditionalLogic); string fileName = Path.Combine(projectDir, MainClassName + ".cs"); File.WriteAllText(fileName, content); }
public string GenerateProject(Benchmark benchmark) { var projectDir = CreateProjectDirectory(benchmark); GenerateProgramFile(projectDir, benchmark); GenerateProjectFile(projectDir, benchmark); GenerateAppConfigFile(projectDir, benchmark.Task.Configuration); return projectDir; }
public BenchmarkCodeExtractor(Benchmark benchmark, Process process, string codeExeName, IBenchmarkLogger logger) { this.process = process; this.codeExeName = codeExeName; this.logger = logger; //Method name format: "BenchmarkDotNet.Samples.Infra.RunFast()" (NOTE: WITHOUT the return type) var methodInfo = benchmark.Target.Method; fullTypeName = methodInfo.DeclaringType.FullName; var methodParams = string.Join(", ", methodInfo.GetParameters().Select(p => p.ParameterType.FullName)); fullMethodName = $"{fullTypeName}.{methodInfo.Name}({methodParams})"; }
private static string CreateProjectDirectory(Benchmark benchmark) { var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), benchmark.Caption); try { if (Directory.Exists(directoryPath)) Directory.Delete(directoryPath, true); } catch (Exception) { // Nevermind } if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); return directoryPath; }
public BenchmarkReport Run(Benchmark benchmark, IList<string> importantPropertyNames) { Logger.WriteLineHeader("// **************************"); Logger.WriteLineHeader("// Benchmark: " + benchmark.Description); var directoryPath = benchmarkProjectGenerator.GenerateProject(benchmark); Logger.WriteLineInfo("// Generated project: " + directoryPath); Logger.NewLine(); Logger.WriteLineInfo("// Build:"); var buildResult = benchmarkProjectGenerator.BuildProject(directoryPath, Logger); if (buildResult.OverallResult == BuildResultCode.Success) { Logger.WriteLineInfo("// OverallResult = Success"); } else { Logger.WriteLineError("// OverallResult = Failure"); if (buildResult.Exception != null) Logger.WriteLineError(buildResult.Exception.Message); return new BenchmarkReport(benchmark, new BenchmarkRunReport[0]); } Logger.NewLine(); var processCount = Math.Max(1, benchmark.Task.ProcessCount); var runReports = new List<BenchmarkRunReport>(); var exeFileName = Path.Combine(directoryPath, "Program.exe"); for (int processNumber = 0; processNumber < processCount; processNumber++) { Logger.WriteLineInfo($"// Run, Process: {processNumber + 1} / {processCount}"); if (importantPropertyNames.Any()) { Logger.WriteInfo("// "); foreach (var name in importantPropertyNames) Logger.WriteInfo($"{name}={benchmark.Properties.GetValue(name)} "); Logger.NewLine(); } var executor = new BenchmarkExecutor(Logger); if (File.Exists(exeFileName)) { var lines = executor.Exec(exeFileName, benchmark.Task.Settings.ToArgs()); var iterRunReports = lines.Select(line => BenchmarkRunReport.Parse(Logger, line)).Where(r => r != null).ToList(); runReports.AddRange(iterRunReports); } } Logger.NewLine(); return new BenchmarkReport(benchmark, runReports); }
private BenchmarkReport Run(Benchmark benchmark, IList<string> importantPropertyNames, BenchmarkParameters parameters = null) { var flow = BenchmarkFlowFactory.CreateFlow(benchmark, Logger); Logger.WriteLineHeader("// **************************"); Logger.WriteLineHeader("// Benchmark: " + benchmark.Description); var generateResult = Generate(flow); if (!generateResult.IsGenerateSuccess) return BenchmarkReport.CreateEmpty(benchmark, parameters); var buildResult = Build(flow, generateResult); if (!buildResult.IsBuildSuccess) return BenchmarkReport.CreateEmpty(benchmark, parameters); var runReports = Exec(benchmark, importantPropertyNames, parameters, flow, buildResult); return new BenchmarkReport(benchmark, runReports, parameters); }
private List<BenchmarkRunReport> Exec(Benchmark benchmark, IList<string> importantPropertyNames, BenchmarkParameters parameters, IBenchmarkFlow flow, BenchmarkBuildResult buildResult) { Logger.WriteLineInfo("// *** Exec ***"); var processCount = Math.Max(1, benchmark.Task.ProcessCount); var runReports = new List<BenchmarkRunReport>(); for (int processNumber = 0; processNumber < processCount; processNumber++) { Logger.WriteLineInfo($"// Run, Process: {processNumber + 1} / {processCount}"); if (parameters != null) Logger.WriteLineInfo($"// {parameters.ToInfo()}"); if (importantPropertyNames.Any()) { Logger.WriteInfo("// "); foreach (var name in importantPropertyNames) Logger.WriteInfo($"{name}={benchmark.Properties.GetValue(name)} "); Logger.NewLine(); } var execResult = flow.Exec(buildResult, parameters); if (execResult.FoundExecutable) { var iterRunReports = execResult.Data.Select(line => BenchmarkRunReport.Parse(Logger, line)).Where(r => r != null).ToList(); runReports.AddRange(iterRunReports); } else { Logger.WriteLineError("Executable not found"); } } Logger.NewLine(); return runReports; }
private static void GenerateProjectFile(string projectDir, Benchmark benchmark) { var configuration = benchmark.Task.Configuration; var platform = configuration.Platform.ToConfig(); var framework = configuration.Framework.ToConfig(); var template = GetTemplate("BenchmarkCsproj.txt"); var content = template. Replace("$Platform$", platform). Replace("$Framework$", framework). Replace("$TargetAssemblyReference$", GetReferenceToAssembly(benchmark.Target.Type)). Replace("$TargetMethodReturnTypeAssemblyReference$", GetReferenceToAssembly(benchmark.Target.Method.ReturnType)); string fileName = Path.Combine(projectDir, MainClassName + ".csproj"); File.WriteAllText(fileName, content); }
private BenchmarkReport Run(IBenchmarkLogger logger, Benchmark benchmark, IList<string> importantPropertyNames, BenchmarkParameters parameters = null) { var toolchain = Plugins.CreateToolchain(benchmark, logger); logger.WriteLineHeader("// **************************"); logger.WriteLineHeader("// Benchmark: " + benchmark.Description); var generateResult = Generate(logger, toolchain); if (!generateResult.IsGenerateSuccess) return BenchmarkReport.CreateEmpty(benchmark, parameters); var buildResult = Build(logger, toolchain, generateResult); if (!buildResult.IsBuildSuccess) return BenchmarkReport.CreateEmpty(benchmark, parameters); var runReports = Execute(logger, benchmark, importantPropertyNames, parameters, toolchain, buildResult); return new BenchmarkReport(benchmark, runReports, EnvironmentInfo.GetCurrentInfo(), parameters); }
private static void GenerateProgramFile(string projectDir, Benchmark benchmark) { var isVoid = benchmark.Target.Method.ReturnType == typeof(void); var operationsPerInvoke = benchmark.Target.OperationsPerInvoke; var targetTypeNamespace = benchmark.Target.Type.Namespace; var targetMethodReturnTypeNamespace = benchmark.Target.Method.ReturnType == typeof(void) ? "System" : benchmark.Target.Method.ReturnType.Namespace; var targetTypeName = benchmark.Target.Type.FullName.Replace('+', '.'); var targetMethodName = benchmark.Target.Method.Name; var targetMethodReturnType = isVoid ? "void" : benchmark.Target.Method.ReturnType.GetCorrectTypeName(); var targetMethodResultHolder = isVoid ? "" : $"private {targetMethodReturnType} value;"; var targetMethodHoldValue = isVoid ? "" : "value = "; var targetMethodDelegateType = isVoid ? "Action " : $"Func<{targetMethodReturnType}> "; // setupMethod is optional, so default to an empty delegate, so there is always something that can be invoked var setupMethodName = benchmark.Target.SetupMethod != null ? benchmark.Target.SetupMethod.Name : "() => { }"; var idleImplementation = isVoid ? "" : $"return default({targetMethodReturnType});"; var paramsContent = ""; if (benchmark.Task.Params != null) { var typeQualifier = benchmark.Task.Params.IsStatic ? $"{benchmark.Target.Type.Name}" : "instance"; paramsContent = $"{typeQualifier}.{benchmark.Task.Params.ParamFieldOrProperty} = BenchmarkParams.Parse(args);"; } string runBenchmarkTemplate = ""; switch (benchmark.Task.Configuration.Mode) { case BenchmarkMode.SingleRun: runBenchmarkTemplate = GetTemplate("BenchmarkSingleRun.txt"); break; case BenchmarkMode.Throughput: runBenchmarkTemplate = GetTemplate("BenchmarkThroughput.txt"); break; } var contentTemplate = GetTemplate("BenchmarkProgram.txt"); var content = contentTemplate. Replace("$RunBenchmarkContent$", runBenchmarkTemplate). Replace("$OperationsPerInvoke$", operationsPerInvoke.ToInvariantString()). Replace("$TargetTypeNamespace$", targetTypeNamespace). Replace("$TargetMethodReturnTypeNamespace$", targetMethodReturnTypeNamespace). Replace("$TargetTypeName$", targetTypeName). Replace("$TargetMethodName$", targetMethodName). Replace("$TargetMethodResultHolder$", targetMethodResultHolder). Replace("$TargetMethodDelegateType$", targetMethodDelegateType). Replace("$TargetMethodHoldValue$", targetMethodHoldValue). Replace("$TargetMethodReturnType$", targetMethodReturnType). Replace("$SetupMethodName$", setupMethodName). Replace("$IdleImplementation$", idleImplementation). Replace("$AdditionalLogic$", benchmark.Target.AdditionalLogic) .Replace("$ParamsContent$", paramsContent); string fileName = Path.Combine(projectDir, MainClassName + ".cs"); File.WriteAllText(fileName, content); }