-3

Original Question
Is there a heuristic or algorithim to programatically find out how many threads i can open in order to obtain maximum throughput of a async operation such as writing on a socket?

Further explained question
I'm assisting a algorithms professor in my college and he posted a assignment where the students are supossed to learn the basics about distributed computing, in his words: Sockets... The assignment is to create a "server" that listens on a given port, receives a string, performs a simple operations on it (i think it's supposed to count it's length) and return Ok or Rejected... The "server" must be able to handle a minimum of 60k submitions per second... My job is to create a little app to simulate 60K clients...

I've managed to automate the distribution of servers and the clients across a university lab in order to test 10 servers at a time (network infrastructure became the bottleneck), the problem here is: A lab is homogeneous, 2 labs are not! If not tunned correctly the "client" usually can't simulate 60k users and report back to me, especially when the lab is a older one, AND i would like to provide the client to the students so they could test their own "server" more reliably... The ability to determine the optimal number of threads to spawn has now become vital! PS: Fire-and-Forget is not a option because the client also tests if the returned value is correct, e.g If i send "Short sentence" i know the result will be "Rejected" and i have to check it...

A class have 60 students... and there's the morning class and the night class, so each week there will be 120 "servers" to test because as the semester moves along the "server" part will have to do more stuff, the client no (it will always only send a string and receive "Ok"/"Rejected")... So there's enough work to be done in order to justify all this work i'm doing...

Edit1
- Changed from Console to a async operation
- I dont want the maximum number of threads, i want the number that will provide maximum throughput! I imagine that on a 6 core pc the number will be higher than on a 2 core pc

Edit2
- I'm building a simple console app to perform some test in another app... one of thouse is a specific kind of load test (RUDY attack) where i have to simulate a lot of clients performing a specific attack... The thing is that there's a curve between throughput and number of threads, where after a given point, opening more threads actually decreases my throughput...

Edit3
Added more context to the initial question...

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • 1
    Do as many as you want.. it won't go any faster. `Console.WriteLine` obtains a lock so that only a single thread can output at once (if I recall correctly). – Simon Whitehead May 27 '14 at 23:07
  • 1
    Why? Are you planning to greet all six billion inhabitants individually and by name? –  May 27 '14 at 23:07
  • possible duplicate of [Maximum number of threads in a .NET app?](http://stackoverflow.com/questions/145312/maximum-number-of-threads-in-a-net-app) – DaveShaw May 27 '14 at 23:07
  • 1
    If all you're doing is that one statement, I'm pretty sure a single-threaded loop is going to be fastest. The console output is the bottleneck. – itsme86 May 27 '14 at 23:07
  • What are you actually trying to accomplish? You could start *very many* threads, but just calling `WriteLine` isn't really accomplishing much work. To actually determine the best number of threads for a particular task, we need to understand more about the algorithms in question. Usually, more than 8 tends to be counter-productive. – Jonathon Reinhart May 27 '14 at 23:07
  • @JonathonReinhart i edited.. check edit 2 – Leonardo May 27 '14 at 23:19
  • @Leonardo Sounds like you need to manually try it and adjust the number of threads to find a maximum throughput. Your app should take the number of threads to use on the command line, so it is easy for you to adjust. Not sure what else you're looking to find here. See also that possible duplicate marked by DaveShaw (comment #3) – Jonathon Reinhart May 28 '14 at 02:15

4 Answers4

3

The Windows console is really meant to be used by more than one thread, otherwise you get interleaved writes. So the thread count for maximum console output would be one.

It's when you're doing computation that multiple threads makes sense. Then, it's rarely useful to use more than one thread per logical processor - or one background thread plus on UI thread for UI apps on a single-core processor.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
David Crowell
  • 3,711
  • 21
  • 28
2

It depends entirely on the situation - so the actual answer to your question of "is there a magical algorithm that will give me the perfect setup for max throughput?" is ... no.

Sure, more cores means more threads that can run and less context-switching. That said, you've edited your question to include an IO-bound example. IO-bound operations generally make use of completion ports for async operations. So, in that particular case, removing your use of your own dedicated threads for such an operation would be your main concern towards achieving maximum throughput.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
1

Since you changed the question, I'll provide another answer.

It depends on the workload. If you're doing compute-heavy tasks, then use every logical processor. If you're doing IO, then use async calls rather than spawning new threads.

Of course, .NET has a way of managing this for you - the Thread Pool. Use it. Don't worry about how many threads you need, just kick off tasks.

David Crowell
  • 3,711
  • 21
  • 28
0

If you are actually trying to do something productive (instead of just printing to the console), you should use System.Threading.Tasks.Task.Factory.StartNew. You can start as many tasks as you want. The runtime will try to distribute them amongst the available hardware threads as well as it can.

Tobias Brandt
  • 3,393
  • 18
  • 28