Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should we use System.nanoTime() instead of System.currentTimeMillis() for tracking the request time? #2452

Closed
costimuraru opened this issue Apr 17, 2018 · 3 comments
Labels
Enhancement Low Priority Stale For auto-closed stale issues and pull requests

Comments

@costimuraru
Copy link

costimuraru commented Apr 17, 2018

I noticed Jetty provides a handy method to retrieve Request.getTimeStamp() (the start time for when the request was received). This is very helpful because by giving us access to this timestamp, we can compute QoS metrics (how much time we spent processing the request) and log those in our system (e.g. by using a RequestLog [1]).
I noticed that Jetty uses System.currentTimeMillis() for setting this timestamp, which may be problematic. [2]
For instance, see this from Android:

System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock.
[3]

What do you think about using System.nanoTime() instead of System.currentTimeMillis()?

If you're just looking for extremely precise measurements of elapsed time, use System.nanoTime()
[4]

[1] https://stackoverflow.com/questions/33493960/how-to-track-the-http-request-and-response-time-in-jetty
[2] https://github.com/eclipse/jetty.project/blob/jetty-9.4.x/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java#L632
[3] https://developer.android.com/reference/android/os/SystemClock.html
[4] https://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime

@sbordet
Copy link
Contributor

sbordet commented Apr 17, 2018

See #121.

The problem is that for certain things you really want the GMT time, not the number that System.nanoTime() returns, because you don't want to compute deltas, but just know the instant in time.

We should also update to java.time, but it's some work.

@stale
Copy link

stale bot commented Nov 20, 2019

This issue has been automatically marked as stale because it has been a full year without activit. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale For auto-closed stale issues and pull requests label Nov 20, 2019
@sbordet
Copy link
Contributor

sbordet commented Nov 21, 2019

Closing in favor of #121.

@sbordet sbordet closed this as completed Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Low Priority Stale For auto-closed stale issues and pull requests
Projects
None yet
Development

No branches or pull requests

3 participants