|
6 | 6 |
|
7 | 7 | -record(state, { |
8 | 8 | host, |
9 | | - port |
| 9 | + port, |
| 10 | + keep_connect, |
| 11 | + batch_size, |
| 12 | + socket |
10 | 13 | }). |
11 | 14 |
|
12 | 15 | new(_Id) -> |
13 | 16 | Host = basho_bench_config:get(carbon_server, "127.0.0.1"), |
14 | 17 | Port = basho_bench_config:get(carbon_port, 2003), |
15 | | - {ok, #state{host=Host, port=Port}}. |
| 18 | + KeepConnect = basho_bench_config:get(carbon_keep_connect, false), |
| 19 | + BatchSize = basho_bench_config:get(carbon_batch_size, 100), |
| 20 | + {ok, #state{ |
| 21 | + host=Host, |
| 22 | + port=Port, |
| 23 | + keep_connect=KeepConnect, |
| 24 | + batch_size=BatchSize, |
| 25 | + socket=nil}}. |
16 | 26 |
|
17 | | -run(set, KeyGen, _ValueGen, State) -> |
18 | | - case gen_tcp:connect(State#state.host, |
19 | | - State#state.port, |
20 | | - [list, {packet, 0}]) of |
| 27 | +carbon(State = #state{host=Host, port=Port, socket=nil}, Message) -> |
| 28 | + case gen_tcp:connect(Host, Port, [list, {packet, 0}]) of |
21 | 29 | {ok, Sock} -> |
22 | | - {Mega, Sec, _Micro} = now(), |
23 | | - Msg = io_lib:format("pim.pam.poum.~p ~p ~p~n", [KeyGen(), (Mega * 1000 + Sec), random:uniform(1000)]), |
24 | | - gen_tcp:send(Sock, Msg), |
25 | | - gen_tcp:close(Sock), |
26 | | - {ok, State}; |
| 30 | + carbon(State#state{socket=Sock}, Message); |
27 | 31 | Error -> |
28 | | - {error, Error, State} |
| 32 | + Error |
| 33 | + end; |
| 34 | + |
| 35 | +carbon(State = #state{socket=Socket, keep_connect=KeepConnect}, Message) -> |
| 36 | + case gen_tcp:send(Socket, Message) of |
| 37 | + ok -> |
| 38 | + case KeepConnect of |
| 39 | + true -> |
| 40 | + {ok, State}; |
| 41 | + _ -> |
| 42 | + ok = gen_tcp:close(Socket), |
| 43 | + {ok, State#state{socket=nil}} |
| 44 | + end; |
| 45 | + Error -> |
| 46 | + Error % let it crash |
| 47 | + end. |
| 48 | + |
| 49 | +concat(0, _Keygen, _Ts, List) -> |
| 50 | + List; |
| 51 | + |
| 52 | +concat(Size, KeyGen, Ts, List) -> |
| 53 | + Msg = io_lib:format("pim.pam.poum.~p ~p ~p~n", [KeyGen(), Ts, random:uniform(1000)]), |
| 54 | + concat(Size -1, KeyGen, Ts, [Msg | List]). |
| 55 | + |
| 56 | +run(set, KeyGen, _ValueGen, State = #state{batch_size=BatchSize}) -> |
| 57 | + {Mega, Sec, _Micro} = now(), |
| 58 | + Msg = concat(BatchSize, KeyGen, Mega * 1000 + Sec, []), |
| 59 | + |
| 60 | + case carbon(State, Msg) of |
| 61 | + {error, E} -> |
| 62 | + {error, E, State}; |
| 63 | + OK -> |
| 64 | + OK |
29 | 65 | end. |
30 | 66 |
|
0 commit comments