diff options
author | Eric Wong <e@80x24.org> | 2015-02-27 22:35:02 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2015-02-27 22:35:02 +0000 |
commit | e3428444e87533ed03fc644d1b97356ae9112845 (patch) | |
tree | 0d846f2865b64364be820abaa5539a3c82fce8a7 | |
parent | 5aea6f54743a6dd6cd58a2a1660d36f112190a9b (diff) | |
download | clogger-e3428444e87533ed03fc644d1b97356ae9112845.tar.gz |
Ruby 2.1.0 and later exposes Process.clock_gettime, resulting in less garbage and access to the monotonic clock if it is available. Try to use it to achieve feature parity with the C extension (which has always used the monotonic clock if possible).
-rw-r--r-- | lib/clogger/pure.rb | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb index 9efb00c..5e7ccfe 100644 --- a/lib/clogger/pure.rb +++ b/lib/clogger/pure.rb @@ -28,7 +28,7 @@ class Clogger end def call(env) - start = Time.now + start = mono_now resp = @app.call(env) unless resp.instance_of?(Array) && resp.size == 3 log(env, 500, {}, start) @@ -165,7 +165,7 @@ private when OP_TIME_LOCAL; Time.now.strftime(op[1]) when OP_TIME_UTC; Time.now.utc.strftime(op[1]) when OP_REQUEST_TIME - t = Time.now - start + t = mono_now - start time_format(t.to_i, (t - t.to_i) * 1000000, op[1], op[2]) when OP_TIME t = Time.now @@ -185,4 +185,14 @@ private end nil end + + # favor monotonic clock if possible, and try to use clock_gettime in + # more recent Rubies since it generates less garbage + if defined?(Process::CLOCK_MONOTONIC) + def mono_now; Process.clock_gettime(Process::CLOCK_MONOTONIC); end + elsif defined?(Process::CLOCK_REALTIME) + def mono_now; Process.clock_gettime(Process::CLOCK_REALTIME); end + else + def mono_now; Time.now.to_f; end + end end |