Page 1 of 1

is socket.http slow?

Posted: Mon Jan 11, 2021 10:38 pm
by dezoitodemaio
I'm executing a simple GET with socket.http to my local api (json-server) and it's taking about 2 seconds to finish the request.

Is this normal?

Code: Select all

  local conn = require 'socket.http'

  print(os.time())
  local r, s = conn.request(url)
  print(os.time())
the above code produces:

Code: Select all

1610403955
1610403957
When i call the api from the browser the request returns almost instantly.

Re: is socket.http slow?

Posted: Tue Jan 12, 2021 11:23 am
by pgimeno
I get a pretty stable 334 ms when querying http://example.com. By the way, socket includes a gettime() function with sub-second precision:

Code: Select all

local url = 'http://example.com'
local gettime = require 'socket'.gettime
local conn = require 'socket.http'

local start = gettime()
local r, s = conn.request(url)
print(gettime() - start)
Not sure what the cause may be in your case. Perhaps your browser has cached the result and that's why it seems to respond much quickly.

Re: is socket.http slow?

Posted: Tue Jan 12, 2021 11:48 am
by dezoitodemaio
Well i should have tested it with another api before opening the thread, your example runs pretty fast in my machine too. I think the problem is json-server, for some reason it is slow when called from socket.http.