From 9f6637ff35bc0d4d56d824400eafaf4043016a6d Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Fri, 21 Jun 2019 05:24:20 +0900 Subject: [PATCH] time: Add EventTime#to_time for faster Time creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fluent::EventTime can be converted into Time via Time.at but this approach is slower. Output plugins sometimes require Time for client libraries so adding faster method. Warming up -------------------------------------- to_time 171.865k i/100ms Time.at 92.733k i/100ms Calculating ------------------------------------- to_time 2.861M (± 5.1%) i/s - 14.265M in 5.002172s Time.at 1.184M (± 4.1%) i/s - 5.935M in 5.022106s Signed-off-by: Masahiro Nakagawa --- lib/fluent/time.rb | 13 +++++++++++++ test/test_event_time.rb | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index dbe1ddee35..f97f89b61a 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -69,6 +69,19 @@ def to_s @sec.to_s end + begin + # ruby 2.5 or later + Time.at(0, 0, :nanosecond) + + def to_time + Time.at(@sec, @nsec, :nanosecond) + end + rescue + def to_time + Time.at(@sec, @nsec / 1000.0) + end + end + def to_json(*args) @sec.to_s end diff --git a/test/test_event_time.rb b/test/test_event_time.rb index 0c74d209e9..dfbeed9f04 100644 --- a/test/test_event_time.rb +++ b/test/test_event_time.rb @@ -36,6 +36,13 @@ class EventTimeTest < Test::Unit::TestCase assert_equal('100', "#{time}") end + test '#to_time' do + time = Fluent::EventTime.new(@now.to_i, @now.nsec).to_time + assert_instance_of(Time, time) + assert_equal(@now.to_i, time.to_i) + assert_equal(@now.nsec, time.nsec) + end + test '#to_json' do time = Fluent::EventTime.new(100) assert_equal('100', time.to_json)