diff --git a/jpos/src/main/java/org/jpos/util/ThroughputControl.java b/jpos/src/main/java/org/jpos/util/ThroughputControl.java index 4812ed3567..4689729b38 100644 --- a/jpos/src/main/java/org/jpos/util/ThroughputControl.java +++ b/jpos/src/main/java/org/jpos/util/ThroughputControl.java @@ -18,7 +18,7 @@ package org.jpos.util; -import java.time.Instant; +import java.time.Duration; /** * ThroughputControl can be used to limit the throughput @@ -29,8 +29,8 @@ public class ThroughputControl { private int[] period; private int[] max; private int[] cnt; - private long[] start; - private long[] sleep; + private Duration[] start; + private Duration[] sleep; /** * @param maxTransactions ditto @@ -50,13 +50,13 @@ public ThroughputControl (int[] maxTransactions, int[] periodInMillis) { period = new int[l]; max = new int[l]; cnt = new int[l]; - start = new long[l]; - sleep = new long[l]; + start = new Duration[l]; + sleep = new Duration[l]; for (int i=0; i max[i]) { delayed = true; try { - Thread.sleep (sleep[i]); + Thread.sleep (sleep[i].toMillis()); } catch (InterruptedException e) { } } synchronized (this) { - long now = Instant.now().toEpochMilli(); - if (now - start[i] > period[i]) { - long elapsed = now - start[i]; + Duration now = Duration.ofNanos(System.nanoTime()); + if (now.minus(start[i]).toMillis() > period[i]) { + long elapsed = now.minus(start[i]).toMillis(); int allowed = (int) (elapsed * max[i] / period[i]); start[i] = now; cnt[i] = Math.max (cnt[i] - allowed, 0); @@ -91,7 +91,7 @@ public long control() { } } while (cnt[i] > max[i]); } - return delayed ? Instant.now().toEpochMilli() - init : 0L; + return delayed ? Duration.ofNanos(System.nanoTime()).minus(init).toMillis() : 0L; } } diff --git a/jpos/src/test/java/org/jpos/util/ThroughputControlTestCase.java b/jpos/src/test/java/org/jpos/util/ThroughputControlTestCase.java index 3621935b2a..8a1928080a 100644 --- a/jpos/src/test/java/org/jpos/util/ThroughputControlTestCase.java +++ b/jpos/src/test/java/org/jpos/util/ThroughputControlTestCase.java @@ -23,43 +23,42 @@ import org.junit.jupiter.api.Test; import java.time.Duration; -import java.time.Instant; public class ThroughputControlTestCase { @Test public void testSingleThread () throws Exception { ThroughputControl tc = new ThroughputControl (2, 1000); - Instant start = Instant.now(); + Duration start = Duration.ofNanos(System.nanoTime()); assertTrue (tc.control() == 0L, "Control should return 0L"); assertTrue ( - Duration.between(start, Instant.now()).toMillis() < 1000L, + Duration.ofNanos(System.nanoTime()).minus(start).toMillis() < 1000L, "Elapsed time should be less than one second" ); tc.control(); assertTrue ( - Duration.between(start, Instant.now()).toMillis() < 1000L, + Duration.ofNanos(System.nanoTime()).minus(start).toMillis() < 1000L, "Elapsed time should still be less than one second" ); tc.control(); assertTrue ( - Duration.between(start, Instant.now()).toMillis() > 1000L, + Duration.ofNanos(System.nanoTime()).minus(start).toMillis() > 1000L, "Elapsed time should be greater than one second" ); tc.control(); assertTrue ( - Duration.between(start, Instant.now()).toMillis() < 2000L, + Duration.ofNanos(System.nanoTime()).minus(start).toMillis() < 2000L, "second transaction should be less than two seconds" ); } @Test public void testFifty () throws Exception { ThroughputControl tc = new ThroughputControl (10, 1000); - Instant start = Instant.now(); + Duration start = Duration.ofNanos(System.nanoTime()); for (int i=0; i<50; i++) tc.control(); - long elapsed = Duration.between(start, Instant.now()).toMillis(); + long elapsed = Duration.ofNanos(System.nanoTime()).minus(start).toMillis(); assertTrue ( elapsed >= 4000L, "50 transactions should take at least 4 seconds but took " + elapsed @@ -75,11 +74,11 @@ public void testDualPeriod () throws Exception { new int[] { 100, 150 }, new int[] { 1000, 5000 } ); - Instant start = Instant.now(); + Duration start = Duration.ofNanos(System.nanoTime()); for (int i=0; i<100; i++) tc.control(); - long elapsed = Duration.between(start, Instant.now()).toMillis(); + long elapsed = Duration.ofNanos(System.nanoTime()).minus(start).toMillis(); assertTrue ( elapsed <= 1000L, "100 initial transactions should take more than about one second but took " + elapsed @@ -87,7 +86,7 @@ public void testDualPeriod () throws Exception { for (int i=0; i<100; i++) tc.control(); - elapsed = Duration.between(start, Instant.now()).toMillis(); + elapsed = Duration.ofNanos(System.nanoTime()).minus(start).toMillis(); assertTrue ( elapsed >= 5000L, "100 additional transactions should take more than five seconds but took " + elapsed @@ -96,7 +95,7 @@ public void testDualPeriod () throws Exception { @Test public void testMultiThread() throws Exception { final ThroughputControl tc = new ThroughputControl (2, 1000); - Instant start = Instant.now(); + Duration start = Duration.ofNanos(System.nanoTime()); Thread[] t = new Thread[10]; for (int i=0; i<10; i++) { t[i] = new Thread() { @@ -109,7 +108,7 @@ public void run() { for (int i=0; i<10; i++) { t[i].join(); } - long elapsed = Duration.between(start, Instant.now()).toMillis(); + long elapsed = Duration.ofNanos(System.nanoTime()).minus(start).toMillis(); assertTrue ( elapsed > 4000L && elapsed < 5000L, "10 transactions should take about four seconds but took " + elapsed