floraison / et-orbi

Time zones for fugit and rufus-scheduler. Urbi et Orbi.
MIT License
24 stars 11 forks source link

Avoid unnecessary calls to Chronic #28

Closed jmettraux closed 4 years ago

jmettraux commented 4 years ago

As seen in https://github.com/ondrejbartas/sidekiq-cron/issues/276


require 'benchmark'

require 'chronic'
require 'fugit'

# https://github.com/ondrejbartas/sidekiq-cron/issues/276

crons = [ '0 8 1 * * America/Chicago' ] * 1000
time = Time.now

#EtOrbi.chronic_enabled = false
  # leave Chronic parsing enabled (the default as of et-orbi 1.2.3)

puts Benchmark.measure {
  crons.map { |cron| Fugit.parse_cron(cron).previous_time(time).utc }
}.to_s
  #
  # => 109.987803   0.493191 110.480994 (110.569114)
  #
  # ( ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
  # ( MacBook Pro mid 2015 16GB RAM

Fugit::Cron#previous_time ends up calling https://github.com/floraison/et-orbi/blob/master/lib/et-orbi/make.rb#L106-L112 so the incoming Time instances ends up as a String and that's where Chronic is called. Do avoid that!

jmettraux commented 4 years ago

Focus on the critical piece.

require 'benchmark'

require 'chronic'
require 'fugit'

# https://github.com/ondrejbartas/sidekiq-cron/issues/276

crons = nil

puts Benchmark.measure {
  crons = ([ '0 8 1 * * America/Chicago' ] * 1000)
    .collect { |c| Fugit.parse(c) }
}

time = Time.now

#EtOrbi.chronic_enabled = false

puts Benchmark.measure {
  crons.map { |cron| cron.previous_time(time) }
}.to_s

# bad =>
#     0.760012   0.004290   0.764302 (  0.764780)
#   110.418065   0.677115 111.095180 (111.298025)