Exemple #1
0
        /// <summary>
        /// Executes the task
        /// </summary>
        /// <param name="task">The task to run</param>
        /// <param name="settings">The settings for the profiler</param>
        /// <returns>The resulting collection of the executions</returns>
        public override IProfilerResult Execute(ITask task, ProfilerSettings settings)
        {
            lock (_threads)
            {
                for (int i = 0; i < _threadCount; i++)
                {
                    var thread = ThreadHelper.QueueTask(i, threadIndex =>
                    {
                        var worker = new Worker();
                        var p      = worker.Run(task, settings);
                        return(p);
                    });

                    System.Diagnostics.Trace.WriteLine($"MeasureMap - Start thread {thread.Id}");

                    _threads.Add(thread);
                }

                foreach (var thread in _threads)
                {
                    thread.Start();
                }
            }

            while (CountOpenThreads() > 0)
            {
                System.Threading.Tasks.Task.WaitAll(GetAwaitableThreads(), -1, CancellationToken.None);
            }

            var results = _threads.Select(s => s.Result);

            var collectîon = new ProfilerResult();

            foreach (var result in results)
            {
                collectîon.Add(result);
            }

            return(collectîon);
        }
        /// <summary>
        /// Starts the profiling session
        /// </summary>
        /// <returns>The resulting profile</returns>
        public ProfilerResult RunSession()
        {
            if (_task == null)
            {
                throw new ArgumentNullException($"task", $"The Task that has to be processed is null or not set.");
            }

            // warmup
            Trace.WriteLine($"Running Task once for warmup on Performance Analysis Benchmark");
            _task();

            var profile = new ProfilerResult();
            var stopwatch = new Stopwatch();

            SetProcessor();
            SetThreadPriority();
            ForceGarbageCollector();

            Trace.WriteLine($"Running Task for {_iterations} iterations for Perfomance Analysis Benchmark");

            profile.InitialSize = GC.GetTotalMemory(true);

            for (int i = 0; i < _iterations; i++)
            {
                var initial = GC.GetTotalMemory(true);

                stopwatch.Reset();
                stopwatch.Start();

                _task();

                stopwatch.Stop();

                var after = GC.GetTotalMemory(false);
                ForceGarbageCollector();
                var afterCollect = GC.GetTotalMemory(true);

                profile.Add(new ProfileIteration(stopwatch.ElapsedTicks, stopwatch.Elapsed, initial, after, afterCollect));
            }

            ForceGarbageCollector();
            profile.EndSize = GC.GetTotalMemory(true);

            foreach (var condition in _conditions)
            {
                if (!condition(profile))
                {
                    throw new AssertionException($"Condition failed: {condition}");
                }
            }

            Trace.WriteLine($"Running Task for {_iterations} iterations with an Average of { profile.AverageMilliseconds} Milliseconds");

            return profile;
        }