diff --git a/jpos/src/main/java/org/jpos/util/TPS.java b/jpos/src/main/java/org/jpos/util/TPS.java index 40feda038a..271a95ea03 100644 --- a/jpos/src/main/java/org/jpos/util/TPS.java +++ b/jpos/src/main/java/org/jpos/util/TPS.java @@ -49,17 +49,16 @@ @SuppressWarnings("unused") public class TPS implements Loggeable { AtomicInteger count; - Instant start; + Duration start; AtomicLong readings; - int peak; + AtomicInteger peak; Instant peakWhen; - static final long FROM_NANOS = 1000000L; Duration period; float tps; float avg; Timer timer; boolean autoupdate; - protected long simulatedNanoTime = 0L; + protected Duration simulatedNanoTime = Duration.ZERO; public TPS() { this(1000L, false); @@ -78,11 +77,21 @@ public TPS(boolean autoupdate) { * @param autoupdate true to autoupdate */ public TPS(final long period, boolean autoupdate) { + this(Duration.ofMillis(period), autoupdate); + } + + /** + * @param period as a duration + * @param autoupdate true to autoupdate + */ + public TPS(Duration period, boolean autoupdate) { super(); count = new AtomicInteger(0); - start = peakWhen = Instant.now(); + start = Duration.ofNanos(System.nanoTime()); + peak = new AtomicInteger(0); + peakWhen = Instant.now(); readings = new AtomicLong(0L); - this.period = Duration.ofMillis(period); + this.period = period; this.autoupdate = autoupdate; if (autoupdate) { timer = new Timer(); @@ -91,7 +100,7 @@ public TPS(final long period, boolean autoupdate) { public void run() { calcTPS(period); } - }, period, period); + }, period.toMillis(), period.toMillis()); } } @@ -112,7 +121,7 @@ public float getAvg() { } public int getPeak() { - return peak; + return peak.get(); } public long getPeakWhen() { @@ -125,7 +134,7 @@ public long getPeakWhen() { public void reset() { synchronized(this) { avg = 0f; - peak = 0; + peak.set(0); peakWhen = Instant.EPOCH; readings.set(0L); } @@ -136,7 +145,7 @@ public long getPeriod() { } public long getElapsed() { - return Duration.between(start, Instant.now()).toMillis(); + return Duration.ofNanos(System.nanoTime()).minus(start).toMillis(); } public String toString() { @@ -158,14 +167,10 @@ public void dump(PrintStream p, String indent) { p.println(indent + "" : ">") - + this.toString() + + this + ""); } - private float calcTPS(long interval) { - return calcTPS(Duration.ofMillis(interval)); - } - private float calcTPS(Duration interval) { synchronized(this) { tps = (float) period.toNanos() * count.get() / interval.toNanos(); @@ -174,8 +179,8 @@ private float calcTPS(Duration interval) { } long r = readings.getAndIncrement(); avg = (r * avg + tps) / ++r; - if (tps > peak) { - peak = Math.round(tps); + if (tps > peak.get()) { + peak.set(Math.round(tps)); peakWhen = Instant.now(); } count.set(0); @@ -185,8 +190,8 @@ private float calcTPS(Duration interval) { private float calcTPS() { synchronized(this) { - Instant now = Instant.now(); - Duration interval = Duration.between(start, now); + Duration now = Duration.ofNanos(System.nanoTime()); + Duration interval = now.minus(start); if (interval.compareTo(period) >= 0) { calcTPS(interval); start = now; @@ -196,13 +201,13 @@ private float calcTPS() { } public void setSimulatedNanoTime(long simulatedNanoTime) { - if (this.simulatedNanoTime == 0L) - start = Instant.ofEpochMilli(simulatedNanoTime / FROM_NANOS); + if (this.simulatedNanoTime.equals(Duration.ZERO)) + start = Duration.ofNanos(simulatedNanoTime); - this.simulatedNanoTime = simulatedNanoTime; + this.simulatedNanoTime = Duration.ofNanos(simulatedNanoTime); } protected long getNanoTime() { - return simulatedNanoTime > 0L ? simulatedNanoTime : System.nanoTime(); + return simulatedNanoTime.equals(Duration.ZERO) ? Duration.ofNanos(System.nanoTime()).toNanos() : simulatedNanoTime.toNanos(); } } diff --git a/jpos/src/test/java/org/jpos/space/TSpacePerformanceTest.java b/jpos/src/test/java/org/jpos/space/TSpacePerformanceTest.java index 0c096d2adc..693af69cba 100644 --- a/jpos/src/test/java/org/jpos/space/TSpacePerformanceTest.java +++ b/jpos/src/test/java/org/jpos/space/TSpacePerformanceTest.java @@ -22,7 +22,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -40,20 +39,19 @@ * * @author Robert Demski */ -@SuppressWarnings("unchecked") public class TSpacePerformanceTest { LocalSpace sp1; LocalSpace sp2; - List t1 = new ArrayList(); - List t2 = new ArrayList(); + final List t1 = new ArrayList<>(); + final List t2 = new ArrayList<>(); // List t1 = Collections.synchronizedCollection(new ArrayList()); public static final int COUNT = 100000; - TPS tpsOut = new TPS(100L, false); - TPS tpsIn = new TPS(100L, false); + final TPS tpsOut = new TPS(100L, false); + final TPS tpsIn = new TPS(100L, false); class WriteSpaceTask implements Runnable { - String key; + final String key; WriteSpaceTask(String key){ this.key = key; @@ -66,12 +64,12 @@ public void run (){ } long stamp2 = System.nanoTime(); t1.add(stamp2-stamp); - System.err.println("Write "+key+" out: "+(stamp2-stamp)/1000000 + " " + tpsOut.toString()); + System.err.println("Write "+key+" out: "+(stamp2-stamp)/1000000 + " " + tpsOut); } } class ReadSpaceTask implements Runnable { - String key; + final String key; ReadSpaceTask(String key){ this.key = key; @@ -84,18 +82,17 @@ public void run (){ } long stamp2 = System.nanoTime(); t2.add(stamp2-stamp); - System.err.println("Read "+key+" in: "+(stamp2-stamp)/1000000 + " " + tpsIn.toString()); + System.err.println("Read "+key+" in: "+(stamp2-stamp)/1000000 + " " + tpsIn); } } - @SuppressWarnings("unchecked") class WriteSpaceWithNotifyTask implements Runnable, SpaceListener { - String key; - LocalSpace sp1; - LocalSpace sp2; + final String key; + final LocalSpace sp1; + final LocalSpace sp2; int count = 0; - WriteSpaceWithNotifyTask(String key, LocalSpace sp1, LocalSpace sp2){ + WriteSpaceWithNotifyTask(String key, LocalSpace sp1, LocalSpace sp2){ this.key = key; this.sp1 = sp1; this.sp2 = sp2; @@ -118,7 +115,7 @@ public void notify(String key, Object value) { } class WriteSpaceWithNotifyReadTask implements Runnable, SpaceListener { - String key; + final String key; WriteSpaceWithNotifyReadTask(String key){ this.key = key; @@ -136,8 +133,8 @@ public void notify(String key, Object value) { @BeforeEach public void setUp () { - sp1 = new TSpace(); - sp2 = new TSpace(); + sp1 = new TSpace<>(); + sp2 = new TSpace<>(); t1.clear(); t2.clear(); } @@ -154,11 +151,11 @@ private void printAvg(List times, String prefix){ } @Test - public void testReadPerformance() throws Throwable { + public void testReadPerformance() throws InterruptedException { int size = 10; - ExecutorService es = new ThreadPoolExecutor(size, Integer.MAX_VALUE, - 30, TimeUnit.SECONDS, new SynchronousQueue()); - ((ThreadPoolExecutor)es).prestartAllCoreThreads(); + ThreadPoolExecutor es = new ThreadPoolExecutor(size, Integer.MAX_VALUE, + 30, TimeUnit.SECONDS, new SynchronousQueue<>()); + es.prestartAllCoreThreads(); for (int i=0; i()); + es.prestartAllCoreThreads(); for (int i=0; i 0) { + while (es.getActiveCount() > 0) { if (Duration.between(stamp, Instant.now()).toMillis() < 10000){ ISOUtil.sleep(100); continue; @@ -199,16 +198,17 @@ public void testDeadLockWithNotify() throws Throwable { // es.shutdown(); es.shutdownNow(); - es.awaitTermination(5, TimeUnit.SECONDS); + if (!es.awaitTermination(5, TimeUnit.SECONDS)) + fail("Failed to shutdown ThreadPoolExecutor."); } @Disabled("Remove it when TSpace can pass it") @Test - public void testStolenEntryAtNotify() throws Throwable { + public void testStolenEntryAtNotify() throws InterruptedException { int size = 10; - final ExecutorService es = new ThreadPoolExecutor(size*2, Integer.MAX_VALUE, - 30, TimeUnit.SECONDS, new SynchronousQueue()); - ((ThreadPoolExecutor)es).prestartAllCoreThreads(); + final ThreadPoolExecutor es = new ThreadPoolExecutor(size*2, Integer.MAX_VALUE, + 30, TimeUnit.SECONDS, new SynchronousQueue<>()); + es.prestartAllCoreThreads(); for (int i=0; i= 800, "Expected aprox 1000 TPS but was " + tps.intValue()); - assertTrue(tps.intValue() >= 800, "Still expecting aprox 1000 TPS on a second call"); + assertTrue(tps.intValue() >= 800, "Still expecting aprox 1000 TPS on a second call but was " + tps.intValue()); Thread.sleep (2500L - Duration.between(nowDone, Instant.now()).toMillis()); assertEquals( 0, tps.intValue(),