Esempio n. 1
0
        internal static bool ParseOptions(Options options)
        {
            if (options.ListBenchmarks)
            {
                Console.WriteLine(string.Join(Environment.NewLine, BenchmarkRunner.GetAvailableBenchmarks()));

                return(false);
            }

            if (options.ListResults)
            {
                ResultSaver.Init(options);
                Console.WriteLine();
                Console.WriteLine(Util.FormatResults(ResultSaver.GetResults()));

                return(false);
            }

            if (options.Upload)
            {
                if (!ResultSaver.Init(options))
                {
                    Console.WriteLine(
                        "A restart or hardware change has been detected. Please redo the benchmark and upload it without restarting.");

                    return(false);
                }

                var result = ResultSaver.IsAllowedToUpload(options);

                if (result == ErrorCode.NOT_WINDOWS)
                {
                    Console.WriteLine("You can only upload your results on a Windows machine.");

                    return(false);
                }

                if (result == ErrorCode.NO_CATEGORY_ALL)
                {
                    Console.WriteLine("You can only upload your results after having completed all benchmarks.");

                    return(false);
                }

                Console.WriteLine();
                Console.WriteLine("Uploading results...");

                var response = ResultSaver.UploadResults().Result;

                if (response != null)
                {
                    Console.WriteLine("Done!");
                    Console.WriteLine("You can view your raw save here: {0}", response.RawPath);
                    Console.WriteLine("You can view your parsed save here: {0}", response.WebsitePath);

                    Console.WriteLine(
                        "Click 'a' to open raw, 'b' to open parsed or any other key to close the program!");

                    while (true)
                    {
                        var key = Console.ReadKey(true);

                        if (key.Key == ConsoleKey.A)
                        {
                            Helper.OpenBrowser(response.RawPath);
                        }
                        else if (key.Key == ConsoleKey.B)
                        {
                            Helper.OpenBrowser(response.WebsitePath);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                Console.WriteLine("Failed uploading results!");

                return(false);
            }

            if (options.Benchmark == null || string.IsNullOrWhiteSpace(options.Benchmark))
            {
                Console.WriteLine("Please specify a benchmark!");

                return(false);
            }

            if (!BenchmarkRunner.GetAvailableBenchmarks()
                .Any(a => a.ToLowerInvariant().Equals(options.Benchmark.ToLowerInvariant())))
            {
                Console.WriteLine("Benchmark name not recognized!");

                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            var options = new Options();

#if RELEASE
            Parser.Default.ParseArguments <Options>(args).WithParsed(opts =>
            {
                options = opts;

                if (opts == null)
                {
                    return;
                }

                if (opts.Threads == 0)
                {
                    if (opts.Multithreaded)
                    {
                        options.Threads = (uint)Environment.ProcessorCount;
                    }
                    else
                    {
                        options.Threads = 1;
                    }
                }
            });

            if (args.Contains("--help") || args.Contains("-h"))
            {
                return;
            }

            if (options.ClearSave)
            {
                ResultSaver.Clear();

                Console.WriteLine("Successfully cleared all saved data!");
                Console.ReadLine();

                return;
            }
#else
            options = new Options {
                Benchmark = "avx2int", Threads = 1, Runs = 1
            };
#endif

            if (!OptionParser.ParseOptions(options))
            {
                Console.ReadLine();

                return;
            }

            Console.WriteLine("Gathering hardware information...");

            var information = MachineInformationGatherer.GatherInformation();

            Console.WriteLine("OS:             {0}", information.OperatingSystem);
            Console.WriteLine("Processor:      {0}", information.Cpu.Name);
            Console.WriteLine("Architecture:   {0}", information.Cpu.Architecture);
            Console.WriteLine("Logical Cores:  {0}", information.Cpu.LogicalCores);
            Console.WriteLine("Physical Cores: {0}", information.Cpu.PhysicalCores);

            Console.WriteLine();

            if (options.Stress)
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var consoleTask             = Task.Run(() =>
                {
                    Thread.CurrentThread.Priority = ThreadPriority.Highest;
                    Console.WriteLine("Press any key to stop the stress test.");
                    Console.ReadKey(true);

                    cancellationTokenSource.Cancel();
                });

                var stressTester = new StressTestRunner(options);
                stressTester.Prepare();
                var completed = stressTester.RunStressTest(cancellationTokenSource.Token);

                Task.WaitAll(consoleTask);

                Console.WriteLine("You've completed {0} benchmark iteration{1}; ~{2} per thread.", completed,
                                  completed == 1 ? "" : "s",
                                  Math.Round(completed / (double)options.Threads, 2));

                return;
            }

            Console.WriteLine("Starting Benchmark...");

            Console.WriteLine();

            ResultSaver.Init(options);

            var runner = new BenchmarkRunner(options, information);

            runner.Prepare();

            var poptions = new ProgressBarOptions
            {
                ForegroundColor      = ConsoleColor.Yellow,
                BackgroundColor      = ConsoleColor.DarkYellow,
                ProgressCharacter    = '─',
                ProgressBarOnBottom  = true,
                CollapseWhenFinished = false
            };

            using (var pbar = new ProgressBar((int)BenchmarkRunner.TotalOverall,
                                              $"Running Benchmark {options.Benchmark} on {options.Threads} threads {options.Runs} times", poptions))
            {
                using var ct = new CancellationTokenSource();
                var t = Task.Run(() =>
                {
                    Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

                    while (!ct.IsCancellationRequested)
                    {
                        lock (BenchmarkRunner.CurrentRunningBenchmark)
                        {
                            pbar.Tick(BenchmarkRunner.FinishedOverall,
                                      $"Overall. Currently running {BenchmarkRunner.CurrentRunningBenchmark} on {options.Threads} threads {options.Runs} times");
                        }

                        Thread.Sleep(200);
                    }
                }, ct.Token);

                try
                {
                    runner.RunBenchmark();
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);

                    return;
                }

                pbar.Tick((int)BenchmarkRunner.TotalOverall);

                ct.Cancel();
                pbar.Tick((int)BenchmarkRunner.TotalOverall);
                t.GetAwaiter().GetResult();
            }

            Console.WriteLine();

            Console.WriteLine(
                Util.FormatResults(new Dictionary <uint, List <Result> > {
                { options.Threads, runner.Results }
            }));

            Console.ReadLine();
        }