Skip to content

Instantly share code, notes, and snippets.

@HJianBo
Created November 16, 2022 09:32
Show Gist options
  • Save HJianBo/a7f555e99025702eda0f41da88afad33 to your computer and use it in GitHub Desktop.
Save HJianBo/a7f555e99025702eda0f41da88afad33 to your computer and use it in GitHub Desktop.
EMQX 5.0: Generate lots of clients
%% Generate a lot of clients for emqx 5.0
-module(emqx_clients_gen).
-compile(export_all).
-compile(nowarn_export_all).
-define(LOG(Fmt, Args), io:format(Fmt, Args)).
%%--------------------------------------------------------------------
%% APIs
%%--------------------------------------------------------------------
%% @doc fullfill emqx_channel_info
%%
fullfill_channel_info(N) ->
do_fullfill_channel_info(list_to_binary(emqx_misc:gen_id(8)), N).
fullfill_channel_info(Tenant, N) ->
do_fullfill_channel_info(Tenant, N).
do_fullfill_channel_info(_Prefix, 0) ->
done;
do_fullfill_channel_info(Prefix, N) ->
{Chan, Info, Stats} = random_client(Prefix, N),
true = ets:insert(emqx_channel_info, {Chan, Info, Stats}),
do_fullfill_channel_info(Prefix, N - 1).
%%--------------------------------------------------------------------
%% helpers
%%--------------------------------------------------------------------
random_client(Prefix, N) ->
Pid = pid(Prefix, N),
TenantId = tenantid(Prefix, N),
ClientId = clientid(Prefix, N),
Username = username(Prefix, N),
%NClientId = emqx_clientid:with_tenant(TenantId, ClientId),
NClientId = ClientId,
Chan = {NClientId, Pid},
{Chan, info(TenantId, ClientId, Username), stats()}.
info(TenantId, ClientId, Username) ->
#{clientinfo => clientinfo(TenantId, ClientId, Username),
conn_state => connected,
conninfo => conninfo(TenantId, ClientId, Username),
sockinfo => sockinfo(),
will_msg => willmsg()
}.
clientid(Prefix, N) ->
iolist_to_binary([Prefix, "-", integer_to_binary(N)]).
username(_Prefix, N) ->
iolist_to_binary(["user-", integer_to_binary(N)]).
tenantid(Prefix, _N) ->
Prefix.
pid(_Prefix, _N) ->
list_to_pid(lists:append(["<0.",
integer_to_list(rand:uniform(6000)),
".",
integer_to_list(rand:uniform(6000)),
">"
])).
clientinfo(TenantId, ClientId, Username) ->
%NClientId = emqx_clientid:with_tenant(TenantId, ClientId),
NClientId = ClientId,
MP = case TenantId of
<<>> -> undefined;
_ -> <<"$tenants/", TenantId/binary, "/">>
end,
#{clientid => NClientId,enable_authn => true,
is_bridge => false,is_superuser => false,
listener => 'ws:default',mountpoint => MP,
peerhost => {127,0,0,1},
protocol => mqtt,sockport => 8083,tenant_id => TenantId,
username => Username,ws_cookie => [],zone => default
}.
conninfo(Peersni, ClientId, Username) ->
NPeersni = case Peersni of
<<>> -> undefined;
_ -> Peersni
end,
#{clean_start => true,clientid => ClientId,
conn_mod => emqx_ws_connection,conn_props => #{},
connected_at => 1668044660747,expiry_interval => 0,
keepalive => 60,peercert => undefined,
peername => {{127,0,0,1},50460},
peersni => NPeersni,proto_name => <<"MQTT">>,
proto_ver => 5,receive_maximum => 32,
sockname => {{127,0,0,1},8083},
socktype => tcp,
username => Username}.
session() ->
#{await_rel_timeout => 300000,created_at => 1668044660747,
id => <<0,5,237,19,233,121,44,109,244,66,0,0,11,15,0,0>>,
is_persistent => false,retry_interval => 30000,
subscriptions => #{},upgrade_qos => false}.
sockinfo() ->
#{peername => {{127,0,0,1},50460},
sockname => {{127,0,0,1},8083},
sockstate => running,socktype => ws}.
willmsg() ->
undefined.
stats() ->
[{recv_oct,54}, {recv_cnt,13}, {send_oct,45}, {send_cnt,13}, {subscriptions_cnt,0},
{subscriptions_max,infinity}, {inflight_cnt,0}, {inflight_max,32}, {mqueue_len,0},
{mqueue_max,1000}, {mqueue_dropped,0}, {next_pkt_id,1}, {awaiting_rel_cnt,0},
{awaiting_rel_max,100}, {recv_pkt,13}, {recv_msg,0}, {'recv_msg.qos0',0},
{'recv_msg.qos1',0}, {'recv_msg.qos2',0}, {'recv_msg.dropped',0},
{'recv_msg.dropped.await_pubrel_timeout',0}, {send_pkt,13}, {send_msg,0},
{'send_msg.qos0',0}, {'send_msg.qos1',0}, {'send_msg.qos2',0},
{'send_msg.dropped',0}, {'send_msg.dropped.expired',0},
{'send_msg.dropped.queue_full',0}, {'send_msg.dropped.too_large',0}, {mailbox_len,0},
{heap_size,1598}, {total_heap_size,5790}, {reductions,19991}, {memory,47392}].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment