PageRenderTime 27ms CodeModel.GetById 21ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/test/hottub_tests.erl

http://github.com/bfrog/hottub
Erlang | 93 lines | 86 code | 5 blank | 2 comment | 0 complexity | e9f1bef5cdb44dd4b119e2237edd0132 MD5 | raw file
 1-module(hottub_tests).
 2-include_lib("eunit/include/eunit.hrl").
 3
 4%% Basic Worker Pool Test.
 5pool_start_stop_test() ->
 6    {ok, Pid} = hottub:start_link(ss_pool, 1, test_worker, start_link, []),
 7    ok = hottub:stop(ss_pool),
 8    ?assertEqual(false, is_process_alive(Pid)),
 9    ok.
10
11pool_dead_worker_test() ->
12    hottub:start_link(dead_pool, 1, test_worker, start_link, []),
13    Pid = self(),
14    BlockFun = fun() ->
15        hottub:execute(dead_pool, fun(Worker) ->
16            Pid ! waiting,
17            receive
18                continue ->
19                    test_worker:crash(Worker)
20            end
21        end)
22    end,
23    BlockedFun = fun() ->
24        Pid ! waiting,
25        hottub:execute(dead_pool, fun(Worker) ->
26            case is_process_alive(Worker) of
27                true ->
28                    Pid ! ok;
29                false ->
30                    Pid ! fail
31            end
32        end)
33    end,
34    Blocker = spawn(BlockFun),
35    receive
36        waiting ->
37            ok
38    end,
39    spawn(BlockedFun),
40    receive 
41        waiting ->
42            ok
43    end,
44    Blocker ! continue,
45    receive
46        ok ->
47            ok;
48        fail ->
49            ?assert(false)
50    end.
51
52pool_crash_test() ->
53    hottub:start_link(test_pool, 1, test_worker, start_link, []),
54    hottub:execute(test_pool,
55        fun(Worker) ->
56            ?assert(is_pid(Worker)),
57            test_worker:crash(Worker)
58        end),
59    hottub:execute(test_pool,
60        fun(Worker) ->
61            ?assert(is_pid(Worker))
62        end),
63    hottub:stop(test_pool),
64    ok.
65
66%% Benchmark Pool Checkout/Checkin Test.
67pool_benchmark_test_() ->
68    {timeout, 120, ?_assertEqual(ok, begin benchmark() end)}.
69
70benchmark() ->
71    NWorkers = 500,
72    hottub:start_link(bench_pool, 100, test_worker, start_link, []),
73    BenchFun = fun() ->
74        hottub:execute(bench_pool,
75            fun(Worker) ->
76                test_worker:nothing(Worker)
77            end)
78    end,
79    BenchWorkers = lists:map(
80        fun(Id) ->
81            {ok, Pid} = benchmark:start_link(Id),
82            benchmark:perform(Pid, BenchFun, 1000),
83            Pid
84    end, lists:seq(0, NWorkers)),
85    {Min, Max, AvgSum} = lists:foldl(
86        fun(Pid, {Min, Max, AvgSum}) ->
87            {RMin, RMax, RAvg} = benchmark:results(Pid),
88            {min(RMin, Min), max(RMax, Max), AvgSum + RAvg}
89        end, {10000000000, 0, 0}, BenchWorkers),
90    Mean = AvgSum/NWorkers,
91    hottub:stop(bench_pool),
92    io:format(user, "Worker Execute Results: Min ~pms, Max ~pms, Mean ~pms~n", [Min, Max, Mean]),
93    ok.