Class: HoneyFormat::BenchmarkCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/honey_format/cli/benchmark_cli.rb

Overview

Benchmark CLI

Constant Summary collapse

CSV_TEST_DATA_URL =

CSV default test data location

'https://gist.github.com/buren/b669dd82fa37e37672da2cab33c8a830/raw/54ba14a698941ff61f3b854b66df0a7782c79c85/csv_1000_rows.csv'.freeze
CSV_TEST_DATA_CACHE_PATH =

CSV default test data cache location

'/tmp/honey-format-benchmark-test.csv'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer: CLIResultWriter.new) ⇒ BenchmarkCLI

Instantiate the CLI

Parameters:

  • writer (CLIResultWriter) (defaults to: CLIResultWriter.new)

    the result writer to use



20
21
22
23
24
25
# File 'lib/honey_format/cli/benchmark_cli.rb', line 20

def initialize(writer: CLIResultWriter.new)
  @used_input_path = nil
  @writer = writer
  @options = parse_options(argv: ARGV)
  writer.verbose = true if @options[:verbose]
end

Instance Attribute Details

#optionsHash (readonly)

from command line arguments

Returns:

  • (Hash)

    the current value of options



10
11
12
# File 'lib/honey_format/cli/benchmark_cli.rb', line 10

def options
  @options
end

#writerCLIResultWriter (readonly)

the CLI result writer

Returns:



10
11
12
# File 'lib/honey_format/cli/benchmark_cli.rb', line 10

def writer
  @writer
end

Instance Method Details

#expected_runtime_seconds(report_count:) ⇒ Integer

Returns the expected runtime in seconds

Parameters:

  • report_count (Integer)

    number of reports in benchmark

Returns:

  • (Integer)

    expected runtime in seconds



30
31
32
33
34
35
36
# File 'lib/honey_format/cli/benchmark_cli.rb', line 30

def expected_runtime_seconds(report_count:)
  runs = report_count * options[:lines_multipliers].length
  warmup_time_seconds = runs * options[:benchmark_warmup]
  bench_time_seconds = runs * options[:benchmark_time]

  warmup_time_seconds + bench_time_seconds
end

#fetch_default_benchmark_csvString

Download or fetch the default benchmark file from cache

Returns:

  • (String)

    CSV file as a string



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/honey_format/cli/benchmark_cli.rb', line 46

def fetch_default_benchmark_csv
  cache_path = CSV_TEST_DATA_CACHE_PATH

  if File.exist?(cache_path)
    writer.puts "Cache file found at #{cache_path}.", verbose: true
    @used_input_path = cache_path
    return File.read(cache_path)
  end

  writer.print 'Downloading test data file from GitHub..', verbose: true
  require 'open-uri'
  open(CSV_TEST_DATA_URL).read.tap do |csv| # rubocop:disable Security/Open
    @used_input_path = CSV_TEST_DATA_URL
    writer.puts 'done!', verbose: true
    File.write(cache_path, csv)
    writer.puts "Wrote cache file to #{cache_path}..", verbose: true
  end
end

#parse_options(argv:) ⇒ Hash

Parse command line arguments and return options

Parameters:

  • argv (Array<String>)

    the command lines arguments

Returns:

  • (Hash)

    the command line options



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/honey_format/cli/benchmark_cli.rb', line 68

def parse_options(argv:)
  input_path = nil
  benchmark_time = 30
  benchmark_warmup = 5
  lines_multipliers = [1]
  verbose = false

  OptionParser.new do |parser|
    parser.banner = 'Usage: bin/benchmark [file.csv] [options]'
    parser.default_argv = argv

    parser.on('--csv=[file1.csv]', String, 'CSV file(s)') do |value|
      input_path = value
    end

    parser.on('--[no-]verbose', 'Verbose output') do |value|
      verbose = value
    end

    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

    parser.on('--time=[30]', String, 'Benchmark time (default: 30)') do |value|
      benchmark_time = Integer(value)
    end

    parser.on('--warmup=[30]', String, 'Benchmark warmup (default: 30)') do |value|
      benchmark_warmup = Integer(value)
    end

    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!

  {
    input_path: input_path,
    benchmark_time: benchmark_time,
    benchmark_warmup: benchmark_warmup,
    lines_multipliers: lines_multipliers,
    verbose: verbose,
  }
end

#used_input_pathString

Return the input path used for the benchmark

Returns:

  • (String)

    the input path (URL or filepath)



40
41
42
# File 'lib/honey_format/cli/benchmark_cli.rb', line 40

def used_input_path
  options[:input_path] || @used_input_path
end