?
Solved

How to calculate download speed?

Posted on 2008-11-01
6
Medium Priority
?
3,770 Views
Last Modified: 2013-11-23
How to calculate the download speed in "kb" using URLDownloadToFile?

sorry for my poooor english ^^
0
Comment
Question by:BuGFiX00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22858856
basically ?
before you start : save the starting time
when your done, (now - starttime) / filesize

// average in Kb/sec
function TimedDownload(Url, FileName: string): Double;
var aStartTime, aEndTime: TDateTime;
begin
  Result := 0;
  aStartTime := Now;
  URLDownLoadToFile(Url, FileName);
  if FileExists(FileName) then
    Result := SecondSpan(Now, aStartTime) / (FileSize(FileName) / 1024);
end;
0
 

Author Comment

by:BuGFiX00
ID: 22859047
I need the current download speed.
i need to show the download speed while the DownloadThread is active and downloading
the download thread code:

type
  TDownloadThread = class(TThread)
  private
  FURL: String;
  FFileName: String;
  protected
  procedure Execute; override;
  public
//  Downloading : Boolean;
  Progress : Int64;
  Duration : Integer;
  FileSize : Int64;
  DownloadPercent : Integer;
  FileName : String;
  URL : String;
  constructor Create(sURL,sSaveToFile: String);
  destructor Destroy; override;
  procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
  fProgress, fProgressMax: Cardinal;
  StatusCode: TURLDownloadStatus;
  StatusText: String; var Cancel: Boolean);
  end;
 
implementation
 
uses
  Main;
 
{ TDownloadThread }
 
constructor TDownloadThread.Create(sURL: string; sSaveToFile: string);
begin
  inherited Create(False);
  FURL := sURL;
  FFileName := sSaveToFile;
  FreeOnTerminate := True;
end;
 
procedure TDownloadThread.Execute;
begin
  with TDownloadURL.Create(nil) do
  try
  URL:= FURL;
  FileName := FFileName;
  OnDownloadProgress := URL_OnDownloadProgress;
  ExecuteTarget(nil);
  finally
  Free;
  end;
end;
 
destructor TDownloadThread.Destroy;
begin
  //Downloading := False;
end;
 
 
procedure TDownloadThread.URL_OnDownloadProgress(Sender: TDownLoadURL;
  fProgress, fProgressMax: Cardinal;
  StatusCode: TURLDownloadStatus;
  StatusText: String; var Cancel: Boolean);
begin
 // Downloading := True;
  Progress := fProgress;
  FileSize := fProgressMax;
 
 if (Progress > 0) and (fProgressMax > 0) then
  DownloadPercent := Trunc(((fProgress / fProgressMax) * 100));
 
  FileName := FFileName;
  URL := FURL;
end;

Open in new window

0
 

Author Comment

by:BuGFiX00
ID: 22862724
?????
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22863081
the download speed average is the total downloaded divided by the total time
when you need to find the speed at that moment
you will monitor it over a certain amount of time (delta time)
in this time frame you will have downloaded a portion (delta download)
download speed at that moment (delta download / delta time)
providing delta time > 0

you need to tell the main form how much is downloaded and how fast it is going ...
for this you will send back info to the main form
for this i use a callback thread

your using your main form in your thread uses ... this will solve that problem too :)
type
  TCallbackProc = procedure (aMessage: string; aMessageInfo: Integer = 0) of object;
 
  TCallbackThread = class(TThread)
  private
    FCallBack: TCallbackProc;
    FCallbackMsg: string;
    FCallbackMsgInfo: integer;
    procedure SynchedCallback;
  protected
    procedure DoCallback(aMsg: string; aMsgInfo: integer = 0); virtual;
    property Callback: TCallbackProc read FCallback;
  public
    constructor Create(aCallback: TCallbackProc; CreateSuspended: Boolean = False); reintroduce; virtual;
  end;
 
  TDownloadThread = class(TCallbackThread)
  private
    FURL: String;
    FFileName: String;
    FProgress: Int64;
    FDuration: Integer;
    FFileSize: Int64;
    FDownloadPercent: Integer;
    fLastTime: TDateTime;
    fLastDownloaded: Int64;
  protected
    procedure Execute; override;
  public
    constructor Create(aURL,aSaveToFile: String; aCallbackProc: TCallbackProc); reintroduce;
    destructor Destroy; override;
    procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
      aProgress, aProgressMax: Cardinal;
      StatusCode: TURLDownloadStatus;
      StatusText: String; var Cancel: Boolean);
  end;
 
