Skip to content

Instantly share code, notes, and snippets.

@jaydoane
Last active October 27, 2017 23:21
Show Gist options
  • Save jaydoane/aa4401058aa051e413c0f8f34b6c293e to your computer and use it in GitHub Desktop.
Save jaydoane/aa4401058aa051e413c0f8f34b6c293e to your computer and use it in GitHub Desktop.
Make parallel requests to a URL for some duration, and print out request rate and number of errors encountered
-module(req).
-export([
req_for/2,
parallel_req_for/3]).
-include_lib("ibrowse/include/ibrowse.hrl").
epoch_sec() ->
{MegaSec, Sec, MicroSec} = os:timestamp(),
MegaSec*1000000 + Sec + MicroSec/1000000.
req_for(Url, Duration) ->
End = epoch_sec() + Duration,
req_until(Url, End, {0, 0}).
req_until(Url, End, {OkCount, ErrCount}) ->
case epoch_sec() < End of
true ->
NewCounts = case ibrowse:send_req(Url, [], get) of
{ok, "200", _Headers, _Body} ->
{OkCount + 1, ErrCount};
_ ->
{OkCount, ErrCount + 1}
end,
req_until(Url, End, NewCounts);
false ->
{OkCount, ErrCount}
end.
parallel_req_for(ProcessCount, Url, Duration) ->
ibrowse:start(),
#url{host = Host, port = Port} = ibrowse_lib:parse_url(Url),
ok = ibrowse:set_max_sessions(Host, Port, ProcessCount),
ok = ibrowse:set_max_pipeline_size(Host, Port, 100),
Until = epoch_sec() + Duration,
Self = self(),
Pids = [spawn_link(fun() -> Self ! {self(), req_until(Url, Until, {0, 0})} end)
|| _N <- lists:seq(1, ProcessCount)],
ZippedCounts = [receive {Pid, Counts} -> Counts end || Pid <- Pids],
{OkCounts, ErrCounts} = lists:unzip(ZippedCounts),
OkSum = lists:sum(OkCounts),
ErrSum = lists:sum(ErrCounts),
{OkSum/Duration, ErrSum}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment