Last active
February 11, 2016 04:02
-
-
Save holyxiaoxin/fe23d044550fa4b1493e to your computer and use it in GitHub Desktop.
Async vs Sync http requests
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
defmodule Tasks do | |
def main(_args) do | |
HTTPoison.start | |
# data gov caches the requests so i had to spin up a json server, | |
# which does not have caching to accurately test the async/sync time. | |
urls = [ | |
"https://data.gov.uk/data/api/service/transport/planned_road_works/road?road=M62", | |
"https://data.gov.uk/data/api/service/transport/planned_road_works/road?road=M62", | |
] | |
{microsec, _} = :timer.tc fn -> | |
urls | |
|> Enum.map(fn(url) -> Task.async(fn -> HTTPoison.get(url,[],timeout: 1500) end) end) | |
|> Enum.map(&Task.await(&1, 30000)) | |
|> Enum.each(fn({status, result}) -> | |
if (status == :ok) do | |
IO.inspect result.status_code | |
else | |
IO.inspect result.reason | |
end | |
end) | |
end | |
IO.puts "async took #{microsec} microsecs" | |
{microsec, _} = :timer.tc fn -> | |
urls | |
|> Enum.map(fn(url) -> HTTPoison.get(url,[],timeout: 1500) end) | |
|> Enum.each(fn({status, result}) -> | |
if (status == :ok) do | |
IO.inspect result.status_code | |
else | |
IO.inspect result.reason | |
end | |
end) | |
end | |
IO.puts "sync took #{microsec} microsecs" | |
end | |
end |
Author
holyxiaoxin
commented
Feb 11, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment