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

Never mind: Needs Ruby 3.0!

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