Skip to content

Instantly share code, notes, and snippets.

@kgbu
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save kgbu/1cd68d759b461d4c5461 to your computer and use it in GitHub Desktop.

Select an option

Save kgbu/1cd68d759b461d4c5461 to your computer and use it in GitHub Desktop.
apach-bench like code
-module(ab).
-export([start/0,start/1,start/2,start/3]).
%% for spawn
-export([req/3]).
-define(DEFAULTURL, "http://127.0.0.1/").
start() ->
start(1000, ?DEFAULTURL, 1).
start(N) ->
start(N, ?DEFAULTURL, 1).
start(N, Url) ->
start(N, Url, 1).
start(0, Url, _) ->
io:format("~p end.~n",[Url]),
ok;
start(N, Url, Count) ->
spawn (ab, req, [N, Url, Count]),
io:format("~p/~p started~n", [N, Count]),
start (N - 1, Url, Count).
req(N, Url, 0) ->
httpc:request(Url),
io:format("~p totally end~n",[N]);
req(N, Url, Count) ->
httpc:request(Url),
io:format("~p/~p end~n",[N, Count]),
req(N, Url, Count - 1).
@kgbu

kgbu commented Sep 23, 2014

Copy link
Copy Markdown
Author

exec sample

bash-3.2$ erl
Erlang/OTP 17 [erts-6.2] [source-5c974be] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.2  (abort with ^G)
1> inets:start().
ok
2> c(ab).
{ok,ab}
3> ab:start(5,"http://192.168.1.127/",3).
5/3 started
4/3 started
3/3 started
2/3 started
1/3 started
"http://192.168.1.127/" end.
ok
2/3 end
3/3 end
4/3 end
5/3 end
1/3 end
2/2 end
4/2 end
3/2 end
1/2 end
5/2 end
2/1 end
4/1 end
3/1 end
1/1 end
5/1 end
2 totally end
4 totally end
1 totally end
3 totally end
5 totally end
4> 

@kgbu

kgbu commented Sep 23, 2014

Copy link
Copy Markdown
Author

todos

  • statistics
    • elapsed time option
    • error ratio

@kgbu

kgbu commented Sep 23, 2014

Copy link
Copy Markdown
Author

単純に何回もURLをアクセスしたい場合のワンライナー

11> AB = fun(M) -> lists:foldl(fun(_,_) -> R = httpc:request("http://192.168.1.127/"),io:format("~p~n",[R]) end, 0,lists:seq(1,M)) end.
#Fun<erl_eval.6.90072148>
12> AB(2).
{ok,{{"HTTP/1.1",200,"OK"},
     [{"connection","close"},
      {"date","Tue, 23 Sep 2014 20:26:16 GMT"},
      {"accept-ranges","bytes"},
      {"etag","\"2614f9-0-503b89d82e68c\""},
      {"server","Apache/2.2.15 (CentOS)"},
      {"content-length","0"},
      {"content-type","text/html; charset=UTF-8"},
      {"last-modified","Tue, 23 Sep 2014 09:58:55 GMT"}],
     []}}
{ok,{{"HTTP/1.1",200,"OK"},
     [{"connection","close"},
      {"date","Tue, 23 Sep 2014 20:26:16 GMT"},
      {"accept-ranges","bytes"},
      {"etag","\"2614f9-0-503b89d82e68c\""},
      {"server","Apache/2.2.15 (CentOS)"},
      {"content-length","0"},
      {"content-type","text/html; charset=UTF-8"},
      {"last-modified","Tue, 23 Sep 2014 09:58:55 GMT"}],
     []}}
ok
13> 

lists:foldl使うなら、経過時間を累計するとかしたい。

27> K2 = fun(N) -> lists:foldl(fun(Elem,Acc) -> {R, _} = timer:tc(httpc, request, ["http://192.168.1.127/"]),R + Acc end, 0,lists:seq(1,N))/N end.
#Fun<erl_eval.6.90072148>
28> K2(4).
4381.75
29> 

@kgbu

kgbu commented Sep 23, 2014

Copy link
Copy Markdown
Author

もっとシンプルな繰り返し

14> [httpc:request("http://192.168.1.127") || _ <- lists:seq(1,2)]. 
[{ok,{{"HTTP/1.1",200,"OK"},
      [{"connection","close"},
       {"date","Tue, 23 Sep 2014 20:30:18 GMT"},
       {"accept-ranges","bytes"},
       {"etag","\"2614f9-0-503b89d82e68c\""},
       {"server","Apache/2.2.15 (CentOS)"},
       {"content-length","0"},
       {"content-type","text/html; charset=UTF-8"},
       {"last-modified","Tue, 23 Sep 2014 09:58:55 GMT"}],
      []}},
 {ok,{{"HTTP/1.1",200,"OK"},
      [{"connection","close"},
       {"date","Tue, 23 Sep 2014 20:30:18 GMT"},
       {"accept-ranges","bytes"},
       {"etag","\"2614f9-0-503b89d82e68c\""},
       {"server","Apache/2.2.15 (CentOS)"},
       {"content-length","0"},
       {"content-type","text/html; charset=UTF-8"},
       {"last-modified","Tue, 23 Sep 2014 09:58:55 GMT"}],
      []}}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment