Can we create 300,000 threads in a C # application and run it on a PC?

I am trying to imitate a scenario when 300,000 users access the server. Therefore, I am trying to create pseudo clients by repeatedly accessing the server from parallel threads.

But the first hurdle that needs to be cleared is the ability to run 300,000 threads on a PC? Here is the code that I use to see how many maximum threads I can get, and then replace the test function with the actual function:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace CheckThread { class Program { static int count; public static void TestThread(int i) { while (true) { Console.Write("\rThread Executing : {0}", i); Thread.Sleep(500); } } static void Main(string[] args) { count = 0; int limit = 0; if (args.Length != 1) { Console.WriteLine("Usage CheckThread <number of threads>"); return; } else { limit = Convert.ToInt32(args[0]); } Console.WriteLine(); while (count < limit) { ThreadStart newThread = new ThreadStart(delegate { TestThread(count); }); Thread mythread = new Thread(newThread); mythread.Start(); Console.WriteLine("Thread # {0}", count++); } while (true) { Thread.Sleep(30*1000); } } // end of main } // end of CheckThread class } // end of namespace 

Now what I'm trying may be unrealistic, but still, if there is a way out, and you know, then please help me.

+7
source share
6 answers

Each thread will create its own stack and local storage, you look at about 512 thousand stack space per thread on a 32-bit OS, I think that the stack space doubles in a 64-bit OS. Fast backward calculation of the spreadsheet gives us 146.484375 gigabytes of stack space for your customers in 300 thousand pages.

So no, do not create 300k streams, but rather use threadpool to simulate 300k requests, although tbh, I think, would be better for you with a few test clients sending your server through a network interface.

There are many tools for testing web downloads. Good starting point: http://www.webperformance.com/library/reports/TestingAspDotNet/

+6
source

You can change the maximum nunmber of threads by calling the ThreadPool.SetMaxThreads method. 300,000 threads are likely to cause your computer to explode *

* This is probably an exaggeration

+4
source

Language-agnostic response:

The best way, perhaps, to go about this is to use a Reactor template with a maximum of 1 or 2 parallel threads per core.

+3
source

How .net commits the entire stack (1 MB) for each clR thread; as Ben says, your computer could explode. Or maybe OoM.

+1
source
0
source

Well, what was the result of your test when you tried to create 300K threads? I'm not going to try this on mine!

You could not connect 300K clients at the same time, because there are not enough sockets on one server (therefore, farming).

I did some server testing, and by modifying the registry to make more sockets available, I had 24K sockets connected to the server and all in one window. This was what I expected, since the server-side <> client connection requires one socket object at each end and only 64 KB sockets are available. I did not try to create 24K streams for my testing, I used a client thread class that opened / closed connections on several client socket objects in the list.

Rgds, Martin

0
source

All Articles