Created
September 12, 2023 09:23
-
-
Save AnkurVyas-BTC/3454ffcfd75f20112e23db438b82e529 to your computer and use it in GitHub Desktop.
Mocking tracking requests
This file contains hidden or 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 'net/http' | |
require 'uri' | |
require 'benchmark' | |
require 'async' | |
# Function to make a network request | |
def tracking_request(tracking_id) | |
uri = URI.parse("https://jsonplaceholder.typicode.com/posts/#{tracking_id}") | |
response = Net::HTTP.get_response(uri) | |
response.body if response.is_a?(Net::HTTPSuccess) | |
end | |
# Benchmark the asynchronous processing | |
async_time = Benchmark.measure do | |
Async do | |
(1..10).each do |tracking_id| | |
Async do | |
post = tracking_request(tracking_id) | |
# Not printing to stdout to avoid clutter | |
end | |
end | |
end | |
end | |
# Benchmark the sequential processing | |
sequential_time = Benchmark.measure do | |
(1..10).each do |tracking_id| | |
post = tracking_request(tracking_id) | |
# Not printing to stdout to avoid clutter | |
end | |
end | |
puts "Benchmark results:" | |
puts "Async time: #{async_time.real}" | |
puts "Sequential time: #{sequential_time.real}" | |
# Output below: | |
# Benchmark results: | |
# Async time: 2.0476754249993974 | |
# Sequential time: 20.371668137999222 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment