TCP/IP Network Speed Check

Share your advanced PureBasic knowledge/code with the community.
dige
Addict
Addict
Posts: 1409
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

TCP/IP Network Speed Check

Post by dige »

The following example shows how transfer and receive network data.
Start it twice one as server and one as client.

I've got here 60MB/sec. I'm not sure if its good or not.

ReceiveNetworData () is used here very often wrong.
Please have a look at which cases can occur, thats why
network data not yet fully transfered.

I wrote this tool to check the PB network performance.
Because I have some problems to get image data (depth map)
from an OpenNI server (transfer speed is only 2MB/sec).

Code: Select all

; Check TCP/IP Network Speed (Example how to use ReceiveNetworkData())
; 06/2012 by Dige

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #hWnd
EndEnumeration
;}
;{ Gadgets
Enumeration
  #hWnd_R_Client
  #hWnd_R_Server
  #hWnd_IP
  #hWnd_SN_PORT
  #hWnd_TB_Buffer
  #hWnd_S_Speed
  #hWnd_B_Run
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu, Buffersize, *mem, ClientID, ReceivedBytes, SendetBytes
;}
Procedure OpenWindow_hWnd()
  If OpenWindow(#hWnd, 560, 426, 154, 169, "TCP-Speed", #PB_Window_TitleBar|#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
    OptionGadget(#hWnd_R_Client, 10, 10, 65, 25, "Client")
    OptionGadget(#hWnd_R_Server, 80, 10, 65, 25, "Server")
    IPAddressGadget(#hWnd_IP, 10, 40, 135, 25)
    SpinGadget(#hWnd_SN_PORT, 10, 70, 55, 20, 0, 65000, #PB_Spin_Numeric)
    TrackBarGadget(#hWnd_TB_Buffer, 75, 70, 70, 25, 0, 16, #PB_TrackBar_Ticks)    
    StringGadget(#hWnd_S_Speed, 10, 105, 135, 25, "Speed", #PB_String_ReadOnly)
    ButtonGadget(#hWnd_B_Run, 10, 135, 135, 25, "Start", #PB_Button_Toggle)
  EndIf
EndProcedure

OpenWindow_hWnd()
SetGadgetState(#hWnd_R_Client, #TRUE)
SetGadgetState(#hWnd_TB_Buffer, 12)
SetGadgetState(#hWnd_IP, MakeIPAddress(127, 0, 0, 1))
SetGadgetState(#hWnd_SN_PORT, 6001)

If Not InitNetwork()
  DisableGadget(#hWnd_B_Run, #TRUE)
EndIf

#MAX_TCP_BUFFERSIZE = $FFFF

Buffersize = Pow( 2, GetGadgetState(#hWnd_TB_Buffer))
*tcp_mem = AllocateMemory(Buffersize); Max Size for TCP
*rec_mem = AllocateMemory(#MAX_TCP_BUFFERSIZE)     ; Server Memory = Max TCP Buffersize

;{- Event loop
Repeat
  
  If ClientID
    ; Send Data
    If GetGadgetState(#hWnd_R_Client)
      
      If SendNetworkData( ClientID, *tcp_mem, Buffersize ) = Buffersize
        SendetBytes + Buffersize
        If SendetBytes > $1000000 Or ElapsedMilliseconds() - Time > 2000
          SendetBytes / 1024 / 1024
          SetGadgetText( #hWnd_S_Speed, StrF(SendetBytes*1000/(ElapsedMilliseconds() - Time), 2) + "MB/sec (P=" + Str(Count) + ")")
          Time = ElapsedMilliseconds()
          SendetBytes = 0
        EndIf
        Count + 1
      Else
        SetGadgetText( #hWnd_S_Speed, "Send Error" )
      EndIf
      
    ; Receive Data via Server
    Else
      
      SEvent = NetworkServerEvent()
      
      If SEvent
        
        Client = EventClient()
        
        If SEvent = #PB_NetworkEvent_Data
          Time = ElapsedMilliseconds()
          
          BufferFillSize = 0
          ReceivedBytes  = 0
          Count          = 0
          
          Repeat
            Repeat
              ; Load Network Data into *rec_mem with selected Buffersize
              ReceivedBytes = ReceiveNetworkData( Client, *rec_mem + BufferFillSize, Buffersize )
              
              If ReceivedBytes > 0
                
                If BufferFillSize = 0 And ReceivedBytes >= 4
                  ; Get Size of the whole transmission
                  Size = PeekL(*tcp_mem)
                EndIf
                
                BufferFillSize + ReceivedBytes
                
                If Size And BufferFillSize >= Size
                  Break 2
                  
                ElseIf BufferFillSize + Buffersize > #MAX_TCP_BUFFERSIZE
                  ReceivedBytes = -1
                  SetGadgetText( #hWnd_S_Speed, "Memory Error" )
                EndIf
              EndIf
              
            Until ReceivedBytes <> Buffersize
            
            If ReceivedBytes <> -1
              Delay(10)
              ; Check if there are new data
              SEvent = NetworkServerEvent()
              Count + 1
            Else
              SEvent = 0
            EndIf
            
          Until SEvent <> #PB_NetworkEvent_Data
          BufferFillSize * 1000 / 1024 / 1024
          SetGadgetText( #hWnd_S_Speed, StrF(BufferFillSize/(ElapsedMilliseconds() - Time + 1), 2) + "MB/sec" )  
            
        EndIf
      EndIf
    EndIf
  EndIf
  
  Event = WaitWindowEvent(0)
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        
        Case #hWnd_R_Client
          DisableGadget(#hWnd_IP, #Null)
          
        Case #hWnd_R_Server
          DisableGadget(#hWnd_IP, #TRUE)
          
          
        Case #hWnd_TB_Buffer
          Buffersize = Pow( 2, GetGadgetState(#hWnd_TB_Buffer))
          If Buffersize > $FFFF : Buffersize = $FFFF : EndIf
          If *tcp_mem
            FreeMemory(*tcp_mem) : *tcp_mem = 0
          EndIf
          *tcp_mem = AllocateMemory(Buffersize)
          SetWindowTitle(#hWnd, "MEM: $" + Hex(Buffersize))
          ; Fill with Random Data
          For n = 0 To Buffersize - 4 Step 4
            PokeL (*tcp_mem + n, Random($FFFFFFFF))
          Next
          ; Set BufferSize
          PokeL (*tcp_mem, Buffersize)
          
        Case #hWnd_B_Run
          Time = ElapsedMilliseconds()
          
          If GetGadgetState(#hWnd_B_Run)
            DisableGadget(#hWnd_R_Client, #TRUE)
            DisableGadget(#hWnd_R_Server, #TRUE) 
            
            If GetGadgetState(#hWnd_R_Client)
              ClientID = OpenNetworkConnection( GetGadgetText(#hWnd_IP), 6001 )
            Else 
              ClientID = CreateNetworkServer( #PB_Any, 6001 )
            EndIf
            SetWindowTitle( #hWnd, "ID:" + Str(ClientID))
          Else
            
            If ClientID
              If GetGadgetState(#hWnd_R_Client)
                CloseNetworkConnection(ClientID)
              Else 
                CloseNetworkServer(ClientID)
              EndIf
              ClientID = #Null
            EndIf
            
            DisableGadget(#hWnd_R_Client, #Null)
            DisableGadget(#hWnd_R_Server, #Null)
          EndIf
      EndSelect
      
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #hWnd
        CloseWindow(#hWnd)
        Break
      EndIf
  EndSelect
Until Event = #PB_Event_CloseWindow
;}

PS: -EDIT- Maybe I also did something wrong, any comment is welcome;-)
"Daddy, I'll run faster, then it is not so far..."
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Re: TCP/IP Network Speed Check

Post by dell_jockey »

Hi Dige,

thanks for this code. I don't really understand how I should interpret the results. If I start the application twice, one as server and one as client (through localhost), I get very different values for each of the thoughputs. How can the client receive more than the server transmits?
For what it's worth: I too get a throughput of around 60 mb/s.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: TCP/IP Network Speed Check

Post by ultralazor »

the smart way to do this is just with pb net and time elapse functions. You can use this with fixed sized file downloads and get good data off averages.

This is how speed check websites do it in flash too..
so many ideas so little time..
Post Reply