Network Throuput

Developed or developing a new product in PureBasic? Tell the world about it.
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post by akee »

interesting filename Tampon.... :wink:
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Here the translation

Code: Select all

; PureBasic 3.93
; Droopy 06/06/05

;{- Constants
#Average=1.5
#Hight=3
#Full=10
#NbofBar=44
#FileSizeMin=100001 ; 1Mo
#PacketSize=100000
;}

;{- If INI file note found --> Create / if exist --> Read it
If FileSize(GetProgramPath()+"NetworkThroughput.ini")=-1
  Repeat ;/ If source file is too small : Ask another file
    SourceFile.s=OpenFileRequester("Source File","","All files|*.*",0)
    If FileSize(SourceFile)<#FileSizeMin
      MessageRequester("Source File too small","Min "+Str(#FileSizeMin)+" byte")
    EndIf
  Until FileSize(SourceFile)>#FileSizeMin
  ;/ Ask for Destination file
  DestinationFile.s=OpenFileRequester("Destination File","","All Files|*.*",0)
  IniWrite(GetProgramPath()+"NetworkThroughput.ini","Config","Read",SourceFile)
  IniWrite(GetProgramPath()+"NetworkThroughput.ini","Config","Write",DestinationFile)
Else
  ;/ Missing INI file --> Create it and store preferences
  SourceFile.s=IniRead(GetProgramPath()+"NetworkThroughput.ini","Config","Read")
  DestinationFile.s=IniRead(GetProgramPath()+"NetworkThroughput.ini","Config","Write")
EndIf
;}
  
;{- SourceFile missing of Destination unreachable
If FileSize(SourceFile)=-1
  MessageRequester("Source File not Found",SourceFile,#MB_ICONERROR)
  End
EndIf
If GetDiskFreeSpaceEx(Left(DestinationFile,2))=0
  MessageRequester("Missing Destination",Left(DestinationFile,2),#MB_ICONERROR)
  End
EndIf
;}

Enumeration ;- of Gadgets
  #Frame1
  #Frame2
  #Read
  #Write
  #GraphRead
  #GraphWrite
EndEnumeration

;{- Sound @ Beginning
Sound=InitSound() 
If Sound <>0
  CatchSound(0, ?Sound)
  PlaySound(0,0)
EndIf
;}

;{- Visual design
Handle=OpenWindow(0, 216, 0, 510, 302,  #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_Invisible , "Network Throughput")
SetWindowsTransparency(Handle,0)
HideWindow(0,0)
CreateGadgetList(WindowID())
TextGadget(#Frame1, 10, 10, 240, 20, "Read",#PB_Text_Center)
TextGadget(#Frame2, 260, 10, 240, 20, "Write",#PB_Text_Center)
TextGadget(#Read, 10, 250, 240, 20, "",#PB_Text_Center )
TextGadget(#Write, 260, 250, 240, 20, "",#PB_Text_Center )
GraphRead=Graph(20, 30, 220, 200, #NbofBar,#Average,#Hight,#Full,255,65535,65280,0)
GraphWrite=Graph(270, 30, 220, 200 ,#NbofBar,#Average,#Hight,#Full,255,65535,65280,0)
CreateStatusBar(0,WindowID(0))
AddStatusBarField(255)
AddStatusBarField(255)

;/ Fade in
For n= 1 To 255 Step 6
  SetWindowsTransparency(WindowID(0),n)
  Delay(1)
Next n
;}    

*buffer=AllocateMemory(#PacketSize)

Repeat

  If totoro>250  
    totoro=0
    
  ;{/ Measure & Calculate Read rate
  OpenFile(0,SourceFile)
  CreateFile(1,GetEnvironmentVariable("temp")+"\NetWork.tst")
  UseFile(0)
  MeasureHiResIntervalStart()
  ReadData(*buffer,#PacketSize)
  UseFile(1)
  WriteData(*buffer,#PacketSize)
  ReadDuration.f=MeasureHiResIntervalStop()
  ReadRate.f=#PacketSize/ReadDuration
  ReadRate/1024/1024 ; Convert rate to Mb/s
  CloseFile(0)
  CloseFile(1)
  DeleteFile(GetEnvironmentVariable("temp")+"\NetWork.tst")
  ;}
  
  ;{/ Measure & Calculate Write rate
  CreateFile(0,DestinationFile)
  MeasureHiResIntervalStart()
  WriteData(*buffer,#PacketSize)
  WriteDuration.f=MeasureHiResIntervalStop()
  WriteRate.f=#PacketSize/WriteDuration
  WriteRate/1024/1024 ; Convert rate to Mb/s
  CloseFile(0)
  DeleteFile(DestinationFile)
  ;}
  
  ;{/ Set GadgetText
  SetGadgetText(#Read,StrF(ReadRate,3)+" Mb/s")
  SetGadgetText(#Write,StrF(WriteRate,3)+" Mb/s")
  ;}
  
  ;{/ Set Status Bar ( Refresh average )
  ReadAverage.f+ReadRate
  WriteAverage.f+WriteRate
  NbofSamples+1
  temp1.s=" Read Average "+StrF((ReadAverage/NbofSamples),2)+" Mb/s"
  temp2.s=" Write Average "+StrF((WriteAverage/NbofSamples),2)+" Mb/s"
  StatusBarText(0,0,temp1)
  StatusBarText(0,1,temp2)
  ;}
  
  ;{/ Set Graphs
  GraphSet(GraphRead,ReadRate)
  GraphSet(GraphWrite,WriteRate)
  ;}

  EndIf

  Delay(1)
  totoro+1

  ;/ Set Nb of Samples in the Windows Title 
  SetWindowTitle(0,"Network Throughput ( "+Str(NbofSamples)+" Samples )")

a=WindowEvent()
Until a=#PB_Event_CloseWindow 

;/ Sound @ End
If Sound <>0
  PlaySound(0,0)
EndIf

;{/ Quit ( Release ressources & Fade Out
FreeMemory(*buffer)
For n= 255 To 0 Step -3
  SetWindowsTransparency(Handle,n)
  Delay(1)
Next n
;}


DataSection
Sound: IncludeBinary "Network Throughput.wav"
EndDataSection
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

got the zip now! hm it asks for a source file what shall that be?
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

This code measure network throughput.

1° (Read Rate) Copy a file ( any file you want ) to temp directory
2° (Write Rate ) Copy the file to the destination file ( a file is created and deleted )
got the zip now! hm it asks for a source file what shall that be?
You must specify the 1° file
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

works! Thanks :)
Post Reply