Skip to content

Commit

Permalink
feat: support %i vars as username opt
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Sep 10, 2024
1 parent f02d86a commit 98fdf4e
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions src/emqtt_bench.erl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
{interval, $i, "interval", {integer, 10},
"interval of connecting to the broker"},
{username, $u, "username", string,
"username for connecting to server"},
"username for connecting to server, support %i, %(i/2), %(rand(100)) variables"},
{password, $P, "password", string,
"password for connecting to server"},
{keepalive, $k, "keepalive", {integer, 300},
Expand Down Expand Up @@ -275,6 +275,25 @@
}
]).

-define(COUNTER_NAMES,
[ publish_latency
, recv
, sub
, sub_fail
, pub
, pub_fail
, pub_overrun
, pub_succ
, connect_succ
, connect_fail
, connect_retried
, reconnect_succ
, unreachable
, connection_refused
, connection_timeout
, connection_idle
]).

-define(cnt_map, cnt_map).
-define(hdr_cnt64, "cnt64").
-define(hdr_ts, "ts").
Expand Down Expand Up @@ -590,6 +609,7 @@ run(Parent, N, PubSub, Opts0, AddrList, HostList) ->
run(Parent, N-1, PubSub, Opts, AddrList, HostList).

connect(Parent, N, PubSub, Opts) ->
_ = erlang:put(conn_id, N),
process_flag(trap_exit, true),
rand:seed(exsplus, erlang:timestamp()),
MRef = case proplists:get_value(publish_signal_pid, Opts) of
Expand Down Expand Up @@ -892,8 +912,9 @@ mqtt_opts([{version, 4}|Opts], Acc) ->
mqtt_opts(Opts, [{proto_ver, v4}|Acc]);
mqtt_opts([{version, 5}|Opts], Acc) ->
mqtt_opts(Opts, [{proto_ver, v5}|Acc]);
mqtt_opts([{username, Username}|Opts], Acc) ->
mqtt_opts(Opts, [{username, list_to_binary(Username)}|Acc]);
mqtt_opts([{username, Username0}|Opts], Acc) ->
Username = parse_expr(Username0),
mqtt_opts(Opts, [{username, bin(Username)}|Acc]);
mqtt_opts([{password, Password}|Opts], Acc) ->
mqtt_opts(Opts, [{password, list_to_binary(Password)}|Acc]);
mqtt_opts([{keepalive, I}|Opts], Acc) ->
Expand Down Expand Up @@ -921,6 +942,31 @@ mqtt_opts([{reconnect, Reconnect}|Opts], Acc) ->
mqtt_opts([_|Opts], Acc) ->
mqtt_opts(Opts, Acc).

eval_expr("random(" ++ Str) ->
case lists:last(Str) of
$) ->
Int = strip_last_char(Str),
rand:uniform(list_to_integer(Int));
_ -> Str
end.

parse_expr("%i") ->
erlang:get(conn_id);
parse_expr("%(i-div-2)") ->
erlang:get(conn_id) div 2;
parse_expr("%(" ++ Str) ->
case lists:last(Str) of
$) ->
eval_expr(strip_last_char(Str));
_ ->
throw({invalid_expression, "%(" ++ Str})
end;
parse_expr(Str) ->
Str.

strip_last_char(Str) ->
lists:sublist(Str, 1, length(Str) - 1).

tcp_opts(Opts) ->
tcp_opts(Opts, []).
tcp_opts([], Acc) ->
Expand Down Expand Up @@ -1188,23 +1234,7 @@ prepare_for_quic(Opts)->

-spec counters() -> {atom(), integer()}.
counters() ->
Names = [ publish_latency
, recv
, sub
, sub_fail
, pub
, pub_fail
, pub_overrun
, pub_succ
, connect_succ
, connect_fail
, connect_retried
, reconnect_succ
, unreachable
, connection_refused
, connection_timeout
, connection_idle
],
Names = ?COUNTER_NAMES,
Idxs = lists:seq(2, length(Names) + 1),
lists:zip(Names, Idxs).

Expand Down

0 comments on commit 98fdf4e

Please sign in to comment.