Skip to content

Instantly share code, notes, and snippets.

@coezbek
Created October 31, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coezbek/07061fb7de2b036bf5c69f8a8b8cc3e5 to your computer and use it in GitHub Desktop.
Save coezbek/07061fb7de2b036bf5c69f8a8b8cc3e5 to your computer and use it in GitHub Desktop.
Testing async with HTTParty and Async gem
require 'async'
require 'benchmark'
require 'open-uri'
require 'httparty'
n = 3
Benchmark.bm(20) do |b|
b.report "Sequential" do
n.times do |i|
HTTParty.get("https://httpbin.org/delay/1.6")
end
end
b.report "Threads + HTTParty" do
threads = Array.new(n) { |i|
Thread.new {
HTTParty.get("https://httpbin.org/delay/1.6")
}
}
threads.each(&:join)
end
b.report "Async + HTTParty" do
Async do |task|
n.times do |i|
task.async do
HTTParty.get("https://httpbin.org/delay/1.6")
end
end
end
end
b.report "Async + open-uri" do
Async do |task|
n.times do |i|
task.async do
URI.open("https://httpbin.org/delay/1.6")
end
end
end
end
end
@coezbek
Copy link
Author

coezbek commented Oct 31, 2021

On my machine Async does not show any speed-up:

                           user     system      total        real
Sequential             0.022462   0.000000   0.022462 (  6.231416)
Threads + HTTParty     0.026574   0.000694   0.027268 (  2.127636)
Async + HTTParty       0.008827   0.010009   0.018836 (  6.255890)
Async + open-uri       0.021286   0.001112   0.022398 (  6.266774)

@coezbek
Copy link
Author

coezbek commented Oct 31, 2021

Never mind: Needs Ruby 3.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment