Skip to content

Commit

Permalink
Merge pull request #179 from lafirest/fix/pub_rate
Browse files Browse the repository at this point in the history
fix: fix that the publish rate does not respect the setting
  • Loading branch information
lafirest authored Jul 6, 2022
2 parents 5a43f75 + 95b308e commit c31c6f2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/emqtt_bench.erl
Original file line number Diff line number Diff line change
Expand Up @@ -645,18 +645,25 @@ loop(Parent, N, Client, PubSub, Opts) ->
end.

put_publish_begin_time() ->
NowT = erlang:monotonic_time(millisecond),
put(last_publish_ts, NowT),
case get(last_publish_ts) of
undefined ->
NowT = erlang:monotonic_time(millisecond),
put(last_publish_ts, NowT);
_ ->
ok
end,
ok.

next_publish(Opts) ->
inc_counter(pub),
BeginTime = get(last_publish_ts),
PubCnt = get_counter(pub),
Interval = proplists:get_value(interval_of_msg, Opts),
LastT = get(last_publish_ts),
NextTime = BeginTime + PubCnt * Interval,
NowT = erlang:monotonic_time(millisecond),
Spent = NowT - LastT,
Remain = Interval - Spent,
Remain = NextTime - NowT,
Interval > 0 andalso Remain < 0 andalso inc_counter(pub_overrun),
inc_counter(pub),

case Remain > 0 of
true -> _ = erlang:send_after(Remain, self(), publish);
false -> self() ! publish
Expand Down

0 comments on commit c31c6f2

Please sign in to comment.