Last active
August 29, 2015 14:06
-
-
Save kgbu/1cd68d759b461d4c5461 to your computer and use it in GitHub Desktop.
apach-bench like code
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
| -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). |
Author
Author
todos
- statistics
- elapsed time option
- error ratio
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>
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
exec sample