reSIProcate/stack  9694
testSelect.cxx
Go to the documentation of this file.
00001 
00002 #include <cassert>
00003 #include <iostream>
00004 
00005 #include "rutil/DnsUtil.hxx"
00006 #include "rutil/Socket.hxx"
00007 #include "rutil/Timer.hxx"
00008 #include "resip/stack/UdpTransport.hxx"
00009 
00010 using namespace resip;
00011 using namespace std;
00012 
00013 
00014 int 
00015 main(int argc, char* argv[])
00016 {
00017    if ( argc != 3 ) 
00018    {
00019       cerr << "Usage: testSelect startPort endPort\n";
00020       exit(1);
00021    }
00022    
00023    int startPort = atoi( argv[1] );
00024    int endPort = atoi( argv[2] );
00025    
00026    cout << "Doing port " << startPort << " to " << endPort << endl;
00027    
00028    int fd[0xFFFF];
00029    
00030  #define NUMCYCLES 50
00031    
00032    UInt64 time1[NUMCYCLES];
00033    UInt64 time2[NUMCYCLES];
00034    UInt64 time3[NUMCYCLES];
00035    
00036    // open the fd 
00037    for( int port=startPort; port<endPort; port++)
00038    {
00039       fd[port] =  socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00040       if( fd[port] ==  INVALID_SOCKET )
00041       {
00042          cerr << "Fail on creating socket for port " << port << endl;
00043          assert(0);
00044       }
00045       
00046       sockaddr_in addr4; 
00047       sockaddr* saddr=0;
00048       int saddrLen = 0;
00049       
00050       memset(&addr4, 0, sizeof(addr4));
00051       addr4.sin_family = AF_INET;
00052       addr4.sin_port = htons(port);
00053       addr4.sin_addr.s_addr = htonl(INADDR_ANY); 
00054 
00055       saddr = reinterpret_cast<sockaddr*>(&addr4);
00056       saddrLen = sizeof(addr4);
00057 
00058       if ( bind( fd[port], saddr, saddrLen) == SOCKET_ERROR )
00059       {
00060          assert(0);
00061       }
00062       
00063       bool ok = makeSocketNonBlocking( fd[port] );
00064       assert(ok);
00065    }
00066    
00067 
00068    for ( int i=0; i<NUMCYCLES; i++ )
00069    {
00070       FdSet fdset;
00071       
00072       time1[i] = Timer::getTimeMicroSec();
00073       // build the fd
00074       for( int port=startPort; port<endPort; port++)
00075       {
00076          fdset.setRead(fd[port]);
00077       }
00078       
00079       time2[i] = Timer::getTimeMicroSec();
00080       // select
00081       int  err = fdset.selectMilliSeconds( 0 );
00082       assert( err != -1 );
00083       
00084       time3[i] = Timer::getTimeMicroSec();
00085       // do the reads
00086       for( int port=startPort; port<endPort; port++)
00087       {
00088          if (fdset.readyToRead(fd[port]))
00089          {
00090             struct sockaddr from;
00091             
00092             const int MaxBufferSize=1024*4;
00093             char buffer[MaxBufferSize];
00094             socklen_t fromLen = sizeof(from);
00095             
00096             int len = recvfrom( fd[port],
00097                                 buffer,
00098                                 MaxBufferSize,
00099                                 0 /*flags */,
00100                                 (struct sockaddr*)&from,
00101                                 &fromLen);
00102             
00103             if ( len == SOCKET_ERROR )
00104             {
00105                int err = errno;
00106                switch (err)
00107                {
00108                   case WSANOTINITIALISED:
00109                      assert(0);
00110                      break;
00111                      
00112                   case EWOULDBLOCK:
00113                      //cerr << " UdpTransport recvfrom got EWOULDBLOCK";
00114                      break;
00115                      
00116                   case 0:
00117                      cerr << " UdpTransport recvfrom got error 0 ";
00118                      break;
00119                      
00120                   default:
00121                      cerr <<"Error receiving, errno="<<err << " " << strerror(err);
00122                      break;
00123                }
00124             }
00125          }
00126          
00127       }
00128    }
00129    
00130    for ( int i=1; i<NUMCYCLES; i++ )
00131    {
00132       cout << time2[i] - time1[i] << " ";
00133       cout << time1[i] - time1[i-1] << " ";
00134       
00135       cout << endl;
00136    }
00137    
00138    return 0;
00139 }
00140 /* ====================================================================
00141  * The Vovida Software License, Version 1.0 
00142  * 
00143  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00144  * 
00145  * Redistribution and use in source and binary forms, with or without
00146  * modification, are permitted provided that the following conditions
00147  * are met:
00148  * 
00149  * 1. Redistributions of source code must retain the above copyright
00150  *    notice, this list of conditions and the following disclaimer.
00151  * 
00152  * 2. Redistributions in binary form must reproduce the above copyright
00153  *    notice, this list of conditions and the following disclaimer in
00154  *    the documentation and/or other materials provided with the
00155  *    distribution.
00156  * 
00157  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00158  *    and "Vovida Open Communication Application Library (VOCAL)" must
00159  *    not be used to endorse or promote products derived from this
00160  *    software without prior written permission. For written
00161  *    permission, please contact vocal@vovida.org.
00162  *
00163  * 4. Products derived from this software may not be called "VOCAL", nor
00164  *    may "VOCAL" appear in their name, without prior written
00165  *    permission of Vovida Networks, Inc.
00166  * 
00167  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00168  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00169  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00170  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00171  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00172  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00173  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00174  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00175  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00176  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00177  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00178  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00179  * DAMAGE.
00180  * 
00181  * ====================================================================
00182  * 
00183  * This software consists of voluntary contributions made by Vovida
00184  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00185  * Inc.  For more information on Vovida Networks, Inc., please see
00186  * <http://www.vovida.org/>.
00187  *
00188  */