Lag at HTTP GET
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
lennit · 2016-01-18 17:31
Hello,
These days I came back to my last project that would need to get
information out of a web form and process it. The problem is I get a
lag of 3-4 seconds, that is not to be ignored, because during that lag
I can't use any of the program's features, as it stalls.
This is my code (a small sample):
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i unit
Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils,
FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
DTAnalogClock, lNetComponents, simpleinternet, HTTPSend; type {
TForm1 } TForm1 = class(TForm) Label1: TLabel; Label2:
TLabel; Label3: TLabel; Label4: TLabel; c1: TLabel; c2:
TLabel; serverstate: TLabel; memResponse: TMemo; reoff2:
TButton; reon1: TButton; reoff1: TButton; client:
TLHTTPClientComponent; reon2: TButton; Timer1: TTimer;
procedure FormCreate(Sender: TObject); procedure
reoff1Click(Sender: TObject); procedure reoff2Click(Sender:
TObject); procedure reon1Click(Sender: TObject); procedure
reon2Click(Sender: TObject); procedure Timer1Timer(Sender:
TObject); private { private declarations } public { public
declarations } end; const templ1:string='{C1:=@value}'; templ2:string='{C2:=@value}'; var Form1: TForm1; template,
content:String; response:TStringList; HTTPClient: THTTPSend;
i:integer;implementation {$R *.lfm} { TForm1 } procedure
TForm1.reon1Click(Sender: TObject);begin
client.URI:='172.16.1.4/?button1on';
client.SendRequest;end; procedure TForm1.reon2Click(Sender:
TObject);begin client.URI:='172.16.1.4/?button2on';
client.SendRequest;end; procedure TForm1.Timer1Timer(Sender:
TObject);begin memresponse.Clear; if HttpGetText('172.16.1.4',
Response)then memResponse.Lines.AddStrings(Response); content:='';
for i:=1 to memresponse.Lines.Count do
content:=content+memresponse.Lines[i-1]; process(content, templ1);
c1.Caption := processedVariables.get('C1').toString;
processedvariables.clear; process(content, templ2); c2.Caption :=
processedVariables.get('C2').toString;
processedvariables.clear; end; procedure TForm1.reoff1Click(Sender:
TObject);begin client.URI:='172.16.1.4/?button1off';
client.SendRequest;end; procedure TForm1.reoff2Click(Sender:
TObject);begin client.URI:='172.16.1.4/?button2off';
client.SendRequest;end; procedure TForm1.FormCreate(Sender:
TObject);begin if client.Connect('172.16.1.4',80)=true then
serverstate.Caption:='Connected'; HTTPClient:= THTTPSend.Create;
Response:=TStringList.Create;end; end.
At the OnTimer event I retrieve information from the web form (every
10 seconds) and all stalls for 3-4 seconds until the data is
processed.
At the REON/REOFF events (which are basically turning relays on and
off) I get the same lag.
In my software I need to switch relays quite often and even if I close
my eyes for the lag at data retrieval, though I can't - this is just
two sensors and my project will extract data from over 50, it's
impossible to afford a 3 seconds lag before the next command is given.
I used to work with SNMP, but as we just switched to Arduino and
serial communication is out of question at the moment, I struggle a
lot with HTTP now.
What can I do to speed up the process? What do I do wrong to cause
this lag?
----------------------------------------------------------------------
engkin · 2016-01-18 18:20
Quote from: lennit on January 18, 2016, 06:31:42 pmWhat can I do to
speed up the process?
Use a thread to retrieve, process the data from the web, and call an
event on the form when the results are ready for display on c1 and c2
labels.
Quote from: lennit on January 18, 2016, 06:31:42 pmWhat do I do wrong
to cause this lag?
You are depending on the main GUI thread. It should be limited for
dealing with UI and quick processing. Anything that takes long time
should be diverted to another thread.
----------------------------------------------------------------------
lennit · 2016-01-18 18:59
Thank you I'll work on this first thing in the morning.