473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Connecting using socket class to server: Scalability/Performance

Hello
I am building a C# app that creates anywhere from 10 to 100
connections to a specified server and sends 1000s of TCP requests and
processes the responses. (it is a stress tool)
I planned to create a Socket object for each connection and do a
BeginReceive on each socket to handle the responses. (it is clean and
I can avoid the hassles of managing my own threads)

My question is, will this scale well? Or is there a more efficient way
to achieve this? (like having a single dedicated thread reading from
all socket streams and queueing the data to be processed by another
thread)

my code:

for(int i = 0; i < numberOfConnections; i++) {
Socket s = new Socket(.....);
s.Connect(serverEndpoint);
GetResponse(s);
}

GetResponse(Socket s) {
s.BeginReceive(...new AsyncCallback(OnReceived)...);
}

OnReceived(IAsyncResult ar) {
s.EndReceive();
GetResponse(s);
}

Send() {
<randomly chosen so\cket>.Send(...);
}

Thanks
Bruce

Oct 28 '07 #1
5 1612
In article
<11**********************@e34g2000pro.googlegroups .com>Bruce
<br*************@gmail.comwrote:
Hello
I am building a C# app that creates anywhere from 10 to 100
connections to a specified server and sends 1000s of TCP requests
andprocesses the responses. (it is a stress tool)
I planned to create a Socket object for each connection and do a
BeginReceive on each socket to handle the responses. (it is clean
andI can avoid the hassles of managing my own threads)
My question is, will this scale well?
It should scale very well. The async Socket methods use IOCP when
supported, which is the most scalable i/o technique for sockets.
Chris Mullins has a great write-up on his observations and experience
using this method for his high-volume server code; if i recall
correctly, in that implementation hundreds of thousands of clients are
served with high efficiency.

I'd provide the link, but I don't have it handy. You may be able to
find it by searching the newsgroup, or he may see this thread and post
it himself. In any case, the basic technique of using the async
methods is a good one.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo

Oct 28 '07 #2
On Oct 28, 11:29 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
In article
<1193556307.465977.192...@e34g2000pro.googlegroups .com>Bruce

<bruce.james....@gmail.comwrote:
Hello
I am building a C# app that creates anywhere from 10 to 100
connections to a specified server and sends 1000s of TCP requests
andprocesses the responses. (it is a stress tool)
I planned to create a Socket object for each connection and do a
BeginReceive on each socket to handle the responses. (it is clean
andI can avoid the hassles of managing my own threads)
My question is, will this scale well?

It should scale very well. The async Socket methods use IOCP when
supported, which is the most scalable i/o technique for sockets.
Chris Mullins has a great write-up on his observations and experience
using this method for his high-volume server code; if i recall
correctly, in that implementation hundreds of thousands of clients are
served with high efficiency.

I'd provide the link, but I don't have it handy. You may be able to
find it by searching the newsgroup, or he may see this thread and post
it himself. In any case, the basic technique of using the async
methods is a good one.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it athttp://www.malcom-mac.com/nemo
<I posted this before,I don't know why it has not appeared after more
than 3 hours.>

I think I got Chris Dullins's thread ->
http://groups.google.com/group/micro...9268bf8635ea65

My main concern with this async socket approach is this - When I have
a large number of connections and a large number of receives/sends
happening, everytime an async receive/send is done, .NET has to find a
free thread from the threadpool to do the job for every asyncCallback.
at high rates, there should be a large number of context switches
across many threads (though the threads themselves do not do strenuous
jobs). Won't this lead to higher CPU and thereby be less efficient?
(In contrast, using a dedicated thread does not have this issue)

Thanks
Bruce

