Debian Codegraph @

tools/groonga-benchmark-indexing.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2
3 require "fileutils"
4 require "json"
5 require "optparse"
6
7 class IndexingBenchmarker
8 def initialize
9 @groonga = "groonga"
10 @database_path = nil
11 @benchmark_database_dir = detect_benchmark_database_dir
12 end
13
14 def run
15 catch(:run) do
16 parse_options!
17 end
18
19 dump_no_indexes = dump("dump-no-indexes.grn",
20 "--dump_indexes", "no")
21 dump_only_indexes = dump("dump-only-indexes.grn",
22 "--dump_plugins", "no",
23 "--dump_schema", "no",
24 "--dump_records", "no",
25 "--dump_configs", "no")
26 dump_no_records = dump("dump-no-records.grn",
27 "--dump_records", "no")
28 dump_only_records = dump("dump-only-records.grn",
29 "--dump_plugins", "no",
30 "--dump_schema", "no",
31 "--dump_indexes", "no",
32 "--dump_configs", "no")
33
34 create_benchmark_database do
35 p [:load_record, measure(dump_no_indexes)]
36 p [:static_index_creation, measure(dump_only_indexes)]
37 end
38
39 create_benchmark_database do
40 p [:create_schema, measure(dump_no_records)]
41 p [:load_record_and_create_index, measure(dump_only_records)]
42 end
43
44 true
45 end
46
47 private
48 def detect_benchmark_database_dir
49 candiates = [
50 "/dev/shm",
51 "tmp",
52 ]
53 candiates.find do |candidate|
54 File.exist?(candidate)
55 end
56 end
57
58 def benchmark_database_path
59 "#{@benchmark_database_dir}/bench-db/db"
60 end
61
62 def parse_options!
63 option_parser = OptionParser.new do |parser|
64 parser.banner += " SOURCE_DATABASE"
65
66 parser.on("--groonga=PATH",
67 "Use PATH as groonga command path") do |path|
68 @groonga = path
69 end
70
71 parser.on("--benchmark-database-dir=DIR",
72 "Use DIR to put benchmark database") do |dir|
73 @benchmark_database_dir = dir
74 end
75 end
76
77 @database_path, = option_parser.parse!(ARGV)
78 if @database_path.nil?
79 puts(option_parser)
80 throw(:run)
81 end
82 end
83
84 def dump(path, *dump_options)
85 return path if File.exist?(path)
86 unless system(@groonga,
87 @database_path,
88 "dump",
89 *dump_options,
90 :out => path)
91 raise "failed to dump: #{dump_options.inspect}"
92 end
93 path
94 end
95
96 def create_benchmark_database
97 dir = File.dirname(benchmark_database_path)
98 FileUtils.rm_rf(dir)
99 FileUtils.mkdir_p(dir)
100 system(@groonga,
101 "-n", benchmark_database_path,
102 "shutdown",
103 :out => IO::NULL)
104 begin
105 yield
106 ensure
107 FileUtils.rm_rf(dir)
108 end
109 end
110
111 def measure(dump_path)
112 result = "result"
113 begin
114 system(@groonga,
115 "--file", dump_path,
116 benchmark_database_path,
117 :out => result)
118 File.open(result) do |output|
119 output.each_line.inject(0) do |result, line|
120 result + JSON.parse(line)[0][2]
121 end
122 end
123 ensure
124 FileUtils.rm_f(result)
125 end
126 end
127 end
128
129 exit(IndexingBenchmarker.new.run)