implementation
 
{ TCallbackThread }
 
constructor TCallbackThread.Create(aCallback: TCallbackProc; CreateSuspended: Boolean = False);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := True;
  FCallback := aCallback;
end;
 
procedure TCallbackThread.DoCallback(aMsg: string; aMsgInfo: Integer = 0);
begin
  FCallbackMsg := aMsg;
  FCallbackMsgInfo := aMsgInfo;
  Synchronize(SynchedCallback);
end;
 
procedure TCallbackThread.SynchedCallback;
begin
  if Assigned(FCallback) then
    FCallBack(FCallbackMsg, FCallbackMsgInfo);
end;
 
{ TDownloadThread }
 
constructor TDownloadThread.Create(aURL, aSaveToFile: string; aCallback: TCallbackProc);
begin
  inherited Create(aCallback, False);
  FURL := aURL;
  FFileName := aSaveToFile;
  fLastTime := Now;
  FreeOnTerminate := True;
end;
 
procedure TDownloadThread.Execute;
begin
  DoCallback('Starting download of ' + fUrl + '/' + fFileName, 0);
  with TDownloadURL.Create(nil) do
  try
    URL:= FURL;
    FileName := FFileName;
    OnDownloadProgress := URL_OnDownloadProgress;
    fLastTime := Now;
    fLastDownloaded := 0;
    ExecuteTarget(nil);
  finally
    Free;
  end;
  DoCallbakc('Download finished', 10);
end;
 
destructor TDownloadThread.Destroy;
begin
  //Downloading := False;
  inherited Destroy;
end;
 
procedure TDownloadThread.URL_OnDownloadProgress(Sender: TDownLoadURL;
  aProgress, aProgressMax: Cardinal;
  StatusCode: TURLDownloadStatus;
  StatusText: String; var Cancel: Boolean);
  deltaDownloaded: Int64;
begin
  // Downloading := True;
  fProgress := aProgress;
  fFileSize := aProgressMax;
 
  if (fProgress > 0) and (fFileSize > 0) then
    fDownloadPercent := Trunc(((fProgress / fFileSize) * 100));
  // Show percent complete  
  DoCallback(FloatToStr(fDownloadPercent), 1);
  // Show speed 
  deltaDownloaded := fProgress - fLastDownloaded;
  DoCallback(FloatToStr(deltaDownloaded / 1024 / SecondSpan(Now, fLastTime)), 2);
  fLastTime := Now;
  fLastDownloaded := fProgress;
end;

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22863112
oh yeah, here is how you create the proc from the main thread

type
  TForm1 = class(TForm)
    ProgressBar1: TProgressBar;
    LabelMessage: TLabel;
    LabelSpeed: TLabel;
  private
    fThread:  TDownloadThread;
  protected
    procedure DownloadMsg(aMessage: string; aMessageInfo: Integer = 0);
  public
    procedure StartDownload(aUrl, aFileName: string);
  end;

procedure TForm1.StartDownload(aUrl, aFileName: string);
begin
  fThread := TDownLoadThread.Create(aUrl, aFileName, DownloadMsg);
end;

procedure TForm1.DownloadMsg(aMessage: string; aMessageInfo: Integer = 0);
begin
  case aMessageInfo of
    0: LabelMessage.Caption := aMessage;
    1: ProgressBar1.Position := Round(StrToFloat(aMessage));
    2: LabelSpeed.Caption := Format('%.2f kb/s', [StrToFloat(aMessage)]);
  end;
end;
0
 

Author Closing Comment

by:BuGFiX00
ID: 31512373
Tankyou very much!! xD
PERFECT!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question