Permalink
Browse files

cache CronLine#brute_frequency results

  • Loading branch information...
1 parent def9b9b commit ef8047bc95e854f12924774576790b7c6c1accc1 @jmettraux committed Dec 25, 2015
Showing with 67 additions and 9 deletions.
  1. +1 −0 CHANGELOG.txt
  2. +2 −0 Gemfile
  3. +10 −9 lib/rufus/scheduler/cronline.rb
  4. +29 −0 misc/kssminus_38k_jobs.rb
  5. +25 −0 misc/kssminus_38k_jobs_rprof.rb
View
@@ -4,6 +4,7 @@
== rufus-scheduler - 3.2.0 not yet released
+- cache CronLine#brute_frequency results
- "every" shift prevention by https://github.com/Korrigan
- Ruby warnings silenced by https://github.com/vivitar
View
@@ -3,5 +3,7 @@ source 'https://rubygems.org'
#gem 'tzinfo-data'
+#gem 'ruby-prof'
+
gemspec
@@ -223,6 +223,10 @@ def frequency
}[1]
end
+ # Caching facility. Currently only used for brute frequencies.
+ #
+ @cache = {}; class << self; attr_reader :cache; end
+
# Returns the shortest delta between two potential occurences of the
# schedule described by this cronline.
#
@@ -239,16 +243,13 @@ def frequency
# Of course, this method can get VERY slow if you call on it a second-
# based cronline...
#
- # Since it's a rarely used method, I haven't taken the time to make it
- # smarter/faster.
- #
- # One obvious improvement would be to cache the result once computed...
- #
- # See https://github.com/jmettraux/rufus-scheduler/issues/89
- # for a discussion about this method.
- #
def brute_frequency
+ key = "brute_frequency:#{@original}"
+
+ delta = self.class.cache[key]
+ return delta if delta
+
delta = 366 * DAY_S
t0 = previous_time(Time.local(2000, 1, 1))
@@ -268,7 +269,7 @@ def brute_frequency
t0 = t1
end
- delta
+ self.class.cache[key] = delta
end
def next_second(time)
@@ -0,0 +1,29 @@
+
+require 'rufus-scheduler'
+
+p [ RUBY_VERSION, RUBY_PLATFORM ]
+
+crons = [
+ '* * * * *',
+ '*/5 * * * *',
+ '*/10 * * * *',
+ '10 4 * * *',
+ '35 */3 * * *',
+ '* */3 * * *',
+ '16 */4 * * *',
+ '30 14 * * *',
+ '26 */2 * * *'
+]
+
+total = 38_000 / crons.length
+
+crons.each do |cron|
+ s = Rufus::Scheduler.new
+ t0 = Time.now
+ total.times do
+ s.cron(cron) {}
+ end
+ p [ cron, Time.now - t0 ]
+ s.shutdown
+end
+
@@ -0,0 +1,25 @@
+
+require 'ruby-prof'
+require 'rufus-scheduler'
+
+p [ RUBY_VERSION, RUBY_PLATFORM ]
+
+s = Rufus::Scheduler.new
+
+t = Time.now
+profile = RubyProf.profile do
+ 10.times do
+ s.cron('*/5 * * * *') {}
+ end
+end
+p Time.now - t
+
+#printer = RubyProf::GraphPrinter.new(profile)
+#printer = RubyProf::GraphHtmlPrinter.new(profile)
+#printer = RubyProf::CallTreePrinter.new(profile)
+#printer = RubyProf::DotPrinter.new(profile)
+#printer.print(STDOUT, {})
+
+printer = RubyProf::CallStackPrinter.new(profile)
+File.open('out.html', 'wb') { |f| printer.print(f, {}) }
+

0 comments on commit ef8047b

Please sign in to comment.