FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace System.currentTimeMillis() by System.nanoTime() by wilderrodrigues · Pull Request #509 · apache/cloudstack · GitHub

Repository navigation

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

Filter by extension

Filter by extension .java  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 9 additions & 11 deletions utils/src/com/cloud/utils/Profiler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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:

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

Cheers,
Wilder

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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());
}
}

Back | FazBrowse Home | New Git URL