From 95b308ea4130d56dc1c88965baf921f6e7034eb6 Mon Sep 17 00:00:00 2001 From: firest Date: Tue, 5 Jul 2022 17:35:50 +0800 Subject: [PATCH] fix: fix that the publish rate does not respect the setting --- src/emqtt_bench.erl | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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