From 303adb3117b3039aa4b0dbc91540627d06bcb07c Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Mon, 22 Jun 2015 14:07:33 +0200 Subject: [PATCH 1/2] Replace System.currentTimeMillis() by System.nanoTime() - System.nanoTime() is the best way to measure elapsed time in Java. - It gives a resolution on the order of microseconds The System.currentTimeMillis() is used when calculating absolut time. --- utils/src/com/cloud/utils/Profiler.java | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/utils/src/com/cloud/utils/Profiler.java b/utils/src/com/cloud/utils/Profiler.java index e75da0efc574..6082a92157d6 100644 --- a/utils/src/com/cloud/utils/Profiler.java +++ b/utils/src/com/cloud/utils/Profiler.java @@ -23,24 +23,20 @@ public class Profiler { private Long startTickInMs; private Long stopTickInMs; - public Profiler() { - startTickInMs = null; - stopTickInMs = null; - } - public long start() { - startTickInMs = System.currentTimeMillis(); + startTickInMs = System.nanoTime(); return startTickInMs.longValue(); } public long stop() { - stopTickInMs = System.currentTimeMillis(); + stopTickInMs = System.nanoTime(); return stopTickInMs.longValue(); } public long getDuration() { - if (startTickInMs != null && stopTickInMs != null) + if (startTickInMs != null && stopTickInMs != null) { return stopTickInMs.longValue() - startTickInMs.longValue(); + } return -1; } @@ -55,12 +51,14 @@ public boolean isStopped() { @Override public String toString() { - if (startTickInMs == null) + if (startTickInMs == null) { return "Not Started"; + } - if (stopTickInMs == null) + if (stopTickInMs == null) { return "Started but not stopped"; + } return "Done. Duration: " + getDuration() + "ms"; } -} +} \ No newline at end of file From b8d46ff4f78f5996c68d86477ec894fea636e866 Mon Sep 17 00:00:00 2001 From: wilderrodrigues Date: Mon, 22 Jun 2015 16:05:15 +0200 Subject: [PATCH 2/2] Add unit tests to cover negative cases - Cover when the profile is not started/stopped --- utils/test/com/cloud/utils/TestProfiler.java | 32 ++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/utils/test/com/cloud/utils/TestProfiler.java b/utils/test/com/cloud/utils/TestProfiler.java index 2690a262e76e..4323f1d3c717 100644 --- a/utils/test/com/cloud/utils/TestProfiler.java +++ b/utils/test/com/cloud/utils/TestProfiler.java @@ -32,11 +32,11 @@ public class TestProfiler extends Log4jEnabledTestCase { public void testProfiler() { s_logger.info("testProfiler() started"); - Profiler pf = new Profiler(); + final Profiler pf = new Profiler(); pf.start(); try { Thread.sleep(1000); - } catch (InterruptedException e) { + } catch (final InterruptedException e) { } pf.stop(); @@ -46,4 +46,30 @@ public void testProfiler() { s_logger.info("testProfiler() stopped"); } -} + + @Test + public void testProfilerNoStart() { + final Profiler pf = new Profiler(); + try { + Thread.sleep(20); + } catch (final InterruptedException e) { + } + pf.stop(); + + Assert.assertTrue(pf.getDuration() == -1); + Assert.assertFalse(pf.isStarted()); + } + + @Test + public void testProfilerNoStop() { + final Profiler pf = new Profiler(); + pf.start(); + try { + Thread.sleep(20); + } catch (final InterruptedException e) { + } + + Assert.assertTrue(pf.getDuration() == -1); + Assert.assertFalse(pf.isStopped()); + } +} \ No newline at end of file