473,382 Members | 1,733 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,382 software developers and data experts.

UDP performance

I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).

I must be doing something wrong and would really appreciate feedback on
my code below:

I open the server port with

self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((myaddress, myport))

I then open a client port with

self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect((host, port))

the client sends data with

self.s.sendall(data)

and the server with

self.s.sendto(data,link.remoteaddress)

both receive with

buf, address = socket.recvfrom(8192)

The sender and receiver are in separate threads (threading.Thread).

Does anyone know what is going wrong here, the socket communication, the
thread scheduling?

Paul Sijben

Apr 20 '06 #1
11 4968

Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).
[snip]
buf, address = socket.recvfrom(8192)


I'm not an expert here, but I AFAIK UDP packet size should be kept
below 512 bytes otherwise it can cause fragmentation and hence all the
slow stuff like acknoledgements, timeouts, etc...

Apr 20 '06 #2
Serge Orlov wrote:
Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).


[snip]
buf, address = socket.recvfrom(8192)


I'm not an expert here, but I AFAIK UDP packet size should be kept
below 512 bytes otherwise it can cause fragmentation and hence all the
slow stuff like acknoledgements, timeouts, etc...

good point in general but in practice they are in this case. but this is
less of a problem on the loopback interface anyway.
Apr 20 '06 #3

Paul Sijben wrote:
Serge Orlov wrote:
Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).


[snip]
buf, address = socket.recvfrom(8192)


I'm not an expert here, but I AFAIK UDP packet size should be kept
below 512 bytes otherwise it can cause fragmentation and hence all the
slow stuff like acknoledgements, timeouts, etc...

good point in general but in practice they are in this case. but this is
less of a problem on the loopback interface anyway.


Isn't it still controlled by MTU even on the loopback? What is your MTU
on the loopback?

Apr 20 '06 #4
Serge Orlov wrote:
Paul Sijben wrote:
Serge Orlov wrote:
Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).
[snip]

buf, address = socket.recvfrom(8192)
I'm not an expert here, but I AFAIK UDP packet size should be kept
below 512 bytes otherwise it can cause fragmentation and hence all the
slow stuff like acknoledgements, timeouts, etc...

good point in general but in practice they are in this case. but this is
less of a problem on the loopback interface anyway.


Isn't it still controlled by MTU even on the loopback? What is your MTU
on the loopback?

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1

eth0 Link encap:Ethernet HWaddr 00:14:85:35:A4:5D
inet addr:192.168.0.124 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::214:85ff:fe35:a45d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Apr 20 '06 #5
Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).

I must be doing something wrong and would really appreciate feedback on
my code below:

I open the server port with

self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((myaddress, myport))

I then open a client port with

self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect((host, port))

the client sends data with

self.s.sendall(data)

and the server with

self.s.sendto(data,link.remoteaddress)

both receive with

buf, address = socket.recvfrom(8192)

The sender and receiver are in separate threads (threading.Thread).

Does anyone know what is going wrong here, the socket communication, the
thread scheduling?

Paul Sijben


Is the connection 1:1 i.e. the receiving end receives data only from one
sender at the time? And how do you handle lost packages in your application?

--
mph
Apr 20 '06 #6
Martin P. Hellwig wrote:
[snip]
Is the connection 1:1 i.e. the receiving end receives data only from one
sender at the time? And how do you handle lost packages in your
application?


no the server is receiving from multiple clients.

and at the moment I do not handle lost packets.
Apr 20 '06 #7
OK the problem I posted about earlier is NOT a UDP/socket problem, it is
a threading problem. Albeit one that only happens when you have many
thrreads????

I have made two little scripts using the code I copied below, one client
and one server (attached).

If I run them concurrently on the same machine I see the following times:
1145526192.825508 <<1145526192.825848
1145526193.829325 <<1145526193.834927

a transfer time in the milliseconds. Much less than the times I see in
the full application.

OK so I put them both in a multithreaded script (also attached)

1145526971.619558 <<1145526971.619909
1145526972.619241 <<1145526972.619647

again transfer time in milliseconds.

Not like this that I get from the profile of my code:
<< 1145517554.363850 send 1145517554.647485 recv

which uses the same communication and threading but has 20+ threads?

Now I am completely baffled!

again I really appreciate if anyone can shed light on this!

Paul
Paul Sijben wrote: I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).

I must be doing something wrong and would really appreciate feedback on
my code below:

I open the server port with

self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((myaddress, myport))

I then open a client port with

self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect((host, port))

the client sends data with

self.s.sendall(data)

and the server with

self.s.sendto(data,link.remoteaddress)

both receive with

buf, address = socket.recvfrom(8192)

The sender and receiver are in separate threads (threading.Thread).

Does anyone know what is going wrong here, the socket communication, the
thread scheduling?

Paul Sijben


import socket
import time

profile=[]

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('127.0.0.1', 42420))
print ">>%f"%time.time()
s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
s.sendall('testerdetest')
print ">>%f"%time.time()
import socket
import time

profile=[]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 42420))
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address

import socket
import time
import threading
profile=[]

