/
cli_parser.rb
106 lines (89 loc) · 3.16 KB
/
cli_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'optparse'
require 'ostruct'
#
# == PARAMETERS
# * a - database adapter. ie: mysql, postgresql, oracle, etc.
# * n - number of objects to test with. ie: 1, 100, 1000, etc.
# * t - the table types to test. ie: myisam, innodb, memory, temporary, etc.
#
module BenchmarkOptionParser
BANNER = "Usage: ruby #{$0} [options]\nSee ruby #{$0} -h for more options.".freeze
def self.print_banner
puts BANNER
end
def self.print_banner!
print_banner
exit
end
def self.print_options( options )
puts "Benchmarking the following options:"
puts " Database adapter: #{options.adapter}"
puts " Number of objects: #{options.number_of_objects}"
puts " Table types:"
print_valid_table_types( options, prefix: " " )
end
# TODO IMPLEMENT THIS
def self.print_valid_table_types( options, hsh = { prefix: '' } )
if !options.table_types.keys.empty?
options.table_types.keys.sort.each { |type| puts hsh[:prefix].to_s + type.to_s }
else
puts 'No table types defined.'
end
end
def self.parse( args )
options = OpenStruct.new(
adapter: 'mysql2',
table_types: {},
delete_on_finish: true,
number_of_objects: [],
outputs: [] )
opt_parser = OptionParser.new do |opts|
opts.banner = BANNER
# parse the database adapter
opts.on( "a", "--adapter [String]",
"The database adapter to use. IE: mysql, postgresql, oracle" ) do |arg|
options.adapter = arg
end
# parse do_not_delete flag
opts.on( "d", "--do-not-delete",
"By default all records in the benchmark tables will be deleted at the end of the benchmark. " \
"This flag indicates not to delete the benchmark data." ) do |_|
options.delete_on_finish = false
end
# parse the number of row objects to test
opts.on( "n", "--num [Integer]",
"The number of objects to benchmark." ) do |arg|
options.number_of_objects << arg.to_i
end
# parse the table types to test
opts.on( "t", "--table-type [String]",
"The table type to test. This can be used multiple times." ) do |arg|
if arg =~ /^all$/
options.table_types['all'] = options.benchmark_all_types = true
else
options.table_types[arg] = true
end
end
# print results in CSV format
opts.on( "--to-csv [String]", "Print results in a CSV file format" ) do |filename|
options.outputs << OpenStruct.new( format: 'csv', filename: filename)
end
# print results in HTML format
opts.on( "--to-html [String]", "Print results in HTML format" ) do |filename|
options.outputs << OpenStruct.new( format: 'html', filename: filename )
end
end # end opt.parse!
begin
opt_parser.parse!( args )
if options.table_types.empty?
options.table_types['all'] = options.benchmark_all_types = true
end
rescue Exception
print_banner!
end
options.number_of_objects = [1000] if options.number_of_objects.empty?
options.outputs = [OpenStruct.new( format: 'html', filename: 'benchmark.html')] if options.outputs.empty?
print_options( options )
options
end
end