public static async Task Execute(IPerfCounterCollectorUC perfCollector, ITextWriter textWriter) { Process.GetCurrentProcess().ProcessName.WriteLine(); BenchmarkingInitPoint.PrintIntro(); string.Empty.WriteLine().WriteLine(); ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; string procName = ProcessorName(); string osName = OperatingSystemName(); ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report("SystemInformation:", BenchInfoNames.Name) .Report(procName, BenchInfoNames.NameCPU) .Report(Environment.ProcessorCount.ToString(), BenchInfoNames.CoresCPU) .Report(Environment.OSVersion.ToString(), BenchInfoNames.VersionOS) .Report(osName, BenchInfoNames.NameOS) .ToString() .WriteLine() ; string.Empty.WriteLine().WriteLine(); ImmutableArray <int> spinsSet = 101.GenerateSpinsSet(); ImmutableArray .Create <string>() .Add("SpinsSet:") .AddRange(spinsSet.Select(x => x.ToString())) .Add(string.Empty) .Add(string.Empty) .Aggregate(string.Empty, (c, n) => string.IsNullOrEmpty(c) ? n : $"{c}{Environment.NewLine}{n}") .WriteLine() ; BenchmarkManagersConfig .BenchmarkManagers .Where(x => x.CollectorSupport == Collector.Collect && !x.IsDataCollector) .ToImmutableArray() .AssignOut(out ImmutableArray <IBenchmarkManager> candidateBenchmarks) ; ImmutableArray .Create <string>() .Add("Selected Benchmark Managers:") .AddRange(candidateBenchmarks.Select(x => x.Name)) .Add(string.Empty) .Add(string.Empty) .Aggregate(string.Empty, (c, n) => string.IsNullOrEmpty(c) ? n : $"{c}{Environment.NewLine}{n}") .WriteLine() ; await Task.Delay(2000); //IBenchmarkManager sequential = BenchmarkManagersConfig.BenchmarkManagers.FirstOrDefault(x => x.IsSequential); //IBenchmarkConfiguration sequentialConfig = new BenchmarkConfiguration(sequential, perfCollector, BenchmarkGlobalSettings.TestingTimeSpan, 1000000, textWriter); //await (sequential?.ExecuteBenchmark(sequentialConfig) ?? Task.CompletedTask); string.Empty.WriteLine().WriteLine(); foreach (int spins in spinsSet) { foreach (IBenchmarkManager info in candidateBenchmarks) { await Task.Delay(2000); await info.ExecuteBenchmark(new BenchmarkConfiguration(info, perfCollector, BenchmarkGlobalSettings.TestingTimeSpan, spins, textWriter)); string.Empty.WriteLine().WriteLine(); } } BenchmarkingInitPoint.PrintExit(); }
public static async Task Execute <TSyncPrimitive>(IBenchmark <TSyncPrimitive> benchmark) where TSyncPrimitive : class { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true, compacting: true); ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; Enumerable .Range(0, benchmark.ThreadGroups) .Select(ThreadGroupIndex.New) .Select(benchmark.CreateBenchInstance) .SelectMany(xws => xws) .ToArray() .AssignOut(out BenchmarkWorker[] workers) ; Task[] tasks = new Task[workers.Length]; for (int i = 0; i < workers.Length; ++i) { tasks[i] = workers[i].RunProcessing(); } await benchmark.PerfCollectorTryClearAsync(); await benchmark.PerfCollectorTryStartAsync(); await Task.WhenAll(tasks); await benchmark.PerfCollectorTryStopAsync(); workers .Select(w => ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(benchmark.Name, BenchInfoNames.Name) .Report(w.Pair, BenchInfoNames.ThreadPair) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{w.ElapsedMilliseconds}", BenchInfoNames.Time_ms) .Report($"{w.Iterations}", BenchInfoNames.Iterations) .Report($"{w.Spins}", BenchInfoNames.Spins) .Report($"{w.ThroughputPerMillisecond:0.000}", BenchInfoNames.Throughput_ms) .Report(w.ResourceName, BenchInfoNames.ResourceName) .ToString()) .Aggregate(string.Empty, (c, n) => string.IsNullOrEmpty(c) ? n : $"{c}{Environment.NewLine}{n}") .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; long totalSpins = workers.Select(w => w.Spins).Aggregate(0L, (c, n) => c + n); long avgSpins = (int)(totalSpins / Math.Max(workers.LongLength, 1L)); long totalTime = workers.Select(w => w.ElapsedMilliseconds).Aggregate(0L, (c, n) => c + n); long avgTime = totalTime / Math.Max(workers.LongLength, 1L); //long medianTime = (long)workers.Select(w => (double) w.ElapsedMilliseconds).Median(); long totalIterations = workers.Select(w => (long)w.Iterations).Aggregate(0L, (c, n) => c + n); long avgIterations = totalIterations / Math.Max(workers.Length, 1L); long medianIterations = (long)workers.Select(w => (double)w.Iterations).Median(); double totalCPU = benchmark.PerfCollector.Select(cpu => cpu).Aggregate(0.0, (c, n) => c + n); double avgCPU = totalCPU / Math.Max(benchmark.PerfCollector.Count(), 1L); double medianCPU = benchmark.PerfCollector.Median(); double totalThroughput = workers.Select(w => w.ThroughputPerMillisecond).Aggregate(0.0, (c, n) => c + n); double avgThroughput = totalThroughput / Math.Max(workers.Length, 1L); double medianThroughput = workers.Select(w => w.ThroughputPerMillisecond).Median(); benchmark .PerfCollector .Select(cpu => ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(benchmark.Name, BenchInfoNames.Name) .Report($"{cpu}", BenchInfoNames.CPU) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{avgTime}", BenchInfoNames.Time_ms) .Report($"{avgSpins}", BenchInfoNames.Spins) .Report($"{avgIterations}", BenchInfoNames.AvgIterations) .ToString()) .Aggregate(string.Empty, (c, n) => string.IsNullOrEmpty(c) ? n : $"{c}{Environment.NewLine}{n}") .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(benchmark.Name, BenchInfoNames.Name) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{totalIterations}", BenchInfoNames.TotalIterations) .Report($"{avgTime}", BenchInfoNames.Time_ms) .Report($"{avgIterations}", BenchInfoNames.AvgIterations) .Report($"{medianIterations}", BenchInfoNames.MedianIterations) .Report($"{avgSpins}", BenchInfoNames.Spins) .Report($"{avgCPU:0}", BenchInfoNames.AvgCPU) .Report($"{medianCPU:0}", BenchInfoNames.MedianCPU) .Report($"{avgThroughput:0.000}", BenchInfoNames.AvgThroughput_ms) .Report($"{medianThroughput:0.000}", BenchInfoNames.MedianThroughput_ms) .ToString() .WriteLine() ; }
GeneralBenchmark <TSyncPrimitive> (IPerfCounterCollectorUC perfColletor, string name, int items, Func <int, TSyncPrimitive> createSyncPrimitive, Func <int, IPerfCounterCollectorUC, TSyncPrimitive, BenchInstance[]> createBenchInstance) { items = Math.Max(items, 1); ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; BenchInstance[] workers = Enumerable .Range(0, items) .Select(createSyncPrimitive) .Select((sync, i) => createBenchInstance(i, perfColletor, sync)) .SelectMany(xws => xws) .ToArray() ; List <Task> tasks = new List <Task>(); for (int i = 0; i < workers.Length; ++i) { tasks.Add(workers[i].RunBenchProcessing()); } while (perfColletor.TryClear() != true) { } while (perfColletor.TryStart() != true) { } await Task.WhenAll(tasks); while (perfColletor.TryStop() != true) { } workers .Select(w => ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(name, BenchInfoNames.Name) .Report(w.Pair, BenchInfoNames.ThreadPair) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{w.ElapsedMilliseconds}", BenchInfoNames.Time_ms) .Report($"{w.Iterations}", BenchInfoNames.Iterations) .Report($"{w.Spin}", BenchInfoNames.Spins) .Report($"{w.ThroughputPerMillisecond:0.000}", BenchInfoNames.Throughput_ms) .ToString()) .Aggregate(string.Empty, (c, n) => string.IsNullOrEmpty(c) ? n : $"{c}{Environment.NewLine}{n}") .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; long totalSpins = workers.Select(w => (long)w.Spin).Aggregate(0L, (c, n) => c + n); int avgSpins = (int)(totalSpins / Math.Max(workers.Length, 1)); long totalTime = workers.Select(w => w.ElapsedMilliseconds).Aggregate(0L, (c, n) => c + n); long avgTime = totalTime / Math.Max(workers.Length, 1); //long medianTime = (long)workers.Select(w => (double) w.ElapsedMilliseconds).Median(); long totalIterations = workers.Select(w => (long)w.Iterations).Aggregate(0L, (c, n) => c + n); long avgIterations = totalIterations / Math.Max(workers.Length, 1); long medianIterations = (long)workers.Select(w => (double)w.Iterations).Median(); double totalCPU = perfColletor.Select(cpu => cpu).Aggregate(0.0, (c, n) => c + n); double avgCPU = totalCPU / Math.Max(perfColletor.Count(), 1); double medianCPU = perfColletor.Median(); double totalThroughput = workers.Select(w => w.ThroughputPerMillisecond).Aggregate(0.0, (c, n) => c + n); double avgThroughput = totalThroughput / Math.Max(workers.Length, 1); double medianThroughput = workers.Select(w => w.ThroughputPerMillisecond).Median(); perfColletor .Select(cpu => ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(name, BenchInfoNames.Name) .Report($"{cpu}", BenchInfoNames.CPU) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{avgTime}", BenchInfoNames.Time_ms) .Report($"{avgSpins}", BenchInfoNames.Spins) .Report($"{avgIterations}", BenchInfoNames.AvgIterations) .ToString()) .Aggregate((c, n) => $"{c}{Environment.NewLine}{n}") .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .NamesAsValues() .ToString() .WriteLine() ; ReportUC .New <BenchInfoNames>(ReportTypeUC.CVS) .Report(name, BenchInfoNames.Name) .Report($"{workers.Length}", BenchInfoNames.Threads) .Report($"{totalIterations}", BenchInfoNames.TotalIterations) .Report($"{avgTime}", BenchInfoNames.Time_ms) .Report($"{avgIterations}", BenchInfoNames.AvgIterations) .Report($"{medianIterations}", BenchInfoNames.MedianIterations) .Report($"{avgSpins}", BenchInfoNames.Spins) .Report($"{avgCPU:0}", BenchInfoNames.AvgCPU) .Report($"{medianCPU:0}", BenchInfoNames.MedianCPU) .Report($"{avgThroughput:0.000}", BenchInfoNames.AvgThroughput_ms) .Report($"{medianThroughput:0.000}", BenchInfoNames.MedianThroughput_ms) .ToString() .WriteLine() ; }