Created
October 31, 2021 20:41
-
-
Save coezbek/07061fb7de2b036bf5c69f8a8b8cc3e5 to your computer and use it in GitHub Desktop.
Testing async with HTTParty and Async gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never mind: Needs Ruby 3.0!