memcache で PRagger の起動を早くしてみたかったけれど

memcache を使って、読み込んだ Plugin クラスをキャッシュ化して、
PRagger の起動時間を早くしてみようと思ったけれども、逆に遅くなってしまった。。。orz


実際には、キャッシュに乗せているのは Plugin が保持しているソースコードなので、
ファイルからの読み込みがなくなっただけなんだけれども。


以下、計測結果とソースコードの diff。
計測時に読み込んだプラグインの数は、今公式サイトで公開しているプラグインの 46 個。

計測結果

変更前
user system total real
1.718000 2.531000 4.249000 ( 4.296000)
変更後
user system total real
初回起動 1.485000 3.500000 4.985000 ( 5.094000)
2回目以降 1.266000 3.156000 4.422000 ( 4.516000)

diff

--- pragger.rb.org      Wed Feb 27 22:05:09 2008
+++ pragger.rb  Wed Apr 09 21:56:10 2008
@@ -12,6 +12,15 @@
   def initialize(file)
     instance_eval( @source = File.read(file).toutf8, file , 1)
   end
+
+  def marshal_dump
+    @source
+  end
+
+  def marshal_load(obj)
+    instance_eval(@source = obj, "from memcache" , 1)
+  end
+
   def self.load_plugins(folder = (Pathname.new(__FILE__).parent + "plugin"))
     Pathname.glob(folder + "**/*.rb").sort.each do |file|
       begin
@@ -29,7 +38,23 @@
   end
 end

-Plugin.load_plugins()
+require 'rubygems'
+require 'memcache'
+require 'benchmark'
+
+cache = MemCache::new 'localhost:11211'
+
+puts Benchmark::CAPTION
+puts Benchmark.measure {
+  plugin_cache = cache.get('pragger_plugins')
+  if plugin_cache.nil?
+    Plugin.load_plugins()
+    cache['pragger_plugins'] = $plugins
+  else
+    $plugins = plugin_cache
+  end
+}
+
 configFile = "config.yaml"
 opt = OptionParser.new
 opt.on("-c", "--configfile CONFIGFILE") {|v| configFile = v }