Created
November 18, 2014 13:10
-
-
Save odo/6c59f604fda6390d034a to your computer and use it in GitHub Desktop.
Example session module for ponos (https://github.com/klarna/ponos).
This file contains 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(ponos_session). | |
-behaviour(gen_server). | |
-export([start/2, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). | |
-record(state, {tasks :: list(), task_state :: any() }). | |
%% == usage == | |
%% 1> ponos_session:start([{print, "Hello"}, {wait, 1000}, {print, "World"}, print_call_count], 0). | |
%% "Hello" | |
%% {ok,<0.43.0>} | |
%% "World" | |
%% Call count: 3 | |
%% == from ponos == | |
%% StartSession = fun() -> ponos_session:start([{print, "Hello"}, {wait, 1000}, {print, "World"}, print_call_count], 0) end. | |
%% Name = session_test. | |
%% LoadSpec = ponos_load_specs:make_constant(10.0). | |
%% Options = [{duration, 10 * 1000}, {auto_init, false}]. | |
%% Args = [ {name, Name}, {task, StartSession}, {load_spec,LoadSpec}, {options, Options}]. | |
%% [ok] = ponos:add_load_generators([Args]). | |
%% ponos:init_load_generators([Name]). | |
%% Public API | |
start(Tasks, InitState) -> | |
gen_server:start_link(?MODULE, [Tasks, InitState], []). | |
%% Callbacks | |
init([Tasks, InitState]) -> | |
self() ! next_task, | |
{ok, #state{ task_state = InitState, tasks = Tasks}}. | |
handle_call(_Request, _From, State) -> | |
{reply, ok, State}. | |
handle_cast(_Msg, State) -> | |
{noreply, State}. | |
handle_info(next_task, #state{task_state = TaskState, tasks = [NextTask | Tasks]}) -> | |
NewTaskState = handle_task(NextTask, TaskState), | |
NewState = #state{task_state = NewTaskState, tasks = Tasks}, | |
case Tasks of | |
[] -> | |
{stop, normal, NewState}; | |
_ -> | |
self() ! next_task, | |
{noreply, #state{task_state = NewTaskState, tasks = Tasks}} | |
end. | |
terminate(_Reason, _State) -> | |
ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. | |
handle_task({wait, Millisecords}, TaskState) -> | |
timer:sleep(Millisecords), | |
TaskState + 1; | |
handle_task({print, Data}, TaskState) -> | |
io:format("~p\n", [Data]), | |
TaskState + 1; | |
handle_task(print_call_count, TaskState) -> | |
io:format("Call count: ~p\n", [TaskState]), | |
TaskState + 1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment