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

Define Fluent::EventTime#inspect method #1915

Merged
merged 3 commits into from
Apr 2, 2018

Conversation

okkez
Copy link
Contributor

@okkez okkez commented Mar 29, 2018

    time = Fluent::EventTime.now
    # previous version
    p time # => #<Fluent::EventTime:0x00005635d50ecdb8 @sec=1522311320, @nsec=861617325>
    # this version
    p time # => 2018-03-29 17:15:20.861617325 +0900

    time = Fluent::EventTime.now
    # previous version
    p time # => #<Fluent::EventTime:0x00005635d50ecdb8 @sec=1522311320, @nsec=861617325>
    # this version
    p time # => 2018-03-29 17:15:20.861617325 +0900
@mururu
Copy link
Member

mururu commented Mar 29, 2018

The 3rd argument of Time.at is available only on Ruby 2.5 or later, so Time.at(self) seems better.

@okkez
Copy link
Contributor Author

okkez commented Mar 29, 2018

Oh, thanks! I will check this tomorrow.

Because 3 arguments version of `Time.at` has been added since Ruby 2.5.0.
See also https://bugs.ruby-lang.org/issues/13919
@okkez
Copy link
Contributor Author

okkez commented Mar 30, 2018

I've checked Time.at(self) for Ruby 2.1, 2.2, 2.3, 2.4, 2.5.

@@ -108,6 +108,10 @@ def self.parse(*args)
def method_missing(name, *args, &block)
@sec.send(name, *args, &block)
end

def inspect
Time.at(self).strftime('%Y-%m-%d %H:%M:%S.%N %z')
Copy link
Member

Choose a reason for hiding this comment

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

How about using Strftime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion!

Strftime is faster than Time#strftime.

```
$ bundle exec ruby t.rb
Rehearsal -------------------------------------------------
Time#strftime   4.380378   0.835296   5.215674 (  5.219284)
Strftime        2.861623   0.740209   3.601832 (  3.603956)
---------------------------------------- total: 8.817506sec

                    user     system      total        real
Time#strftime   4.688617   0.847361   5.535978 (  5.539647)
Strftime        2.691129   0.852342   3.543471 (  3.546031)
```

Using following benchmark script:

```ruby
require "benchmark"
require_relative "./lib/fluent/load"

N = 1000

formatter = Strftime.new('%Y-%m-%d %H:%M:%S.%N %z')

event_times = []
1000.times do
  event_times << Fluent::EventTime.new(Time.now.to_i, (rand * 10**9).to_i)
end

Benchmark.bmbm do |x|
  x.report("Time#strftime") do
    N.times do
      event_times.each do |event_time|
        Time.at(event_time).strftime('%Y-%m-%d %H:%M:%S.%N %z')
      end
    end
  end
  x.report("Strftime") do
    N.times do
      event_times.each do |event_time|
        formatter.exec(Time.at(event_time))
      end
    end
  end
end
```
@repeatedly repeatedly merged commit 095bf17 into fluent:master Apr 2, 2018
@repeatedly
Copy link
Member

Thanks!

@okkez okkez deleted the human-readable-event-time branch April 3, 2018 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants