bin/benchmark in honey_format-0.12.0 vs bin/benchmark in honey_format-0.13.0
- old
+ new
@@ -2,74 +2,45 @@
require 'bundler/setup'
require 'honey_format'
require 'benchmark/ips'
require 'csv'
-require 'optparse'
-input_path = nil
-benchmark_time = 30
-benchmark_warmup = 5
-lines_multipliers = [1]
+require 'honey_format/cli/benchmark_cli'
-OptionParser.new do |parser|
- parser.banner = "Usage: bin/benchmark [file.csv] [options]"
- parser.default_argv = ARGV
+cli = HoneyFormat::BenchmarkCLI.new
+Writer = cli.writer
+options = cli.options
- parser.on("--csv=file1.csv", String, "CSV file(s)") do |value|
- input_path = value
- end
+input_path = options[:input_path]
+benchmark_time = options[:benchmark_time]
+benchmark_warmup = options[:benchmark_warmup]
+lines_multipliers = options[:lines_multipliers]
- parser.on("--lines-multipliers=[1,10,50]", Array, "Multiply the rows in the CSV file (default: 1)") do |value|
- lines_multipliers = value.map do |v|
- Integer(v).tap do |int|
- unless int >= 1
- raise(ArgumentError, '--lines-multiplier must be 1 or greater')
- end
- end
- end
- end
+original_csv = input_path ? File.read(input_path) : cli.fetch_default_benchmark_csv
+original_csv_lines = original_csv.lines
- parser.on("--time=[30]", String, "Benchmark time (default: 30)") do |value|
- benchmark_time = Integer(value)
- end
+runtime_seconds = cli.expected_runtime_seconds(report_count: 3)
+Writer.puts "Expected runtime: ~#{runtime_seconds} seconds.", verbose: true
- parser.on("--warmup=[30]", String, "Benchmark warmup (default: 30)") do |value|
- benchmark_warmup = Integer(value)
- end
+lines_multipliers.each_with_index do |lines_multiplier, index|
+ rows = original_csv_lines[1..-1] * lines_multiplier
+ csv = original_csv_lines.first + rows.join
- parser.on("-h", "--help", "How to use") do
- puts parser
- exit
- end
-
- # No argument, shows at tail. This will print an options summary.
- parser.on_tail("-h", "--help", "Show this message") do
- puts parser
- exit
- end
-end.parse!
-
-csv = File.read(input_path)
-
-lines_multipliers.each do |lines_multiplier|
- if lines_multiplier > 1
- orignial_csv_lines = csv.lines
- rows = orignial_csv_lines[1..-1] * lines_multiplier
- csv = orignial_csv_lines.first + rows.join
- end
-
line_count = csv.lines.length
- puts "== [START] Benchmark for #{line_count} lines =="
+ Writer.puts "== Benchmark #{index + 1} of #{lines_multipliers.length} =="
+ Writer.puts "path #{cli.used_input_path}"
+ Writer.puts "lines #{line_count}"
+ Writer.puts "multiplier #{lines_multiplier}"
+
Benchmark.ips do |x|
x.time = benchmark_time
x.warmup = benchmark_warmup
- x.report('stdlib CSV no options') { CSV.parse(csv) }
- x.report('stdlib CSV with header') { CSV.parse(csv, headers: true) }
- x.report('HoneyFormat::CSV') { HoneyFormat::CSV.new(csv).rows }
+ x.report('CSV no options') { CSV.parse(csv) }
+ x.report('CSV with header') { CSV.parse(csv, headers: true) }
+ x.report('HoneyFormat::CSV') { HoneyFormat::CSV.new(csv).rows }
x.compare!
end
- puts "== [END] Benchmark for #{line_count} lines =="
end