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

Use faster way to get sec and nsec #2557

Merged
merged 4 commits into from
Aug 16, 2019

Conversation

ganmacs
Copy link
Member

@ganmacs ganmacs commented Aug 13, 2019

Which issue(s) this PR fixes:
no

What this PR does / why we need it:

Process.clock_gettime is faster than Time.now if getting sec and nsec.
and Time.now is called in EventTime.now.
EventTime.now is called per data in parser_none and per chunk creation. so it's better to replace them with faster version's method.

def now1
  now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
  sec = now / 1_000_000_000
  nsec = now % 1_000_000_000
  [sec, nsec]
end

def now2
  t = Time.now
  sec = t.to_i
  nsec = t.nsec
  [sec, nsec]
end

require 'benchmark/ips'

Benchmark.ips do |x|
  x.report("Time.now") do
    now2
  end

  x.report("Process.clock_gettime") do
    now1
  end
end
Warming up --------------------------------------
            Time.now   191.241k i/100ms
Process.clock_gettime
                       291.915k i/100ms
Calculating -------------------------------------
            Time.now      2.956M (± 1.9%) i/s -     14.917M in   5.047422s
Process.clock_gettime
                          5.563M (± 4.5%) i/s -     28.024M in   5.047899s

Docs Changes:

no need

Release Note:

same as title

it's faster than Time.now

Signed-off-by: Yuta Iwama <[email protected]>
@ganmacs ganmacs force-pushed the use-process-get-clock branch from 6076a3e to 55bc09d Compare August 14, 2019 01:52
@ganmacs
Copy link
Member Author

ganmacs commented Aug 14, 2019

memo: timecop's issue. travisjeffery/timecop#220

@ganmacs ganmacs force-pushed the use-process-get-clock branch from 55bc09d to 191a2ea Compare August 14, 2019 03:16
@ganmacs ganmacs added the enhancement Feature request or improve operations label Aug 14, 2019
@@ -111,7 +111,8 @@ def self.eq?(a, b)
end

def self.now
from_time(Time.now)
now = Fluent::Clock.real_now(:nanosecond)
Copy link
Member

@repeatedly repeatedly Aug 14, 2019

Choose a reason for hiding this comment

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

EventTime.now is called frequently so how about call Process.clock_gettime directly to reduce function call?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed 809af50

…ly instead of Fluent::Clock.real_now

Signed-off-by: Yuta Iwama <[email protected]>
@ganmacs ganmacs force-pushed the use-process-get-clock branch from 191a2ea to 809af50 Compare August 15, 2019 02:17
@repeatedly repeatedly merged commit 2110b44 into fluent:master Aug 16, 2019
@ganmacs ganmacs deleted the use-process-get-clock branch August 16, 2019 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request or improve operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants