Class: Serialbench::Cli::BenchmarkCli

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

Overview

CLI for managing individual benchmark runs

Instance Method Summary collapse

Methods inherited from BaseCli

exit_on_failure?

Instance Method Details

#_docker_execute(environment_config_path, benchmark_config_path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/serialbench/cli/benchmark_cli.rb', line 145

def _docker_execute(environment_config_path, benchmark_config_path)
  environment_config = load_environment_config(environment_config_path)
  benchmark_config = load_benchmark_config(benchmark_config_path)

  say '🚀 Executing benchmark run', :green
  say "Environment: #{environment_config_path} (#{environment_config.kind})", :cyan
  say "Configuration: #{benchmark_config_path}", :cyan

  runner = Serialbench::BenchmarkRunner.new(
    environment_config: environment_config,
    benchmark_config: benchmark_config
  )

  # Run benchmarks
  results = runner.run_all_benchmarks

  platform = Serialbench::Models::Platform.current_local

   = Models::RunMetadata.new(
    benchmark_config_path: benchmark_config_path,
    environment_config_path: environment_config_path,
    tags: [
      'docker',
      platform.os,
      platform.arch,
      "ruby-#{environment_config.ruby_build_tag}"
    ]
  )
  # Create results directory
  result_dir = options[:result_dir]
  FileUtils.mkdir_p(result_dir)

  # Save results to single YAML file with platform and metadata merged in
  results_model = Models::Result.new(
    platform: platform,
    metadata: ,
    environment_config: environment_config,
    benchmark_config: benchmark_config,
    benchmark_result: results
  )

  # Restore YAML to use Psych for output, otherwise lutaml-model's to_yaml
  # will have no output
  Object.const_set(:YAML, Psych)

  results_file = File.join(result_dir, 'results.yaml')
  results_model.to_file(results_file)

  say '✅ Local benchmark completed successfully!', :green
  say 'Results saved.', :cyan
rescue StandardError => e
  say "❌ Local benchmark failed: #{e.message}", :red
  say "Details: #{e.backtrace.first(3).join("\n")}", :white if options[:verbose]
  raise e
end

#build_site(result_path) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/serialbench/cli/benchmark_cli.rb', line 213

def build_site(result_path)
  unless Dir.exist?(result_path)
    say "Result directory not found: #{result_path}", :red
    say "Please create a result first using 'serialbench benchmark create'", :white
    exit 1
  end

  result = Serialbench::Models::Result.load(result_path)

  if result.nil?
    say "Result '#{result_path}' not found", :yellow
    say "Use 'serialbench benchmark add-result' to add a result first", :white
    return
  end

  say "🏗️  Generating HTML site for result: #{result_path}", :green

  # Use the unified site generator for results
  puts 'Site generation moved to serialbench.github.io'

  say '✅ HTML site generated successfully!', :green
  say "Site location: #{options[:output_dir]}", :cyan
  say "Open: #{File.join(options[:output_dir], 'index.html')}", :white
rescue StandardError => e
  say "Error generating site: #{e.message}", :red
  say "Details: #{e.backtrace.first(3).join("\n")}", :red if options[:verbose]
  exit 1
end

#create(name = nil) ⇒ Object



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
# File 'lib/serialbench/cli/benchmark_cli.rb', line 31

def create(name = nil)
  raise 'Run name cannot be empty' if name&.strip&.empty?

  validate_name(name)

  config_dir = 'config/runs'
  FileUtils.mkdir_p(config_dir)

  benchmark_config_path = File.join(config_dir, "#{name}.yml")

  if File.exist?(benchmark_config_path)
    say "Configuration file already exists: #{benchmark_config_path}", :yellow
    return unless yes?('Overwrite existing file? (y/n)')
  end

  benchmark_config = Models::BenchmarkConfig.new(
    formats: options[:formats],
    warmup: options[:warmup],
    data_sizes: options[:data_sizes]
  )

  benchmark_config.to_file(benchmark_config_path)

  say "✅ Generated run configuration: #{benchmark_config_path}", :green
  say 'Edit the file to customize benchmark settings', :cyan
end

#execute(environment_config_path, benchmark_config_path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/serialbench/cli/benchmark_cli.rb', line 71

def execute(environment_config_path, benchmark_config_path)
  environment = load_environment_config(environment_config_path)
  config = load_benchmark_config(benchmark_config_path)

  say '🚀 Executing benchmark run', :green
  say "Environment: #{environment.name} (#{environment.kind})", :cyan
  say "Configuration: #{benchmark_config_path}", :cyan

  result_dir = "results/runs/#{environment.name}-results"
  FileUtils.mkdir_p(result_dir)

  Runners.for(environment, environment_config_path)
         .run_benchmark(config, benchmark_config_path, result_dir)

  say '✅ Benchmark completed successfully!', :green
  say "Results saved to: #{result_dir}", :cyan
rescue StandardError => e
  say "❌ Error executing benchmark run: #{e.message}", :red
  exit 1
end

#listObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/serialbench/cli/benchmark_cli.rb', line 249

def list
  store = Serialbench::Models::ResultStore.default
  runs = if options[:tags]
           store.find_runs(tags: options[:tags])
         else
           store.find_runs
         end

  if runs.empty?
    say 'No runs found', :yellow
    return
  end

  say 'Available Runs:', :green
  say '=' * 50, :green

  runs.each do |run|
    say '📊 Run:', :cyan
    say "   Created: #{run..created_at}", :white
    say "   Platform: #{run.platform.platform_string} (os: #{run.platform.os}, arch: #{run.platform.arch})",
        :white
    say "   Environment config: #{run..environment_config_path}", :white
    say "   Benchmark config: #{run..benchmark_config_path}", :white
    say "   Environment: #{run.environment_config.name} (#{run.environment_config.kind})", :white
    say "   Tags: [#{run..tags.join(', ')}]", :white
    say ''
  end
rescue StandardError => e
  say "Error listing runs: #{e.message}", :red
  exit 1
end