Skip to content

Instantly share code, notes, and snippets.

@hiroeorz
Created October 9, 2011 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroeorz/1273955 to your computer and use it in GitHub Desktop.
Save hiroeorz/1273955 to your computer and use it in GitHub Desktop.
eredisのテスト
-module(redis_test).
-define(KEY, <<"sample">>).
-export([push_to_list/1, parallel_push_to_list/2, get_from_list/2, delete/0]).
%%--------------------------------------------------------------------
%% @doc Count件数のデータをリストに追加し、最終的な件数と経過時間を表示する.
%%--------------------------------------------------------------------
push_to_list(Count) ->
{ok, Pid} = eredis:start_link(),
StartTime = get_seconds(),
set_value_to_list(Pid, Count, ?KEY),
EndTime = get_seconds(),
PassedSeconds = EndTime - StartTime,
{ok, LenBin} = eredis:q(Pid, ["llen", ?KEY]),
io:format("Length : ~p~n", [binary_to_list(LenBin)]),
io:format("Passed Time: ~w sec~n", [PassedSeconds]).
%%--------------------------------------------------------------------
%% @doc ProcessCount個数のプロセスから並列にCount件数のデータをリストに追加し、最終的な件数と経過時間を表示する.
%%--------------------------------------------------------------------
parallel_push_to_list(ProcessCount, Count) ->
{ok, Pid} = eredis:start_link(),
StartTime = get_seconds(),
parallel_push(ProcessCount, Count, Pid),
collect_loop(ProcessCount),
EndTime = get_seconds(),
PassedSeconds = EndTime - StartTime,
io:format("Passed Time: ~w sec~n", [PassedSeconds]).
%%--------------------------------------------------------------------
%% @doc 指定した範囲の値を取得して経過時間を表示する.
%%--------------------------------------------------------------------
get_from_list(SIndex, EIndex) ->
{ok, Pid} = eredis:start_link(),
StartTime = get_seconds(),
{ok, List} = eredis:q(Pid, ["lrange" | [?KEY, SIndex, EIndex] ]),
EndTime = get_seconds(),
PassedSeconds = EndTime - StartTime,
io:format("Length : ~w~n", [length(List)]),
io:format("Passed Time: ~w sec~n", [PassedSeconds]).
%%--------------------------------------------------------------------
%% @doc リストを削除する.
%%--------------------------------------------------------------------
delete() ->
{ok, Pid} = eredis:start_link(),
eredis:q(Pid, ["del", ?KEY]).
%%--------------------------------------------------------------------
%% @private
%%
%% @doc Count件数のデータをリストに追加する.
%%--------------------------------------------------------------------
set_value_to_list(_Pid, 0, _Key) -> ok;
set_value_to_list(Pid, Count, Key) ->
Val = lists:flatten(io_lib:format("this is value of ~w", [Count])),
{ok, _} = eredis:q(Pid, ["rpush" | [Key, Val] ]),
set_value_to_list(Pid, Count - 1, Key).
%%--------------------------------------------------------------------
%% @private
%%
%% @doc 現在時間を小数点以下を含む秒単位で返す.
%%--------------------------------------------------------------------
get_seconds() ->
{Mega, Sec, Micro} = now(),
((Mega * 1000000 + Sec) * 1000000 + Micro) / 1000000.
%%--------------------------------------------------------------------
%% @private
%%
%% @doc Redisに値を保存するプロセスをProcessCount数だけ生成する.
%%--------------------------------------------------------------------
parallel_push(0, _Count, _Pid) -> ok;
parallel_push(ProcessCount, Count, Pid) ->
ParentPid = self(),
spawn(fun() ->
Key = lists:flatten(io_lib:format("key1_~w", [ProcessCount])),
ok = set_value_to_list(Pid, Count, Key),
ParentPid ! ok
end),
parallel_push(ProcessCount - 1, Count, Pid).
%%--------------------------------------------------------------------
%% @private
%%
%% @doc Redisに値を保存する各プロセスからの結果を収集する.
%%--------------------------------------------------------------------
collect_loop(0) -> ok;
collect_loop(Count) ->
receive
ok -> collect_loop(Count - 1)
after 5000 -> {error, timeout}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment