Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions utils/src/com/cloud/utils/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use a new name for the var, like startTickInNanos (and of course stopTickInNanos)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nanoTime() is given in Microseconds. So, the MS still fits in.

Cheers,
Wilder

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasn't the semantics changed as it no longer return milliseconds but now micro-(or even nano-) seconds? (this goes for stop and start as well btw. Did you check the callers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did... and it doesn't change. Only the resolution does,

Quick googled that one here for you: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

Implementation is still the same:

image

http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime

Cheers,
Wilder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and so does the semantics of the return of starts() stop() and getDuration().
I checked; the first two are never cought in a variable but the last one is used in output and to check against settings. There the semantics really changes. It now returns higher numbers then axpected.

}

return -1;
}
Expand All @@ -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";
}
}
}
32 changes: 29 additions & 3 deletions utils/test/com/cloud/utils/TestProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());
}
}