Esempio n. 1
0
 public WorkerThread(IProfiler runner, ProfileRequest <TPayload> request)
 {
     m_runner  = runner;
     m_request = request;
     m_thread  = new Thread(new ParameterizedThreadStart(state => DoWork((ProfileRequest <TPayload>)state)))
     {
         Name = runner.Name
     };
 }
Esempio n. 2
0
        public IEnumerable <ProfileResult <TPayload> > Go <TPayload>(ProfileRequest <TPayload> request)
        {
            var payloads = request.Payloads;
            var results  = new List <ProfileResult <TPayload> >(payloads.Length);

            Elapsed = TimeSpan.MinValue;
            using (var scope = request.CreateRunnerScope())
            {
                var runner  = scope.CreateRunner();
                var overall = Stopwatch.StartNew();

                Stopwatch stopwatch;
                TPayload  payload;
                Exception error;
                for (var sample = 0; sample < payloads.Length; sample++)
                {
                    payload   = payloads[sample];
                    stopwatch = Stopwatch.StartNew();

                    try
                    {
                        runner(payload);
                        error = null;
                    }
                    catch (Exception ex)
                    {
                        error = ex;
                    }

                    stopwatch.Stop();
                    var result = new ProfileResult <TPayload>()
                    {
                        Elapsed = stopwatch.Elapsed,
                        Error   = error,
                        Payload = payload
                    };
                    results.Add(result);
                }

                overall.Stop();
                Elapsed = overall.Elapsed;
            }

            return(results.AsReadOnly());
        }
Esempio n. 3
0
        public static ProfileReport <T> Profile <T>(int threads,
                                                    Func <IRunnerScope <T> > runnerScopeFactory,
                                                    Func <IEnumerable <T> > payloadFactoryMethod,
                                                    int samplesCount)
        {
            // Arrange
            var request = new ProfileRequest <T>(runnerScopeFactory)
            {
                Payloads = EnsureSamples(payloadFactoryMethod, samplesCount)
            };
            var profiler = new ProfilerPool(threads);

            // Act
            var results = profiler.Go(request);

            // Assert
            var report = new ProfileReport <T>(profiler.Elapsed, results);

            return(report);
        }
Esempio n. 4
0
        public IEnumerable <ProfileResult <TPayload> > Go <TPayload>(ProfileRequest <TPayload> request)
        {
            var results       = new List <ProfileResult <TPayload> >(request.Payloads.Length * PoolSize);
            var workerThreads = new WorkerThread <TPayload> [PoolSize];
            var random        = new Random(RandomHelper.Seed());

            for (int i = 0; i < PoolSize; i++)
            {
                var runner        = new Profiler(string.Format(CultureInfo.InvariantCulture, "{0}{1}", Name, i));
                var workerRequest = new ProfileRequest <TPayload>(request.RunnerFactory)
                {
                    Payloads = RandomHelper.Shuffle(random, request.Payloads).ToArray()
                };
                workerThreads[i] = new WorkerThread <TPayload>(runner, workerRequest);
            }

            Elapsed = TimeSpan.MinValue;
            var overall = Stopwatch.StartNew();

            EnumerableHelper.ForEach(workerThreads, t => t.Start());
            EnumerableHelper.ForEach(workerThreads, t =>
            {
                if (t.Join(Timeout.Infinite))
                {
                    lock (results)
                    {
                        results.AddRange(t.Results);
                    }
                }
            });

            overall.Stop();
            Elapsed = overall.Elapsed;

            return(results.AsReadOnly());
        }
Esempio n. 5
0
 private void DoWork(ProfileRequest <TPayload> request)
 {
     Results = m_runner.Go(request);
 }