diff --git a/src/emqtt_bench.erl b/src/emqtt_bench.erl index e06fb9d..01b4fc0 100644 --- a/src/emqtt_bench.erl +++ b/src/emqtt_bench.erl @@ -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