class server(threading.Thread):
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(('', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address

class client(threading.Thread):
def __init__(self):
self.s =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect(('127.0.0.1', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
print ">>%f"%time.time()
self.s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
self.s.sendall('testerdetest')
print ">>%f"%time.time()
serv=server()
cl=client()

Apr 20 '06 #8
I found that the problem was caused by the sending thread not giving
control back quickly enough to the receiving thread.

Also in going through the code I found an old self.s.setblocking(0)call
that was no longer relevant. Removing that solved my problem.

Something that took 20 seconds now takes just 1.

Thanks to those who took the time to respond to my earlier messages.

Paul

Paul Sijben wrote:
OK the problem I posted about earlier is NOT a UDP/socket problem, it is
a threading problem. Albeit one that only happens when you have many
thrreads????

I have made two little scripts using the code I copied below, one client
and one server (attached).

If I run them concurrently on the same machine I see the following times:
1145526192.825508 <<1145526192.825848
1145526193.829325 <<1145526193.834927

a transfer time in the milliseconds. Much less than the times I see in
the full application.

OK so I put them both in a multithreaded script (also attached)

1145526971.619558 <<1145526971.619909
1145526972.619241 <<1145526972.619647

again transfer time in milliseconds.

Not like this that I get from the profile of my code:
<< 1145517554.363850 send 1145517554.647485 recv


which uses the same communication and threading but has 20+ threads?

Now I am completely baffled!

again I really appreciate if anyone can shed light on this!

Paul
Paul Sijben wrote:
I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).

I must be doing something wrong and would really appreciate feedback on
my code below:

I open the server port with

self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((myaddress, myport))

I then open a client port with

self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect((host, port))

the client sends data with

self.s.sendall(data)

and the server with

self.s.sendto(data,link.remoteaddress)

both receive with

buf, address = socket.recvfrom(8192)

The sender and receiver are in separate threads (threading.Thread).

Does anyone know what is going wrong here, the socket communication, the
thread scheduling?

Paul Sijben


------------------------------------------------------------------------

import socket
import time

profile=[]

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('127.0.0.1', 42420))
print ">>%f"%time.time()
s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
s.sendall('testerdetest')
print ">>%f"%time.time()

------------------------------------------------------------------------

import socket
import time

profile=[]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 42420))
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
------------------------------------------------------------------------

import socket
import time
import threading
profile=[]

class server(threading.Thread):
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(('', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address

class client(threading.Thread):
def __init__(self):
self.s =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect(('127.0.0.1', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
print ">>%f"%time.time()
self.s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
self.s.sendall('testerdetest')
print ">>%f"%time.time()
serv=server()
cl=client()

Apr 20 '06 #9
OK the problem I posted about earlier is NOT a UDP/socket problem, it is
a threading problem. Albeit one that only happens when you have many
thrreads????

I have made two little scripts using the code I copied below, one client
and one server (attached).

If I run them concurrently on the same machine I see the following times:
1145526192.825508 <<1145526192.825848
1145526193.829325 <<1145526193.834927

a transfer time in the milliseconds. Much less than the times I see in
the full application.

OK so I put them both in a multithreaded script (also attached)

1145526971.619558 <<1145526971.619909
1145526972.619241 <<1145526972.619647

again transfer time in milliseconds.

Not like this that I get from the profile of my code:
<< 1145517554.363850 send 1145517554.647485 recv

which uses the same communication and threading but has 20+ threads?

Now I am completely baffled!

again I really appreciate if anyone can shed light on this!

Paul
Paul Sijben wrote: I am stumped by the following problem. I have a large multi-threaded
server accepting communications on one UDP port (chosen for its supposed
speed).

I have been profiling the code and found that the UDP communication is
my biggest drain on performance! Communication where the client and the
server are on the same machine still takes 300ms or sometimes much more
per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64).

I must be doing something wrong and would really appreciate feedback on
my code below:

I open the server port with

self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((myaddress, myport))

I then open a client port with

self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect((host, port))

the client sends data with

self.s.sendall(data)

and the server with

self.s.sendto(data,link.remoteaddress)

both receive with

buf, address = socket.recvfrom(8192)

The sender and receiver are in separate threads (threading.Thread).

Does anyone know what is going wrong here, the socket communication, the
thread scheduling?

Paul Sijben

import socket
import time

profile=[]

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('127.0.0.1', 42420))
print ">>%f"%time.time()
s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
s.sendall('testerdetest')
print ">>%f"%time.time()

import socket
import time

profile=[]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 42420))
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
import socket
import time
import threading
profile=[]

class server(threading.Thread):
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(('', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address
buf, address =self.s.recvfrom(8192)
print "<<%f"%time.time()
print buf, address

class client(threading.Thread):
def __init__(self):
self.s =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.connect(('127.0.0.1', 42420))
threading.Thread.__init__(self)
self.start()

def run(self):
print ">>%f"%time.time()
self.s.sendall('testerdetest')
time.sleep(1)
print ">>%f"%time.time()
self.s.sendall('testerdetest')
print ">>%f"%time.time()
serv=server()
cl=client()
Apr 20 '06 #10
In article <44***********************@news.xs4all.nl>,
Paul Sijben <si****@eemvalley.com> wrote:
I found that the problem was caused by the sending thread not giving
control back quickly enough to the receiving thread.

Also in going through the code I found an old self.s.setblocking(0)call
that was no longer relevant. Removing that solved my problem.

Something that took 20 seconds now takes just 1.


You might also find that it goes still faster if you forego threading
and use a select.select loop.
Apr 22 '06 #11
Lawrence D'Oliveiro wrote:
In article <44***********************@news.xs4all.nl>,
Paul Sijben <si****@eemvalley.com> wrote:
I found that the problem was caused by the sending thread not giving
control back quickly enough to the receiving thread.

Also in going through the code I found an old self.s.setblocking(0)call
that was no longer relevant. Removing that solved my problem.

Something that took 20 seconds now takes just 1.


You might also find that it goes still faster if you forego threading
and use a select.select loop.


thanks for that. I will have to try it.
Apr 25 '06 #12

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

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
12
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
5
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used the Upsizing Wizard to move the tables to SQL...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
1
by: jvn | last post by:
I am experiencing a particular problem with performance counters. I have created a set of classes, that uses System.Diagnostics.PerformanceCounter to increment custom performance counters (using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.