Class: Morpheus::Cli::BenchmarkCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/standard/benchmark_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeBenchmarkCommand

this would be cool, we should store all benchmarking results in memory or on disk =o register_subcommands :list, :get, :put, :remove



19
20
21
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 19

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(opts) ⇒ Object



23
24
25
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 23

def connect(opts)
  # no connection needed
end

#execute(args) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 296

def execute(args)
  n = 1
  benchmark_name = nil
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[command...]")
    opts.on('-n', '--iterations NUMBER', Integer, "Number of iterations to run. The default is 1.") do |val|
      if val.to_i > 1
        n = val.to_i
      end
    end
    opts.on('--name NAME', String, "Name for the benchmark. Default is the command itself.") do |val|
      benchmark_name = val
    end
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Benchmark a specified command.
[command] is required. This is the command to execute
EOT
  end
  optparse.parse!(args)
  connect(options)
  if args.count < 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  
  cmd = args.join(' ')
  benchmark_name ||= cmd
  if n == 1
    start_benchmark(benchmark_name)
    # exit_code, err = my_terminal.execute(cmd)
    cmd_result = Morpheus::Cli::CliRegistry.exec_expression(cmd)
    exit_code, err = Morpheus::Cli::CliRegistry.parse_command_result(cmd_result)
    benchmark_record = stop_benchmark(exit_code, err)
    Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg) if benchmark_record
  else
    benchmark_records = []
    n.times do |iteration_index|
      start_benchmark(benchmark_name)
      # exit_code, err = my_terminal.execute(cmd)
      cmd_result = Morpheus::Cli::CliRegistry.exec_expression(cmd)
      exit_code, err = Morpheus::Cli::CliRegistry.parse_command_result(cmd_result)
      benchmark_record = stop_benchmark(exit_code, err)
      Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg) if Morpheus::Logging.debug?
      benchmark_records << benchmark_record
    end
    # calc total and mean and print it
    # all_durations = benchmark_records.collect {|benchmark_record| benchmark_record.duration }
    # total_duration = all_durations.inject(0.0) {|acc, i| acc + i }
    # avg_duration = total_duration / all_durations.size
    # total_time_str = "#{total_duration.round((total_duration > 0.002) ? 3 : 6)}s"
    # avg_time_str = "#{avg_duration.round((total_duration > 0.002) ? 3 : 6)}s"

    all_durations = []
    stats = {total: 0, avg: nil, min: nil, max: nil}
    benchmark_records.each do |benchmark_record| 
      duration = benchmark_record.duration
      if duration
        all_durations << duration
        stats[:total] += duration
        if stats[:min].nil? || stats[:min] > duration
          stats[:min] = duration
        end
        if stats[:max].nil? || stats[:max] < duration
          stats[:max] = duration
        end
      end
    end
    if all_durations.size > 0
      stats[:avg] = stats[:total].to_f / all_durations.size
    end

    total_time_str = "#{stats[:total].round((stats[:total] > 0.002) ? 3 : 6)}s"
    min_time_str = stats[:min] ? "#{stats[:min].round((stats[:min] > 0.002) ? 3 : 6)}s" : ""
    max_time_str = stats[:max] ? "#{stats[:max].round((stats[:max] > 0.002) ? 3 : 6)}s" : ""
    avg_time_str = stats[:avg] ? "#{stats[:avg].round((stats[:avg] > 0.002) ? 3 : 6)}s" : ""

    out = ""
    # <benchmark name or command>
    out << "#{benchmark_name.ljust(30, ' ')}"
    # exit: 0
    exit_str = "0"
    bad_benchmark = benchmark_records.find {|benchmark_record| benchmark_record.exit_code && benchmark_record.exit_code != 0 }
    if bad_benchmark
      bad_benchmark.exit_code.to_s
      out << "\texit: #{bad_benchmark.exit_code.to_s.ljust(2, ' ')}"
      out << "\terror: #{bad_benchmark.error.to_s.ljust(12, ' ')}"
    else
      out << "\texit: 0 "
    end

    out << "\tn: #{n.to_s.ljust(4, ' ')}"
    out << "\ttotal: #{total_time_str.ljust(9, ' ')}"
    out << "\tmin: #{min_time_str.ljust(9, ' ')}"
    out << "\tmax: #{max_time_str.ljust(9, ' ')}"
    out << "\tavg: #{avg_time_str.ljust(9, ' ')}"


    if bad_benchmark
      print red,out,reset,"\n"
      return 1
    else
      print cyan,out,reset,"\n"
      return 0
    end
    
  end

  return 0 
end

#handle(args) ⇒ Object



27
28
29
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 27

def handle(args)
  handle_subcommand(args)
end

#off(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 61

def off(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Disable global benchmarking. 
The default state for this setting is off.
EOT
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  benchmark_was_enabled = Morpheus::Benchmarking.enabled
  Morpheus::Benchmarking.enabled = false
  my_terminal.benchmarking = Morpheus::Benchmarking.enabled
  unless options[:quiet]
    # if benchmark_was_enabled == true
    #   Morpheus::Logging::DarkPrinter.puts "benchmark disabled" if Morpheus::Logging.debug?
    # end
    puts "#{cyan}benchmark: #{dark}off#{reset}"
  end
  return 0 
end

#off?(args) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 117

def off?(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Print the value of the global benchmark setting. 
Exit 0 if off.
EOT
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  unless options[:quiet]
    if Morpheus::Benchmarking.enabled?
      puts "#{cyan}benchmark: #{green}on#{reset}"
    else
      puts "#{cyan}benchmark: #{dark}off#{reset}"
    end
  end
  return Morpheus::Benchmarking.enabled? ? 1 : 0
end

#on(args) ⇒ 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
57
58
59
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 31

def on(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Enable global benchmarking. 
This behaves the same as if you were to add the -B switch to every command.
EOT
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  benchmark_was_enabled = Morpheus::Benchmarking.enabled
  Morpheus::Benchmarking.enabled = true
  my_terminal.benchmarking = Morpheus::Benchmarking.enabled
  unless options[:quiet]
    # if benchmark_was_enabled == false
    #   Morpheus::Logging::DarkPrinter.puts "benchmark enabled" if Morpheus::Logging.debug?
    # end
    puts "#{cyan}benchmark: #{green}on#{reset}"
  end
  return 0 
end

#on?(args) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 91

def on?(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Print the value of the global benchmark setting. 
Exit 0 if on.
EOT
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0 and got #{args.count} #{args.join(' ')}\n#{optparse}"
    return 1
  end
  if Morpheus::Benchmarking.enabled?
    puts "#{cyan}benchmark: #{green}on#{reset}" unless options[:quiet]
  else
    puts "#{cyan}benchmark: #{dark}off#{reset}" unless options[:quiet]
  end
  return Morpheus::Benchmarking.enabled? ? 0 : 1
end

#start(args) ⇒ 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
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 145

def start(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Start recording a benchmark.
[name] is required. This is just a name for the routine.
This allows you to record how long it takes to run a series of commands.
Just run `benchmark stop` when you are finished.
EOT
  end
  optparse.parse!(args)
  connect(options)
  # if args.count < 1
  #   print_error Morpheus::Terminal.angry_prompt
  #   puts_error  "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
  #   return 1
  # end
  benchmark_name = nil
  if !args.empty?
    benchmark_name = args.join(' ')
  end
  benchmark_record = Morpheus::Benchmarking.start(benchmark_name)
  unless options[:quiet]
    if benchmark_record.name
      print_green_success "Started benchmark '#{benchmark_record.name}'"
    else
      print_green_success "Started benchmark"
    end
  end
  # record this record so it can be stopped later without knowing (or remembering) the name

  return 0 
end

#status(args) ⇒ Object



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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 250

def status(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Print status of benchmark.
[name] is optional. This is the name of the benchmark to inspect.
The last benchmark is used by default.
EOT
  end
  optparse.parse!(args)
  connect(options)
  # if args.count < 1
  #   print_error Morpheus::Terminal.angry_prompt
  #   puts_error  "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
  #   return 1
  # end
  # benchmark_name = args.join(' ')
  # benchmark_record = Morpheus::Benchmarking.stop(benchmark_name)

  benchmark_name = nil
  benchmark_record = nil
  if args.empty?
    # stop the last one..
    benchmark_record = Morpheus::Benchmarking.last
    if benchmark_record.nil?
      print_error red,"No benchmark is running",reset,"\n"
      return 1
    end
  else
    benchmark_name = args.join(' ')
    benchmark_record = Morpheus::Benchmarking.lookup(benchmark_name)
    if benchmark_record.nil?
      print_error red,"Benchmark not found by name '#{benchmark_name}'",reset,"\n"
      return 1
    end
  end
  
  unless options[:quiet]
    Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg)
  end
  return 0 
end

#stop(args) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
241
242
243
244
245
246
247
248
# File 'lib/morpheus/cli/commands/standard/benchmark_command.rb', line 182

def stop(args)
  options = {}
  params = {}
  benchmark_exit_code = nil
  benchmark_err = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on('--exit CODE', String, "Exit code to end benchmark with. Default is 0 to indicate success.") do |val| # default should my_terminal.last_exit_code
      benchmark_exit_code = val.to_i
    end
    opts.on('--error ERROR', String, "Error message to include with a benchmark that failed.") do |val|
      benchmark_err = val
    end
    build_common_options(opts, options, [:quiet])
    opts.footer = <<-EOT
Stop recording a benchmark.
[name] is optional. This is the name of the benchmark to stop. 
The last benchmark is used by default.
EOT
  end
  optparse.parse!(args)
  connect(options)
  # if args.count < 1
  #   print_error Morpheus::Terminal.angry_prompt
  #   puts_error  "wrong number of arguments, expected 1-N and got #{args.count} #{args.join(' ')}\n#{optparse}"
  #   return 1
  # end
  # benchmark_name = args.join(' ')
  # benchmark_record = Morpheus::Benchmarking.stop(benchmark_name)

  benchmark_name = nil
  benchmark_record = nil
  if args.empty?
    # stop the last one..
    benchmark_record = Morpheus::Benchmarking.last
    if benchmark_record.nil?
      print_error red,"Benchmark not found",reset,"\n"
      return 1
    end
  else
    benchmark_name = args.join(' ')
    benchmark_record = Morpheus::Benchmarking.lookup(benchmark_name)
    if benchmark_record.nil?
      print_error red,"Benchmark not found by name '#{benchmark_name}'",reset,"\n"
      return 1
    end
  end
  if benchmark_record.start_time == nil || benchmark_record.end_time != nil
    if benchmark_record.name
      print_error red,"Benchmark '#{benchmark_record.name}' is not running",reset,"\n"
    else
      print_error red,"Benchmark is not running",reset,"\n"
    end
    return 1
  end
  benchmark_record.stop(benchmark_exit_code || 0, benchmark_err)
  unless options[:quiet]
    if benchmark_record.name
      print_green_success "Stopped benchmark '#{benchmark_record.name}'"
    else
      print_green_success "Stopped benchmark"
    end
  end
  # always print benchmark info
  Morpheus::Logging::DarkPrinter.puts(cyan + dark + benchmark_record.msg)
  return 0 
end