From a8d4ca33b5da925416ddc435abbec7a834465436 Mon Sep 17 00:00:00 2001 From: onion <398263861@qq.com> Date: Mon, 20 May 2024 18:47:03 +0800 Subject: [PATCH] Fix the wrong throughput of the ConstantThroughputTimer --- .../jmeter/timers/ConstantThroughputTimer.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/src/main/java/org/apache/jmeter/timers/ConstantThroughputTimer.java b/src/components/src/main/java/org/apache/jmeter/timers/ConstantThroughputTimer.java index 448e0cc2683..db925bbeec1 100644 --- a/src/components/src/main/java/org/apache/jmeter/timers/ConstantThroughputTimer.java +++ b/src/components/src/main/java/org/apache/jmeter/timers/ConstantThroughputTimer.java @@ -57,7 +57,7 @@ public class ConstantThroughputTimer extends AbstractTestElement implements Time private static class ThroughputInfo{ final Object MUTEX = new Object(); - long lastScheduledTime = 0; + double lastScheduledTime = 0.0; } private static final Logger log = LoggerFactory.getLogger(ConstantThroughputTimer.class); private static final AtomicLong PREV_TEST_STARTED = new AtomicLong(0L); @@ -201,7 +201,7 @@ private long calculateDelay() { break; case AllActiveThreads_Shared: // All threads - alternate calculation - delay = calculateSharedDelay(allThreadsInfo,Math.round(msPerRequest)); + delay = calculateSharedDelay(allThreadsInfo, msPerRequest); break; case AllActiveThreadsInCurrentThreadGroup_Shared: //All threads in this group - alternate calculation @@ -212,7 +212,7 @@ private long calculateDelay() { if (groupInfo == null) { groupInfo = threadGroupsInfoMap.computeIfAbsent(key, (k) -> new ThroughputInfo()); } - delay = calculateSharedDelay(groupInfo,Math.round(msPerRequest)); + delay = calculateSharedDelay(groupInfo, msPerRequest); break; case ThisThreadOnly: @@ -223,19 +223,19 @@ private long calculateDelay() { return delay; } - private static long calculateSharedDelay(ThroughputInfo info, long milliSecPerRequest) { - final long now = System.currentTimeMillis(); - final long calculatedDelay; + private static long calculateSharedDelay(ThroughputInfo info, double milliSecPerRequest) { + final double now = (double)System.currentTimeMillis(); + final double calculatedDelay; //Synchronize on the info object's MUTEX to ensure //Multiple threads don't update the scheduled time simultaneously synchronized (info.MUTEX) { - final long nextRequestTime = info.lastScheduledTime + milliSecPerRequest; + final double nextRequestTime = info.lastScheduledTime + milliSecPerRequest; info.lastScheduledTime = Math.max(now, nextRequestTime); calculatedDelay = info.lastScheduledTime - now; } - return Math.max(calculatedDelay, 0); + return Math.round(Math.max(calculatedDelay, 0)); } private void reset() {