Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 21st, 2012  |  syntax: None  |  size: 1.44 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -module(bench).
  2. -compile(export_all).
  3.  
  4. -define(COUNT, 1000).
  5. -define(SIZE, 10000).
  6. -define(PORT, 9001).
  7.  
  8. read_all(Socket, Limit) when Limit > 0 ->
  9.   case gen_tcp:recv(Socket, 100*?SIZE) of
  10.     {ok, Bin} ->
  11.       read_all(Socket, Limit - size(Bin));
  12.     {error, Error} ->
  13.       {error, {Error, Limit}}
  14.   end;
  15.  
  16. read_all(Socket, 0) ->
  17.   gen_tcp:close(Socket),
  18.   ok.
  19.  
  20. listener(Self) ->
  21.   {ok, Listen} = gen_tcp:listen(?PORT, [binary, {reuseaddr,true}]),
  22.   Self ! ready,
  23.   {ok, Client} = gen_tcp:accept(Listen),
  24.   gen_tcp:close(Listen),
  25.   ok = inet:setopts(Client, [{recbuf, 2*?SIZE}, {packet,raw},{active,false}]),
  26.   ok = read_all(Client, ?COUNT*?SIZE),
  27.   ok.
  28.  
  29. listen() ->
  30.   Self = self(),
  31.   spawn(?MODULE, listener, [Self]),
  32.   receive
  33.     ready -> ok
  34.   end,
  35.   ok.
  36.  
  37. run() ->
  38.   Bin = crypto:rand_bytes(?SIZE),
  39.   List = lists:seq(1,?COUNT),
  40.  
  41.   listen(),
  42.   {ok, Socket} = gen_tcp:connect("localhost", ?PORT, [binary, {sndbuf, 1000*?SIZE}]),
  43.   T1 = erlang:now(),
  44.   % [erlang:port_command(Socket, Bin, [nosuspend]) || _N <- List],
  45.   [gen_tcp:send(Socket, Bin) || _N <- List],
  46.   T2 = erlang:now(),
  47.   io:format("~p slow sends in ~p microseconds~n", [?COUNT, timer:now_diff(T2,T1)]),
  48.  
  49.  
  50.   listen(),
  51.   {ok, Socket2} = gen_tcp:connect("localhost", ?PORT, [binary, {sndbuf, 1000*?SIZE}]),
  52.   T3 = erlang:now(),
  53.   [erlang:port_command(Socket2, Bin, [nosuspend]) || _N <- List],
  54.   T4 = erlang:now(),
  55.   io:format("~p fast sends in ~p microseconds~n", [?COUNT, timer:now_diff(T4,T3)]),
  56.   ok.