Skip to content

In Async environment, why http.get is too slow? #357

Open
@mephistobooks

Description

@mephistobooks

Hi, ioquatix.
Thank you for your great async-related gems. And I would like to ask a question here.

Running the code followings:

#
#
#
require 'net/http'
require 'async'

#
def http_new
  uri  = URI.parse "https://www.google.co.jp"
  http_obj = Net::HTTP.new(uri.host, uri.port)
  http_obj.use_ssl = true
  http_obj
end
def http_get( http_obj )
  ret = http_obj.get("/index.html")
  ret
end

#
hobj = http_new

#
puts "test 1"
    response = nil
    t = Async::Clock.now
    response = http_get(hobj)
    t = Async::Clock.now - t
    puts "message: #{response.message} in #{t} sec."

puts "test 2"
    response = nil
    t = Async::Clock.now
    Sync {
      response = http_get(hobj)
    }
    t = Async::Clock.now - t
    puts "message: #{response.message} in #{t} sec."

puts "test 3"
    response = nil
    t = Async::Clock.now
    Sync {
      Thread.new{ response = http_get(hobj) }.join
    }
    t = Async::Clock.now - t
    puts "message: #{response.message} in #{t} sec."

We get:

test 1
message: OK in 0.20697699999436736 sec.
test 2
message: OK in 10.215283999918029 sec.
test 3
message: OK in 0.17304600006900728 sec.

Why test 2 is too slow (x50 slower than test 1)...?
(test 3 is my work around for Async, but i cannot understand why the performance is improved. 🧐)


My environments are:
$ uname -a
Darwin clt-m1.local 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:36:26 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T8103 arm64
$ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin22]
$ gem list|grep async
async (2.17.0, 2.12.1)
$ gem list|grep net-http
net-http (default: 0.4.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions