Recent

Author Topic: Slow request  (Read 1845 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Slow request
« on: November 14, 2021, 07:41:50 pm »
Hey guys,
I'm using TFPHttpClient from fphttpclient uses to create this request:

Code: Pascal  [Select][+][-]
  1. function GetRequest: string;
  2. var
  3.   Client: TFPHttpClient;
  4.         Response : TStringStream;
  5.  
  6. begin
  7.         Client := TFPHttpClient.Create(nil);
  8.         Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  9.         Client.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  10.         Client.AddHeader('Accept', 'application/json');
  11.   Client.KeepConnection := True;
  12.         //Client.AllowRedirect := true;
  13.         Response := TStringStream.Create('');
  14.         try
  15.     try
  16.         Client.Post('... my url ...', Response);
  17.           except on E:Exception do
  18.                 ShowMessage('Something bad happened : ' + E.Message);
  19.           end;
  20.         finally
  21.     Client.RequestBody.Free;
  22.     Client.Free;
  23.  
  24.         Result := Response.DataString;
  25.     Response.Free;
  26.                
  27.         end;
  28. end;

It works well but problem is that this request takes about 700ms to response. If i run it in Chrome browser, it takes about 67ms only! Interesting thing is when i run it again, it takes much less time, but it still isn't fast as Chrome. What's wrong?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1786
  • Former Delphi 1-7, 10.2 user
Re: Slow request
« Reply #1 on: November 14, 2021, 09:59:44 pm »
I'm guessing the delay is caused by DNS lookups which would explain why the second time is quicker.
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Slow request
« Reply #2 on: November 14, 2021, 10:39:17 pm »
What can i do to make it faster?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1786
  • Former Delphi 1-7, 10.2 user
Re: Slow request
« Reply #3 on: November 14, 2021, 10:52:18 pm »
Run a caching nameserver... when such a nameserver retrieves the answer to a client query, it not only returns the answer to the client but also stores the answer in its cache for the period of time allowed by the TTL value of the record. The cache can then be used as a source for subsequent requests to speed up the total round-trip time.
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Slow request
« Reply #4 on: November 14, 2021, 11:05:09 pm »
If I understand correctly, this can only be used if the request and the answer are the same. But what if the request is different?
For example:

http://mypage.com/index.php?name=Willie
http://mypage.com/index.php?name=John
http://mypage.com/index.php?name=Barnie

According to this cache logic, it shouldn't affect speed, right?
But it's not clear to me that when I run any request in Google Chrome, it always comes back extremely quickly. Why is that?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1786
  • Former Delphi 1-7, 10.2 user
Re: Slow request
« Reply #5 on: November 14, 2021, 11:47:13 pm »
If I understand correctly, this can only be used if the request and the answer are the same. But what if the request is different?
For example:

http://mypage.com/index.php?name=Willie
http://mypage.com/index.php?name=John
http://mypage.com/index.php?name=Barnie

You misunderstand - the lookup is for mypage.com only. None of the rest of those urls can be found in DNS records.

Quote
But it's not clear to me that when I run any request in Google Chrome, it always comes back extremely quickly. Why is that?

My experience with the SeaMonkey browser is that it does its own DNS caching. I've never used Chrome, but I'd speculate that it's also doing its own DNS caching for a period which may well exceed a record's TTL.

Here's an example of DNS caching on my FreeBSD server - note the 1ms query response time:

Code: Text  [Select][+][-]
  1. $ dig sentry.org
  2.  
  3. ; <<>> DiG 9.16.22 <<>> sentry.org
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37769
  7. ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
  8.  
  9. ;; OPT PSEUDOSECTION:
  10. ; EDNS: version: 0, flags:; udp: 1232
  11. ; COOKIE: 1aad2be30b46cf47010000006191924ce3540b5e84fdcfb0 (good)
  12. ;; QUESTION SECTION:
  13. ;sentry.org.               IN   A
  14.  
  15. ;; ANSWER SECTION:
  16. sentry.org.     86400   IN A    45.124.52.214
  17.  
  18. ;; Query time: 1 msec
  19. ;; SERVER: 192.168.1.4#53(192.168.1.4)
  20. ;; WHEN: Mon Nov 15 09:48:44 AEDT 2021
  21. ;; MSG SIZE  rcvd: 83
« Last Edit: November 14, 2021, 11:50:44 pm by trev »
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Slow request
« Reply #6 on: November 15, 2021, 11:37:00 pm »
Thank you for your response, I get it now. So the problem is that my application in Lazarus takes longer time because it needs to translate "mypage.com" to IP address (i think it's called DNS translate or so).

It's still strange because I have a application created with Java that sends request to the same address and it works blazingly fast (at least as fast as Google Chrome).

So, what am I supposed to do now?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1786
  • Former Delphi 1-7, 10.2 user
Re: Slow request
« Reply #7 on: November 16, 2021, 12:30:12 am »
You didn't mention what OS you are using, but you could try using an OS native API which may resolve sites faster.

* macOS: NSURLSession
* Windows: WinInet
Lazarus 2.3, FPC 3.3.1 macOS 12.1 x86_64 Xcode 13.2
Lazarus 2.3, FPC 3.3.1 macOS 12.1 aarch64 Xcode 13.2
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 amd64 VM

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: Slow request
« Reply #8 on: November 16, 2021, 03:28:44 pm »
Application runs on Windows 10.
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

 

TinyPortal © 2005-2018