member.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %% @doc A pool member used for perf testing pooler. The member has a
  2. %% configurable start-up delay. You set a delay value and actual start
  3. %% delay will be `delay + random:uniform(delay)'. The module supports
  4. %% a crash function to make the member crash.
  5. -module(member).
  6. -behaviour(gen_server).
  7. -define(SERVER, ?MODULE).
  8. %% ------------------------------------------------------------------
  9. %% API Function Exports
  10. %% ------------------------------------------------------------------
  11. -export([start_link/1,
  12. ping/1,
  13. ping_count/1,
  14. crash/1,
  15. stop/1
  16. ]).
  17. %% ------------------------------------------------------------------
  18. %% gen_server Function Exports
  19. %% ------------------------------------------------------------------
  20. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  21. terminate/2, code_change/3]).
  22. %% ------------------------------------------------------------------
  23. %% API Function Definitions
  24. %% ------------------------------------------------------------------
  25. start_link(Config) ->
  26. % not registered
  27. gen_server:start_link(?MODULE, Config, []).
  28. ping(S) ->
  29. gen_server:call(S, ping).
  30. ping_count(S) ->
  31. gen_server:call(S, ping_count).
  32. crash(S) ->
  33. gen_server:cast(S, crash),
  34. sent_crash_request.
  35. stop(S) ->
  36. gen_server:call(S, stop).
  37. %% ------------------------------------------------------------------
  38. %% gen_server Function Definitions
  39. %% ------------------------------------------------------------------
  40. -record(state, {
  41. id,
  42. ping_count = 0
  43. }).
  44. init(Config) ->
  45. start_up_delay(Config),
  46. {ok, #state{id = make_ref()}}.
  47. %% pause server init based on start_up_delay config plus jitter (of up
  48. %% to 2x delay)
  49. start_up_delay(Config) ->
  50. case proplists:get_value(start_up_delay, Config) of
  51. T when is_integer(T) ->
  52. random:seed(erlang:now()),
  53. J = random:uniform(T),
  54. timer:sleep(T + J),
  55. ok;
  56. _ ->
  57. ok
  58. end.
  59. handle_call(ping, _From, #state{ping_count = C } = State) ->
  60. State1 = State#state{ping_count = C + 1},
  61. {reply, pong, State1};
  62. handle_call(ping_count, _From, #state{ping_count = C } = State) ->
  63. {reply, C, State};
  64. handle_call(stop, _From, State) ->
  65. {stop, normal, stop_ok, State};
  66. handle_call(_Request, _From, State) ->
  67. {noreply, ok, State}.
  68. handle_cast(crash, _State) ->
  69. erlang:error({member, requested_crash});
  70. handle_cast(_Msg, State) ->
  71. {noreply, State}.
  72. handle_info(_Info, State) ->
  73. {noreply, State}.
  74. terminate(_Reason, _State) ->
  75. ok.
  76. code_change(_OldVsn, State, _Extra) ->
  77. {ok, State}.