Esempio n. 1
0
        public void TestThreadPoolPerformance()
        {
            const int numberOfOperations = 100000;
            var       ssw = new SplitStopwatch();

            // Test without any threadPool.
            ssw.Start("SINGLE THREADED WITHOUT POOL:");
            for (var i = 0; i < numberOfOperations; i++)
            {
                CalcAverage(GetNumbersForAverage());
            }
            ssw.Stop("Done.", 1);
            Console.Out.WriteLine(string.Empty);
            ssw.Reset();

            for (var numberOfWorkerThreads = 1;
                 numberOfWorkerThreads < Environment.ProcessorCount * 2;
                 numberOfWorkerThreads++)
            {
                ssw.Start("THREADPOOL (" + numberOfWorkerThreads + " workerThreads):");
                threadPool = new ThreadPool(numberOfWorkerThreads, "testWorker");
                ssw.Split("Starting to enqueue.", 1);

                for (var i = 0; i < numberOfOperations; i++)
                {
                    threadPool.EnqueueWorkItem(CalcAverage, GetNumbersForAverage());
                }

                ssw.Split("All items are enqueued.", 1);
                threadPool.WaitForEveryWorkerIdle();
                threadPool.ShutDown();

                ssw.Stop("Done.", 1);
                Console.Out.WriteLine(string.Empty);
                ssw.Reset();
            }
        }
Esempio n. 2
0
        public void TestThreadPool()
        {
            threadPool = new ThreadPool(Environment.ProcessorCount, "testWorker");

            var ssw = new SplitStopwatch();

            ssw.Start("starting to enqueue...");

            var wir1  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir2  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 1, 1, 1, 1 });
            var wir3  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir4  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir5  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir6  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir7  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir8  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir9  = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });
            var wir10 = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });

            ssw.Split("all items are enqueued...");

            var average = wir1.Result;

            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 1...");
            wir1.Dispose();

            average = wir2.Result;
            Assert.AreEqual(average, 1.0);
            ssw.Split("we waited for result 2...");
            wir2.Dispose();

            average = wir3.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 3...");
            wir3.Dispose();

            average = wir4.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 4...");
            wir4.Dispose();

            average = wir5.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 5...");
            wir5.Dispose();

            average = wir6.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 6...");
            wir6.Dispose();

            average = wir7.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 7...");
            wir7.Dispose();

            average = wir8.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 8...");
            wir8.Dispose();

            average = wir9.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 9...");
            wir9.Dispose();

            average = wir10.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 10...");
            wir10.Dispose();

            var wir11 = threadPool.EnqueueWorkItem(CalcAverage, new[] { 2, 3, 2, 5 });

            average = wir11.Result;
            Assert.AreEqual(average, 3.0);
            ssw.Split("we waited for result 11...");
            wir11.Dispose();

            threadPool.ShutDown();
        }