[Python-bugs-list] [ python-Bugs-746895 ] socket.sendto(SOCK_DGRAM) very slow on OSX!

SourceForge.net noreply@sourceforge.net
Sat, 31 May 2003 23:21:59 -0700


Bugs item #746895, was opened at 2003-06-01 02:21
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=746895&group_id=5470

Category: Python Library
Group: Platform-specific
Status: Open
Resolution: None
Priority: 5
Submitted By: Matt Kangas (mkangas)
Assigned to: Nobody/Anonymous (nobody)
Summary: socket.sendto(SOCK_DGRAM) very slow on OSX!

Initial Comment:
I'm trying to send UDP packets using socket.sendto().
With Python 2.2 on Linux, this works like a champ. On
Mac OS X, something's terribly wrong.

Time to send 100 UDP packets using test code + Python
2.2.1:
- Linux 2.4.18 (RedHat 8): 0.009 sec
- MacOS X 10.2.6: > 1 sec, sometimes > 2 sec.

I've tried the following Python builds on OS X, all
with the same results:
- Stock 2.2 build that comes with OS X 10.2
- 2.2.1 provided by Fink
- built-from-scratch 2.2.3: "./configure; make"

provided are sample programs in Python and C.
ktrace/kdump seem to indicate that both programs make
the same sequence of syscalls. the C program runs
blazingly fast on OS X, while the Python one seems to
stall on every call to socket.sendto().

why does socket.sendto() perform so poorly on OS X?

----------------- python sample ----------------
 
#
# UDP socket test: how fast can we write?
# (5/2003 kangas)
#
# time to run with python 2.2.1:
# - Linux 2.4.18:     0.009 sec
# - Mac OS x 10.2.6:  1.272 sec (!!!)

import socket, time, sys

PORT = 9999
DEST_ADDR = ("192.168.1.60", PORT)

def run():
    maxcount = 100
    data = "pingme pingme pingme pingme pingme..."
    dest = DEST_ADDR

    print "opening socket"
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    print "Sending %i packets" % maxcount
    t0 = time.time()
    for i in xrange(0, maxcount):
        s.sendto(data, dest)
    t1 = time.time() - t0
    print "%0.4f secs elapsed" % t1

    s.close()

if __name__=="__main__":
    run()
    

----------------- C sample ----------------

/*
 * UDP socket test: how fast can we write?
 * (5/2003 kangas)
 *
 * Tested on Mac OS X 10.2.6 and Linux 2.4
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

static const int MAXCOUNT = 100;
static const char DATA[] = "pingme pingme pingme pingme
pingme...";
    
int main(void) {
    int s, i, err;
    struct sockaddr_in serverAddr;

    bzero(&serverAddr, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(9999);
    
    inet_pton(AF_INET, "192.168.1.60",
&serverAddr.sin_addr);
    
    printf("opening socket\n");
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
	perror("socket");
	exit(1);
    }

    printf("sending %i packets\n", MAXCOUNT);
    for (i=0; i<MAXCOUNT; i++) {
	err = sendto(s, DATA, strlen(DATA), 0,
		     (struct sockaddr *)&serverAddr, sizeof(serverAddr));
	if (err < 0) {
	    perror("sendto");
	    break;
	}
	printf(".");
     };
    printf("\n");

    printf("closing...\n");
    close(s);
    printf("done!\n");
    return 0;
}


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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=746895&group_id=5470