3

What is the starting time for process.hrtime (node.js version of microTime)? I couldn't find any event that starts it. Is the timestamp of the start saved anywhere?

I need my server to be able to get latency to client both ways and for that, I need reliable microtime measure. (+ for some other things)

Anagmate
  • 71
  • 1
  • 4

1 Answers1

5

The starting time is arbitrary; its actual value alone is meaningless. Call hrtime when you want to start the stopwatch, and call it again when your operation is done. Subtract the former from the latter and you have the elapsed time.

process.hrtime()

Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array. It is relative to an arbitrary time in the past. It is not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals.

You may pass in the result of a previous call to process.hrtime() to get a diff reading, useful for benchmarks and measuring intervals:

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • Is the start time saved anywhere? – Anagmate Jul 30 '14 at 00:47
  • 1
    `process.uptime()` gives you the number of seconds the process has been running, which you could use to determine its start time. Why does it matter anyway? If you're just trying to measure client latency, you call `hrtime` twice, and absolute time doesn't matter. – josh3736 Jul 30 '14 at 00:52
  • Problem is that client is pinging server and I want to know separately the time from client to server and from server to client (these can be different). – Anagmate Jul 30 '14 at 01:55
  • Another problem is, that hrtime doesn't start at process start. Just after process start hrtime already returned cca. 32000 second. – Anagmate Jul 30 '14 at 01:59
  • `hrtime` is *relative to an **arbitrary** time in the past*. Its absolute value is meaningless; only the difference between two calls has meaning. Also, it is [mathematically impossible](http://cs.stackexchange.com/a/28581/22721) to measure one-way latency. – josh3736 Oct 14 '14 at 21:53