Oct 29 '07 #3
On 2007-10-28 18:57:16 -0700, Bruce <br*************@gmail.comsaid:
I think I got Chris Dullins's thread ->
http://groups.google.com/group/micro...9268bf8635ea65
That's
>
not it. (And it's Chris Mullins). The post I'm thinking of was some
time ago (more than a year) and contained a link to his write-up.
My main concern with this async socket approach is this - When I have
a large number of connections and a large number of receives/sends
happening, everytime an async receive/send is done, .NET has to find a
free thread from the threadpool to do the job for every asyncCallback.
at high rates, there should be a large number of context switches
across many threads (though the threads themselves do not do strenuous
jobs). Won't this lead to higher CPU and thereby be less efficient?
(In contrast, using a dedicated thread does not have this issue)
No. The whole point of IOCP is to deal with exactly that issue. The
underlying network i/o driver signals via the completion port that some
i/o has finished. Some specific number of threads are assigned to
dequeue i/o completions via the completion port. Completed i/o
operations are preferentially handled by a thread that's already
running, avoiding context switches if possible.

This number of threads is chosen to balance the needs of
responsiveness, the number of available CPUs and other factors (for
example, other i/o bandwidth issues to ensure the threads don't compete
with each other for disk i/o, etc.) On the whole, however, having some
relatively small number of threads, slightly more than the number of
CPUs, is a nice number.

One of the biggest problems with having a single thread handling all of
the i/o is, how to do decide which socket the i/o happened on? IOCP is
the way to most efficiently manage this, as well as the threading
issues, and that's exactly how the async methods on the Socket class
are implemented.

There's a reason they use IOCP for those methods. It wasn't just an
arbitrary decision.

Pete

Oct 29 '07 #4
On Oct 28, 7:10 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-10-28 18:57:16 -0700, Bruce <bruce.james....@gmail.comsaid:
I think I got Chris Dullins's thread ->
http://groups.google.com/group/micro...languages.csha...

That's

not it. (And it's Chris Mullins). The post I'm thinking of was some
time ago (more than a year) and contained a link to his write-up.
My main concern with this async socket approach is this - When I have
a large number of connections and a large number of receives/sends
happening, everytime an async receive/send is done, .NET has to find a
free thread from the threadpool to do the job for every asyncCallback.
at high rates, there should be a large number of context switches
across many threads (though the threads themselves do not do strenuous
jobs). Won't this lead to higher CPU and thereby be less efficient?
(In contrast, using a dedicated thread does not have this issue)

No. The whole point of IOCP is to deal with exactly that issue. The
underlying network i/o driver signals via the completion port that some
i/o has finished. Some specific number of threads are assigned to
dequeue i/o completions via the completion port. Completed i/o
operations are preferentially handled by a thread that's already
running, avoiding context switches if possible.

This number of threads is chosen to balance the needs of
responsiveness, the number of available CPUs and other factors (for
example, other i/o bandwidth issues to ensure the threads don't compete
with each other for disk i/o, etc.) On the whole, however, having some
relatively small number of threads, slightly more than the number of
CPUs, is a nice number.

One of the biggest problems with having a single thread handling all of
the i/o is, how to do decide which socket the i/o happened on? IOCP is
the way to most efficiently manage this, as well as the threading
issues, and that's exactly how the async methods on the Socket class
are implemented.

There's a reason they use IOCP for those methods. It wasn't just an
arbitrary decision.

Pete
That was a lucid explanation. Thanks Pete. I think I should read up
more on IOCP and how they exactly work. With a single thread, I
thought of looping through all the sockets to read from the network
stream, but this definitely seems like a cleaner and easier approach.
My only concern was performance, but as you mentioned, I guess it is
not a hit after all. I will incorporate this in my tool and hope it
scales well!

Bruce

Oct 29 '07 #5
On Oct 28, 11:29 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
In article
<1193556307.465977.192...@e34g2000pro.googlegroups .com>Bruce

<bruce.james....@gmail.comwrote:
Hello
I am building a C# app that creates anywhere from 10 to 100
connections to a specified server and sends 1000s of TCP requests
andprocesses the responses. (it is a stress tool)
I planned to create a Socket object for each connection and do a
BeginReceive on each socket to handle the responses. (it is clean
andI can avoid the hassles of managing my own threads)
My question is, will this scale well?

It should scale very well. The async Socket methods use IOCP when
supported, which is the most scalable i/o technique for sockets.
Chris Mullins has a great write-up on his observations and experience
using this method for his high-volume server code; if i recall
correctly, in that implementation hundreds of thousands of clients are
served with high efficiency.

I'd provide the link, but I don't have it handy. You may be able to
find it by searching the newsgroup, or he may see this thread and post
it himself. In any case, the basic technique of using the async
methods is a good one.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it athttp://www.malcom-mac.com/nemo
Thanks for the response. I think i found Chris Mullins's link (this
one? http://developers.coversant.net/Down...3/Default.aspx)
My main concern with this async socket style for a lot of connections
is thread contention. For every action, BeginAccept/BeginReceive,
callbacks etc, .NET needs to find a free thread in the threadpool, use
the thread to execute the action, put the thread back in the
threadpool. Given that there are a lot of sends and receives this
should lead to a lot of context switches between the threads (though
the work they do is not strenuous), leading to higher CPU. In
contrast, having a single dedicated thread will prevent this overhead.

Bruce

Oct 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
0
by: christian_stengel | last post by:
Hi *, I have just started to learn python and I am having a problem with an python client connecting to a perl server using ssl (I tried this with pyOpenSSL and with the build in SSL Module). ...
0
by: Usman | last post by:
Hi I'm having problem with a scenarion where I have a server written in C# and client written in VC6++. Here is the server code that i'm using including the Callback function for handling...
10
by: gasfusion | last post by:
For some reason my socket isn't doing anything when i try connect. I have the header file and the class provided below. Everything works except for the socket_connect() function. Also, i do not...
7
by: =?Utf-8?B?TW9iaWxlTWFu?= | last post by:
Hello everyone: I am looking for everyone's thoughts on moving large amounts (actually, not very large, but large enough that I'm throwing exceptions using the default configurations). We're...
0
by: bishnu12 | last post by:
Hi all, There are two questions 1. I am trying to write a programme which can query to a modem get the modem details. i am using normal send() and recv() functions. But when i try to fetch a...
9
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
3
by: Giampaolo Rodola' | last post by:
Hi, I'd like to know if there's a way to determine which is the best buffer size to use when you have to send() and recv() some data over the network. I have an FTP server application which, on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.