private static Tuple <string, string, bool, TimeSpan> RunSample(MethodInfo sample) { var sampleName = sample.Name; var sampleAttributes = (SampleAttribute[])sample.GetCustomAttributes(typeof(SampleAttribute), false); var categoryNames = string.Join <SampleAttribute>(",", sampleAttributes); if (samplesCategoryRegex != null) { if (!sampleAttributes.Any(attribute => attribute.Match(samplesCategoryRegex))) { return(null); } } if (samplesToRunRegex != null) { //assumes method/sample names are unique, and different samples are delimited by ',' var containedInSamplesToRun = SparkCLRSamples.Configuration.SamplesToRun.ToLowerInvariant().Trim().Split(',').Contains(sampleName.ToLowerInvariant()); if (!containedInSamplesToRun && !samplesToRunRegex.IsMatch(sampleName)) { return(null); } } var clockStart = stopWatch.Elapsed; var duration = stopWatch.Elapsed - clockStart; try { if (!SparkCLRSamples.Configuration.IsDryrun) { SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Cyan, string.Format("----- Running sample {0} -----", sampleName)); sample.Invoke(null, new object[] { }); duration = stopWatch.Elapsed - clockStart; SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Green, string.Format("----- Successfully finished running sample {0} (duration={1}) -----", sampleName, duration)); } return(new Tuple <string, string, bool, TimeSpan>(sampleName, categoryNames, true, duration)); } catch (Exception ex) { duration = stopWatch.Elapsed - clockStart; SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Red, string.Format("----- Error running sample {0} (duration={3}) -----{1}{2}", sampleName, Environment.NewLine, ex, duration)); return(new Tuple <string, string, bool, TimeSpan>(sampleName, categoryNames, false, duration)); } }
private static bool ReportOutcome() { var succeededSamples = samplesRunInfoList.Where(x => x.Item3).ToList(); var failedSamples = samplesRunInfoList.Where(x => !x.Item3).ToList(); var summary = new StringBuilder().Append("----- ") .Append("Finished running ") .Append(string.Format("{0} samples(s) [succeeded={1}, failed={2}]", samplesRunInfoList.Count, succeededSamples.Count, failedSamples.Count)) .Append(" in ").Append(stopWatch.Elapsed) .AppendLine(" -----").ToString(); SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Yellow, summary); if (succeededSamples.Count > 0) { SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Green, "Successfully completed samples:"); foreach (var s in succeededSamples) { var sampleResult = new StringBuilder().Append(" ") .Append(string.Format("{0} (category: {1}), duration={2}", s.Item1, s.Item2, s.Item4)).ToString(); SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Green, sampleResult); } } if (failedSamples.Count > 0) { SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Red, "Failed samples:"); foreach (var s in failedSamples) { var sampleResult = new StringBuilder().Append(" ") .Append(string.Format("{0} (category: {1}), duration={2}", s.Item1, s.Item2, s.Item4)).ToString(); SparkCLRSamples.WriteColorCodedConsoleMessage(ConsoleColor.Red, sampleResult); } } return(failedSamples.Count == 0); }