예제 #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();
            }
        }