Esempio n. 1
0
        public TestRunner(TestRunnerOptions options)
        {
            _options = options;

            _options.CancellationToken.Register(() => {
                _timer?.Stop();
            });

            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            _pathToSpeedtestCli = config.AppSettings.Settings["SpeedtestPath"].Value;

            _silent = options.Silent;

            if (_options.Frequency != default)
            {
                _timer           = new System.Timers.Timer(_options.Frequency.TotalMilliseconds);
                _timer.AutoReset = true;
                _timer.Elapsed  += async(_, __) =>
                {
                    if (_options.CancellationToken.IsCancellationRequested || (_options.MaxRuns > 0 && ++_runCounter > _options.MaxRuns))
                    {
                        _timer.Stop();
                        TestsComplete?.Invoke(this, EventArgs.Empty);
                    }

                    await Execute().ConfigureAwait(false);
                };
            }

            if (_options.ResultRepository != null && !_options.CancellationToken.IsCancellationRequested)
            {
                Execute = async() =>
                {
                    var model = await RunSpeedtest().ConfigureAwait(false);

                    if (model is null)
                    {
                        return;
                    }

                    await _options.ResultRepository.AddResult(model).ConfigureAwait(false);

                    if (!_silent)
                    {
                        Console.WriteLine("Results saved.");
                    }
                };
            }
            else
            {
                Execute = async() => await RunSpeedtest().ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            var cancelTokenSource = new CancellationTokenSource();

            Console.CancelKeyPress += (o, e) =>
            {
                Console.WriteLine("Cancelling...");
                cancelTokenSource.Cancel();
            };

            var parsedArgs = new CommandLineArguments();

            CommandLineParser.ParseArguments(parsedArgs, args);

            if (parsedArgs.Help == true)
            {
                Console.WriteLine(HelpInfo);
                return;
            }

            var options = new TestRunnerOptions();

            options.Silent            = parsedArgs.Silent == true ? true : false;
            options.HideResults       = parsedArgs.HideResults == true ? true : false;
            options.ResultRepository  = parsedArgs.PersistResults == true ? new Database.ResultRepository() : null;
            options.CancellationToken = cancelTokenSource.Token;

            string intervalStr = "";

            if (parsedArgs.Frequency != null)
            {
                var components = parsedArgs.Frequency.Split(':');
                if (components.Count() != 3)
                {
                    Console.WriteLine("the Frequency argument must be of the form HH:MM:SS, such as /Frequency=3:15:00 to repeat every 3 hours 15 minutes.");
                    return;
                }
                try
                {
                    var frequencySpecs = components.Select(n => int.Parse(n)).ToArray();
                    if (frequencySpecs.Any(i => i < 0))
                    {
                        Console.WriteLine("Only positive integers supported within Frequency argument");
                        return;
                    }
                    else if (frequencySpecs[0] > 23 || frequencySpecs[1] > 59 || frequencySpecs[2] > 59)
                    {
                        Console.WriteLine("Invalid time arguments. Max values allowed: 23:59:59");
                        return;
                    }

                    options.Frequency = new TimeSpan(frequencySpecs[0], frequencySpecs[1], frequencySpecs[2]);
                }
                catch
                {
                    Console.WriteLine("Something went wrong interpreting the Frequency argument. Please see the /help.");
                    return;
                }
                intervalStr = $" Speed tests will be run at intervals of {options.Frequency.ToString("h'h 'm'm 's's'")}{(options.MaxRuns > 0 ? $" a total of {options.MaxRuns} times" : " until you terminate this program")}.";
            }

            if (parsedArgs.MaxRuns != null)
            {
                if (parsedArgs.MaxRuns < 0)
                {
                    Console.WriteLine("Max Runs argument must be greater than 0");
                    return;
                }
                options.MaxRuns = (int)parsedArgs.MaxRuns;
            }

            var resultsStr = $" Results will {(options.ResultRepository is null ? "not " : "")}be saved to the database.";
            var displayStr = $" Results will {(options.HideResults ? "not " : "")}be displayed.";
            var quietStr   = $" Status messages will {(options.Silent ? "not " : "")}be displayed.";

            Console.WriteLine($"Welcome." + intervalStr + resultsStr + displayStr + quietStr);

            using (var testRunner = new TestRunner(options))
                await testRunner.Begin();

            Console.WriteLine("All tests complete. Press any key to exit...");
            Console.ReadKey();
        }