public Task LoadAndRunBatch(string[] args)
        {
            Load();
            BenchmarkItem[] activeBenchmarks = BenchmarkItem.GetActive(benchmarks, args);
            var             dispatcher       = this.GetRequiredService <IDispatcherService>();
            var             awaiter          = this.GetService <IUIAwaiter>();

            this.batchTotal = activeBenchmarks.Length;
            return(Task.Factory.StartNew(() =>
            {
                int total = 0;
                for (int i = 0; i < activeBenchmarks.Length; i++)
                {
                    this.batchIndex = i + 1;
                    var current = activeBenchmarks[i];
                    total = UIOperation <int>(dispatcher, () =>
                    {
                        ActiveHostItem = HostItems.FirstOrDefault(host => BenchmarkItem.IsBenchmarkFor(current.Type, host.Name));
                        if (ActiveHostItem != null)
                        {
                            ActiveBenchmarkItem = current;
                        }
                        return total + ((ActiveHostItem != null) ? 1 : 0);
                    }).ContinueWith(tSelectItem =>
                    {
                        Run().Wait();
                        return tSelectItem.Result;
                    }).Result;
                }
                this.batchTotal = null;
                this.batchIndex = null;
                dispatcher.BeginInvoke(() =>
                                       OnBatchRunComplete(total));
            }));
        }
        protected virtual void OnActiveHostItemChanged()
        {
            var hostService = this.GetService <IHostService>();

            if (hostService != null)
            {
                hostService.Show(ActiveHostItem.Target);
            }
            //
            if (ActiveHostItem == null)
            {
                BenchmarkItems = new List <BenchmarkItem>();
            }
            else
            {
                BenchmarkItems = benchmarks
                                 .Where(x => BenchmarkItem.IsBenchmarkFor(x.Type, ActiveHostItem.Name))
                                 .ToList();
            }
        }
        public Task WarmUp()
        {
            var  dispatcher    = this.GetRequiredService <IDispatcherService>();
            var  awaiter       = this.GetService <IUIAwaiter>();
            var  target        = ActiveBenchmarkItem.Target;
            var  uiControl     = ActiveHostItem.Target.UIControl;
            int  warmUpCounter = BenchmarkItem.GetWarmUpCounter(ActiveBenchmarkItem.Type);
            Task coldRun       = IsCold ? ColdRun() : Prepare(dispatcher, awaiter);

            return(coldRun.ContinueWith(_ =>
            {
                long result = 0;
                for (int i = -2 /*pre-warmup*/; i < warmUpCounter; i++)
                {
                    if (i == -1)
                    {
                        if (result > 5000)
                        {
                            warmUpCounter = 5;
                        }
                        if (result > 15000)
                        {
                            warmUpCounter = 3;
                        }
                    }
                    if (i == 0)
                    {
                        result = 0;
                    }
                    result += DoCycle(dispatcher, awaiter, target, uiControl).Result;
                }
                warmUpResult = Math.Max(1, result / warmUpCounter);
                dispatcher.BeginInvoke(() =>
                {
                    this.RaisePropertyChanged(x => x.Result);
                    this.RaiseCanExecuteChanged(x => x.Run());
                });
            }));
        }
 public void Load()
 {
     this.types      = typeof(MainViewModel).Assembly.GetTypes();
     this.benchmarks = types
                       .Where(t => t.IsPublic && !t.IsAbstract && !t.IsInterface && BenchmarkItem.Match(t, Benchmarks.Data.Configuration.Current))
                       .Select(x => new BenchmarkItem(x)).ToArray();
     HostItems = types
                 .Where(t => t.IsPublic && !t.IsAbstract && typeof(IBenchmarkHost).IsAssignableFrom(t) && !t.IsInterface)
                 .Select(x => new HostItem(x)).ToList();
 }
        public Task Run()
        {
            running++;
            var  dispatcher = this.GetRequiredService <IDispatcherService>();
            var  awaiter    = this.GetService <IUIAwaiter>();
            Task warmUp     = !IsWarmedUp?WarmUp() : Prepare(dispatcher, awaiter);

            // Prepare
            var target    = ActiveBenchmarkItem.Target;
            var uiControl = ActiveHostItem.Target.UIControl;

            return(warmUp.ContinueWith(_ =>
            {
                int counter = BenchmarkItem.GetBenchmarkCounter(ActiveBenchmarkItem.Type, Math.Max(10, Math.Min(100, 1000 / (int)warmUpResult.Value)));
                long[] results = new long[counter];
                long low = warmUpResult.Value - Math.Min(warmUpResult.Value / 3, Math.Max(5, warmUpResult.Value / 20));
                long hi = warmUpResult.Value + Math.Min(warmUpResult.Value / 3, Math.Max(5, warmUpResult.Value / 20));
                if (low == hi)
                {
                    low--; hi++;
                }
                worst = 0;
                int watchDog = Benchmarks.Data.Configuration.Current.WatchDog(counter);
                runTotal = counter;
                while (counter > 0 && (0 < watchDog--))
                {
                    // Run Normal
                    long current = DoCycle(dispatcher, awaiter, target, uiControl).Result;
                    worst = Math.Max(current, worst.Value);
                    if (current > low && current < hi)
                    {
                        results[--counter] = current;
                        runIndex = runTotal - counter;
                        dispatcher.BeginInvoke(() =>
                                               this.RaisePropertyChanged(x => x.Title));
                    }
                }
                int actualResultsCount = results.Where(r => r != 0).Count();
                // Check results completeness
                if (watchDog <= 0 && (results.Length - counter) >= 0)
                {
                    while (counter > 0)
                    {
                        // Run Ever!
                        long current = DoCycle(dispatcher, awaiter, target, uiControl).Result;
                        worst = Math.Max(current, worst.Value);
                        if (current < warmUpResult.Value * 2)
                        {
                            results[--counter] = current;
                            runIndex = runTotal - counter;
                            dispatcher.BeginInvoke(() =>
                                                   this.RaisePropertyChanged(x => x.Title));
                        }
                    }
                    actualResultsCount = results.Length;
                }
                // Calc
                result = (long)Math.Ceiling((double)results.Sum() / (double)actualResultsCount);
                dispatcher.BeginInvoke(() =>
                {
                    this.runTotal = null;
                    this.runIndex = null;
                    this.RaisePropertyChanged(x => x.Result);
                    running--;
                    UpdateCommands();
                    LogResults();
                    CopyToClipboard();
                });
            }));
        }
        static int GetBenchmarkIndex(BenchmarkItem current, string[] args)
        {
            int typeNameIndex = Array.IndexOf(args, current.Type.FullName.ToLowerInvariant());

            return((typeNameIndex != -1) ? typeNameIndex : Array.IndexOf(args, current.Name.ToLowerInvariant()));
        }
 static bool IsSpecificBenchmark(BenchmarkItem current, string[] args)
 {
     return
         (Array.IndexOf(args, current.Type.FullName.ToLowerInvariant()) != -1 ||
          Array.IndexOf(args, current.Name.ToLowerInvariant()) != -1);
 }