bdberl/test/thrash_SUITE.erl

81 lines
2 KiB
Erlang
Raw Normal View History

2008-12-11 01:18:05 +00:00
%% -------------------------------------------------------------------
%%
%% bdberl: Port Driver Thrash tests
%% Copyright (c) 2008 The Hive. All rights reserved.
%%
%% -------------------------------------------------------------------
-module(thrash_SUITE).
-compile(export_all).
all() ->
[test_thrash].
-define(PROCS, 10).
2008-12-11 01:18:05 +00:00
test_thrash(_Config) ->
%% Spin up 15 processes (async thread pool is 10)
start_procs(?PROCS),
wait_for_finish(?PROCS).
start_procs(0) ->
ok;
start_procs(Count) ->
spawn_link(?MODULE, thrash_run, [self()]),
start_procs(Count-1).
wait_for_finish(0) ->
ok;
wait_for_finish(Count) ->
receive
{finished, Pid} ->
io:format("~p is done; ~p remaining.\n", [Pid, Count-1]),
wait_for_finish(Count-1)
end.
thrash_run(Owner) ->
%% Seed the RNG
{A1, A2, A3} = now(),
random:seed(A1, A2, A3),
%% Open up a port and database
2008-12-12 20:43:09 +00:00
{ok, 0} = bdberl:open("thrash", btree),
2008-12-11 01:18:05 +00:00
%% Start thrashing
2008-12-12 20:43:09 +00:00
thrash_incr_loop(Owner, 1000).
2008-12-11 01:18:05 +00:00
2008-12-12 20:43:09 +00:00
thrash_incr_loop(Owner, 0) ->
2008-12-11 01:18:05 +00:00
Owner ! {finished, self()};
2008-12-12 20:43:09 +00:00
thrash_incr_loop(Owner, Count) ->
ct:print("~p\n", [Count]),
2008-12-11 01:18:05 +00:00
%% Choose random key
Key = random:uniform(1200),
2008-12-11 01:18:05 +00:00
%% Start a txn that will read the current value of the key and increment by 1
F = fun() ->
2008-12-12 20:43:09 +00:00
case bdberl:get(0, Key, [rmw]) of
2008-12-11 01:18:05 +00:00
not_found ->
Value = 0;
2008-12-12 20:43:09 +00:00
{ok, Value} ->
2008-12-11 01:18:05 +00:00
Value
end,
2008-12-12 20:43:09 +00:00
ok = bdberl:put(0, Key, Value)
2008-12-11 01:18:05 +00:00
end,
2008-12-12 20:43:09 +00:00
ok = do_txn(F, 0),
thrash_incr_loop(Owner, Count-1).
2008-12-12 20:43:09 +00:00
do_txn(F, Count) ->
case bdberl:txn_begin() of
2008-12-11 01:18:05 +00:00
ok ->
case catch(F()) of
2008-12-12 20:43:09 +00:00
{'EXIT', _Reason} ->
io:format("Txn attempt ~p failed; retrying", [Count]),
2008-12-12 20:43:09 +00:00
do_txn(F, Count+1);
_Other ->
ok = bdberl:txn_commit()
2008-12-11 01:18:05 +00:00
end;
2008-12-12 20:43:09 +00:00
{error, _Reason} ->
do_txn(F, Count+1)
2008-12-11 01:18:05 +00:00
end.