Пример #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHaveConfigurableJettyThreadPoolSize() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHaveConfigurableJettyThreadPoolSize()
        {
            Jetty9WebServer server               = new Jetty9WebServer(NullLogProvider.Instance, Config.defaults(), NetworkConnectionTracker.NO_OP);
            int             numCores             = 1;
            int             configuredMaxThreads = 12;                                                       // 12 is the new min max Threads value, for one core
            int             acceptorThreads      = 1;                                                        // In this configuration, 1 thread will become an acceptor...
            int             selectorThreads      = 1;                                                        // ... and 1 thread will become a selector...
            int             jobThreads           = configuredMaxThreads - acceptorThreads - selectorThreads; // ... and the rest are job threads

            server.MaxThreads  = numCores;
            server.HttpAddress = new ListenSocketAddress("localhost", 0);
            try
            {
                server.Start();
                QueuedThreadPool threadPool = ( QueuedThreadPool )server.Jetty.ThreadPool;
                threadPool.start();
                System.Threading.CountdownEvent startLatch = new System.Threading.CountdownEvent(jobThreads);
                System.Threading.CountdownEvent endLatch   = LoadThreadPool(threadPool, configuredMaxThreads + 1, startLatch);
                startLatch.await();                         // Wait for threadPool to create threads
                int threads = threadPool.Threads;
                assertEquals("Wrong number of threads in pool", configuredMaxThreads, threads);
                endLatch.Signal();
            }
            finally
            {
                server.Stop();
            }
        }
Пример #2
0
        private static QueuedThreadPool CreateQueuedThreadPool(JettyThreadCalculator jtc)
        {
            BlockingQueue <ThreadStart> queue      = new BlockingArrayQueue <ThreadStart>(jtc.MinThreads, jtc.MinThreads, jtc.MaxCapacity);
            QueuedThreadPool            threadPool = new QueuedThreadPool(jtc.MaxThreads, jtc.MinThreads, JETTY_THREAD_POOL_IDLE_TIMEOUT, queue);

            threadPool.ThreadPoolBudget = null;               // mute warnings about Jetty thread pool size
            return(threadPool);
        }
Пример #3
0
        public void TestQueuedThreadPool()
        {
            //System.Threading.Thread.CurrentThread.Priority = ThreadPriority.Highest;

            QueuedThreadPool tp = new QueuedThreadPool();

            tp.MinThreads      = 5;
            tp.MaxThreads      = 10;
            tp.MaxIdleTimeMs   = 1000;
            tp.SpawnOrShrinkAt = 2;
            tp.ThreadsPriority = ThreadPriority.BelowNormal;

            tp.Start();
            System.Threading.Thread.Sleep(500);


            Assert.AreEqual(5, tp.Threads);
            Assert.AreEqual(5, tp.IdleThreads);

            tp.Dispatch(_job);
            tp.Dispatch(_job);

            Assert.AreEqual(5, tp.Threads);
            //Assert.AreEqual(3,tp.IdleThreads);
            System.Threading.Thread.Sleep(5000);
            Assert.AreEqual(5, tp.Threads);
            Assert.AreEqual(5, tp.IdleThreads);

            for (int i = 0; i < 100; i++)
            {
                tp.Dispatch(_job);
            }

            Assert.Greater(tp.QueueSize, 10);
            Assert.LessOrEqual(tp.IdleThreads, 1);

            System.Threading.Thread.Sleep(2000);

            Assert.AreEqual(0, tp.QueueSize);
            Assert.Greater(tp.IdleThreads, 5);

            int threads = tp.Threads;

            Assert.Greater(threads, 5);
            System.Threading.Thread.Sleep(1500);
            Assert.Less(tp.Threads, threads);
        }
Пример #4
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: private static java.util.concurrent.CountDownLatch loadThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool threadPool, int tasksToSubmit, final java.util.concurrent.CountDownLatch startLatch)
        private static System.Threading.CountdownEvent LoadThreadPool(QueuedThreadPool threadPool, int tasksToSubmit, System.Threading.CountdownEvent startLatch)
        {
            System.Threading.CountdownEvent endLatch = new System.Threading.CountdownEvent(1);
            for (int i = 0; i < tasksToSubmit; i++)
            {
                threadPool.execute(() =>
                {
                    startLatch.Signal();
                    try
                    {
                        endLatch.await();
                    }
                    catch (InterruptedException e)
                    {
                        Console.WriteLine(e.ToString());
                        Console.Write(e.StackTrace);
                    }
                });
            }
            return(endLatch);
        }
Пример #5
0
        public void TestMaxStopTime()
        {
            QueuedThreadPool tp = new QueuedThreadPool();

            tp.MinThreads    = 500;
            tp.MaxThreads    = 1000;
            tp.MaxStopTimeMs = 500;
            tp.Start();

            // dispatch jobs
            for (int i = 0; i < 5000; i++)
            {
                tp.Dispatch(new ThreadStart(
                                () =>
                {
                    //Log.Info("Running job with thread id " + System.Threading.Thread.CurrentThread.Name);
                    while (true)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(1000);
                        }
                        catch (ThreadInterruptedException) {}
                    }
                }
                                ));
            }



            System.Threading.Thread.Sleep(100);
            long beforeStop = (DateTime.UtcNow.Ticks / 1000);

            tp.Stop();
            long afterStop = (DateTime.UtcNow.Ticks / 1000);

            Assert.IsTrue(tp.IsStopped);
            Assert.Less(afterStop - beforeStop, 100000);
        }
Пример #6
0
        static void RunThreadPool()
        {
            QueuedThreadPool tp = new QueuedThreadPool();

            tp.MaxStopTimeMs = 500;
            tp.Start();

            // dispatch jobs
            for (int i = 0; i < 100; i++)
            {
                tp.Dispatch(new ThreadStart(
                                () =>
                {
                    Log.Info("Running job with thread id " + System.Threading.Thread.CurrentThread.Name);
                    //while (true)
                    //{
                    try
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (ThreadInterruptedException) { }
                    //}
                }
                                ));
            }



            System.Threading.Thread.Sleep(1000);
            long beforeStop = (DateTime.UtcNow.Ticks / 1000);

            tp.Stop();
            long afterStop = (DateTime.UtcNow.Ticks / 1000);

            Log.Info("Time to Stop {0}", afterStop - beforeStop);
        }
Пример #7
0
        public void TestStress()
        {
            QueuedThreadPool tp = new QueuedThreadPool();

            tp.MinThreads    = 240;
            tp.MaxThreads    = 250;
            tp.MaxIdleTimeMs = 100;
            tp.Start();

            tp.MinThreads = 90;


            int count = 0;

            Random random = new Random((int)(DateTime.UtcNow.Ticks / 1000));
            int    loops  = 16000;

            try
            {
                for (int i = 0; i < loops;)
                {
                    int burst = random.Next(100);
                    for (int b = 0; b < burst && i < loops; b++)
                    {
                        if (i % 20 == 0)
                        {
                            Console.Error.Write('.');
                        }
                        if (i % 1600 == 1599)
                        {
                            Console.Error.WriteLine();
                        }
                        if (i == 1000)
                        {
                            tp.MinThreads = 10;
                        }

                        if (i == 10000)
                        {
                            tp.MaxThreads = 20;
                        }

                        i++;
                        tp.Dispatch(
                            new ThreadStart(
                                () =>
                        {
                            int s = random.Next(50);
                            try
                            {
                                System.Threading.Thread.Sleep(s);
                            }
                            catch (ThreadInterruptedException e)
                            {
                                Console.WriteLine(e.StackTrace);
                            }
                            finally
                            {
                                Interlocked.Increment(ref count);
                            }
                        }


                                )

                            );
                    }

                    System.Threading.Thread.Sleep(random.Next(100));
                }

                System.Threading.Thread.Sleep(2000);

                tp.Stop();

                Assert.AreEqual(loops, count);
            }
            catch (Exception e)
            {
                //Console.Error.WriteLine(e.StackTrace);
                Assert.IsTrue(false, e.Message);
            }
        }