# System.nanoTime in Elastic REST client

**URL:** <https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039>\
**Category:** Elasticsearch\
**Created:** [June 20, 2017, 6:14am UTC](https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039 "2017-06-20T06:14:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaileshaag](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@shaileshaag](https://discuss.elastic.co/u/shaileshaag)\
**Post date:** [June 20, 2017, 6:14am UTC](https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039/1 "2017-06-20T06:14:00Z")

</div>

We noticed that for timing in [ES RestClient](https://github.com/elastic/elasticsearch/blob/master/client/rest/src/main/java/org/elasticsearch/client/RestClient.java) "System.nanoTime()" is being used. We've been bitten by it when we used it in a multi-threaded application hitting this code. Our performance actually degraded. This pushed us back to the usage of "System.currentTimeInMillis()". There are several threads on SFO stating this. One of such links is [this](https://stackoverflow.com/questions/19052316/why-is-system-nanotime-way-slower-in-performance-than-system-currenttimemill).  
Is there a particular reason that "System.nanoTime()" is being used for measuring the time? We are currently having troubles related to the ES Rest client performance and our network team is sure that there is no issue between our server and ES cluster. We were wondering if there is a way to turn-off / toggle away the call to "System.nanoTime()"?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [June 23, 2017, 8:50am UTC](https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039/2 "2017-06-23T08:50:08Z")

</div>

> [@shaileshaag](#):
>
> Is there a particular reason that "System.nanoTime()" is being used for measuring the time?

That's a good question, as you can see from the code its used to measure timeouts amongs other things, so maybe the millis resolution wasn't good enough for this. But it would be a good question to ask on Github again I think. I can open an issue if you like, but maybe you prefer doing so yourself.

> [@shaileshaag](#):
>
> currently having troubles related to the ES Rest client performance and our network team is sure that there is no issue between our server and ES cluster

Does this lead you to believe System.nanoTime() is part of it? If so, how do you currently measure your performance? There's no easy way to switch this off currently since as I mentioned it is used for measuring timeouts. I doubt it is having such a big effect on the client performance though.

---

<div class="post-metadata">

**Author:** ![dakrone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakrone/32/23351_2.png) [@dakrone](https://discuss.elastic.co/u/dakrone)\
**Post date:** [June 23, 2017, 2:13pm UTC](https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039/3 "2017-06-23T14:13:53Z")

</div>

> [@shaileshaag](#):
>
> Is there a particular reason that "System.nanoTime()" is being used for measuring the time?

`System.nanoTime()` is guaranteed by the JVM to be monotonic increasing value, whereas `System.currentTimeInMillies()` can go backwards when the OS clock changes. Because of this, when we are timing the duration of things, we use nanoTime so `endTime - startTime` doesn't end up negative.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 21, 2017, 2:13pm UTC](https://discuss.elastic.co/t/system-nanotime-in-elastic-rest-client/90039/4 "2017-07-21T14:13:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
