Exemple #1
0
        private static string GenerateParamsStartCode(BenchmarkInfo info)
        {
            if (info.Params != null)
            {
                var paramsStartCodeTemplate =
                    @"int [] paramArgs = new [] { ##PARAM-ARGS## };
for (int param = 0; param < paramArgs.Length; param++)
{
    benchmarkClass.##PARAM-NAME## = paramArgs[param];
    Console.WriteLine(""\nParam = "" + benchmarkClass.##PARAM-NAME##);
";
                var paramsStartCode = paramsStartCodeTemplate
                                      .Replace("##PARAM-ARGS##", String.Join(", ", info.Params.Args))
                                      .Replace("##PARAM-NAME##", info.ParamsFieldName);
                return(paramsStartCode);
            }
            else if (info.ParamsWithSteps != null)
            {
                var paramsStartCodeTemplate =
                    @"for (int param = ##START##; param <= ##END##; param += ##STEP##)
{
    benchmarkClass.##PARAM-NAME## = param;
    Console.WriteLine(""\nParam = "" + benchmarkClass.##PARAM-NAME##);
";
                var paramsStartCode = paramsStartCodeTemplate
                                      .Replace("##START##", info.ParamsWithSteps.Start.ToString(CultureInfo.InvariantCulture))
                                      .Replace("##END##", info.ParamsWithSteps.End.ToString(CultureInfo.InvariantCulture))
                                      .Replace("##STEP##", info.ParamsWithSteps.Step.ToString(CultureInfo.InvariantCulture))
                                      .Replace("##PARAM-NAME##", info.ParamsFieldName);
                return(paramsStartCode);
            }

            return(String.Empty);
        }
Exemple #2
0
        private static string GetMethodCallWithParameters(BenchmarkInfo info, bool warmupMethod = false)
        {
            var methodParameters = new StringBuilder();

            foreach (var paramater in info.ParametersToInject)
            {
                switch (paramater)
                {
                case "IterationParams":
                    methodParameters.Append(warmupMethod ? "warmupIterations" : "iterations");
                    break;
                    // TODO Add in "BenchmarkParams" support
                    //case "BenchmarkParams":
                }
            }

            string methodCallWithParameters = string.Format("benchmarkClass.{0}({1})", info.MethodName, methodParameters);

            if (info.GenerateBlackhole)
            {
                methodCallWithParameters = string.Format("blackhole.Consume({0})", methodCallWithParameters);
            }

            return(methodCallWithParameters);
        }
Exemple #3
0
        internal static string ProcessCodeTemplates(BenchmarkInfo info)
        {
            // TODO at some point, we might need a less-hacky templating mechanism?!
            // Maybe Razor? see https://github.com/volkovku/RazorTemplates

            var benchmarkMethodCall = GetMethodCallWithParameters(info);
            var warmupMethodCall    = GetMethodCallWithParameters(info, warmupMethod: true);

            // We know we produce code with wierd/no formatting, but later on we use Roslyn to fix it for us!!
            // See Formatter.Format() call in GenerateRunners() in CodeGenerator.cs
            string paramsStartCode = String.Empty, paramsEndCode = String.Empty;

            if (info.ParamsFieldName != null && (info.Params != null || info.ParamsWithSteps != null))
            {
                paramsStartCode = GenerateParamsStartCode(info);
                // We always generate a loop, so the end code is that same
                paramsEndCode = "}\n";
            }

            var setupMethodCode = String.Empty;

            if (info.SetupMethod != null)
            {
                setupMethodCode = "benchmarkClass." + info.SetupMethod + "();";
            }

            var generatedBenchmark = benchmarkHarnessTemplate
                                     .Replace(namespaceReplaceText, info.NamespaceName)
                                     .Replace(classReplaceText, info.ClassName)
                                     .Replace(methodReplaceText, info.MethodName)
                                     .Replace(warmupMethodCallReplaceText, warmupMethodCall)
                                     .Replace(benchmarkMethodCallReplaceText, benchmarkMethodCall)
                                     .Replace(generatedClassReplaceText, info.GeneratedClassName)
                                     .Replace(paramsStartCodeReplaceText, paramsStartCode)
                                     .Replace(paramsEndCodeReplaceText, paramsEndCode)
                                     .Replace(setupMethodCallReplaceText, setupMethodCode);

            return(generatedBenchmark);
        }