[Groonga-commit] droonga/fluent-plugin-droonga at bb95aa3 [master] Run benchmark with generated data

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Oct 25 18:35:04 JST 2013


YUKI Hiroshi	2013-10-25 18:35:04 +0900 (Fri, 25 Oct 2013)

  New Revision: bb95aa35789227de688067a9918939bf605e1517
  https://github.com/droonga/fluent-plugin-droonga/commit/bb95aa35789227de688067a9918939bf605e1517

  Message:
    Run benchmark with generated data

  Added files:
    benchmark/utils.rb
  Removed files:
    benchmark/watch/benchmark-watch-ddl.grn
  Modified files:
    benchmark/watch/benchmark-scan.rb

  Added: benchmark/utils.rb (+120 -0) 100644
===================================================================
--- /dev/null
+++ benchmark/utils.rb    2013-10-25 18:35:04 +0900 (a993e7a)
@@ -0,0 +1,120 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013 droonga project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+module DroongaBenchmark
+  class StubWorker
+    attr_reader :context
+    def initialize(context)
+      @context = context
+    end
+  end
+
+  class WatchDatabase
+    attr_reader :context
+
+    def initialize(n_times)
+      setup_database
+      @context = Groonga::Context.new
+      @context.open_database("#{@database_path}/db")
+    end
+
+    def setup_database
+      @database_path = "/tmp/watch-benchmark"
+      @ddl_path = File.expand_path(File.join(__FILE__, "..", "..", "ddl", "watchdb.grn"))
+      FileUtils.rm_rf(@database_path)
+      FileUtils.mkdir_p(@database_path)
+      `cat #{@ddl_path} | groonga -n #{@database_path}/db`
+    end
+
+    def subscribe(term)
+      queries = @context["Query"]
+      query = queries.add(term, :keywords => [term])
+
+      subscribers = @context["Subscriber"]
+      subscribers.add("subscriber for #{term}",
+                      :subscriptions => [query],
+                      :route => "0.0.0.0:0/benchamrk")
+    end
+  end
+
+  class TermsGenerator
+    class << self
+      def generate(n_terms)
+        new.generate(n_terms)
+      end
+    end
+
+    def initialize
+      @generator = to_enum(:each)
+    end
+
+    def generate(n_terms)
+      terms = []
+      n_terms.times do
+        terms << @generator.next
+      end
+      terms
+    end
+
+    FIRST_INITIAL_LETTER = "㐀"
+    SUFFIX = "あいうえおかきくけこ"
+    def each
+      initial_letter = FIRST_INITIAL_LETTER
+      while true do
+        yield "#{initial_letter}#{SUFFIX}"
+        initial_letter.succ!
+      end
+    end
+  end
+
+  class TargetsGenerator
+    class << self
+      def generate(n_terms, params)
+        new(params).generate(n_terms)
+      end
+    end
+
+    PADDING = "パディング"
+    SIZE    = 1000
+
+    def initialize(params)
+      @terms     = params[:terms]
+      @incidence = params[:incidence]
+    end
+
+    def generate(n_targets)
+      targets = []
+
+      n_matched_targets = (n_targets.to_f * @incidence).to_i
+      n_unmatched_targets = (n_targets - n_matched_targets)
+
+      n_matched_targets.times do
+        targets << generate_target(@terms.sample(1))
+      end
+
+      n_unmatched_targets.times do
+        targets << generate_target
+      end
+
+      targets
+    end
+
+    def generate_target(term="")
+      term + (PADDING * (SIZE / PADDING.size))
+    end
+  end
+end

  Modified: benchmark/watch/benchmark-scan.rb (+18 -26)
===================================================================
--- benchmark/watch/benchmark-scan.rb    2013-10-25 17:59:42 +0900 (668d1b5)
+++ benchmark/watch/benchmark-scan.rb    2013-10-25 18:35:04 +0900 (d32abf0)
@@ -21,45 +21,37 @@ require "fileutils"
 require "groonga"
 
 require "droonga/plugin/handler_watch"
-
-class StubWorker
-  attr_reader :context
-  def initialize(context)
-    @context = context
-  end
-end
+require File.join(__FILE__, "..", "..", "utils.rb")
 
 class ScanBenchmark
   def initialize(n_times)
     @n_times = n_times
-    setup
-  end
 
-  def setup
-    setup_database
-    @context = Groonga::Context.new
-    @context.open_database("#{@database_path}/db")
-    @worker = StubWorker.new(@context)
-    @watch = Droonga::WatchHandler.new(@worker)
-    @hits = []
-  end
+    @database = WatchDatabase.new
 
-  def setup_database
-    @database_path = "/tmp/watch-benchmark"
-    @ddl_path = File.expand_path(File.join(__FILE__, "..", "benchmark-watch-ddl.grn"))
-    FileUtils.rm_rf(@database_path)
-    FileUtils.mkdir_p(@database_path)
-    `cat #{@ddl_path} | groonga -n #{@database_path}/db`
+    @worker = DroongaBenchmark::StubWorker.new(@database.context)
+    @watch_handler = Droonga::WatchHandler.new(@worker)
+
+    @terms = DroongaBenchmark::TermsGenerator.generate(@n_times)
+    @targets = DroongaBenchmark::TargetsGenerator.generate(@n_times,
+                                                           :terms => @terms,
+                                                           :incidence => 0.1)
+
+    @terms.each do |term|
+      @database.subscribe(term)
+    end
+
+    @hits = []
   end
 
   def run
-    @n_times.times do
-      scan("This is a comment.")
+    @targets.each do |target|
+      scan(target)
     end
   end
 
   def scan(target)
-    @watch.send(:scan_body, @hits, target)
+    @watch_handler.send(:scan_body, @hits, target)
     @hits.clear
   end
 end

  Deleted: benchmark/watch/benchmark-watch-ddl.grn (+0 -11) 100644
===================================================================
--- benchmark/watch/benchmark-watch-ddl.grn    2013-10-25 17:59:42 +0900 (676ff4b)
+++ /dev/null
@@ -1,11 +0,0 @@
-table_create Keyword TABLE_PAT_KEY ShortText --normalizer NormalizerAuto
-
-table_create Query TABLE_PAT_KEY ShortText
-column_create Query keywords COLUMN_VECTOR Keyword
-
-column_create Keyword queries COLUMN_INDEX Query keywords
-
-load --table Query
-[
-{"_key": "This comment", "keywords": ["This", "comment"]}
-]
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index