Skip to content

Commit

Permalink
Add verbose cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardiff committed Aug 14, 2019
1 parent 2310fb7 commit 1d0cfcc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Given a `manifest.yml` file there are three commands.
### run - Run all configurations

```sh
./benchy run --csv=output.csv --ndjson=output.ndjson --keep-logs manifest.yml
./benchy run -v --csv=output.csv --ndjson=output.ndjson --keep-logs manifest.yml
```

The `--csv` will store the measures in a CSV file.
The `--ndjson` will store the measures in a NDJSON file.
The `--keep-logs` will store in `.benchy_logs` the output of each execution.
The `-v,--verbose` will show verbose information of the running commands.

### run:\[index\] - Run single configuration

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Ensure that `time` and `ab` are installed. `$ apt-get install time apache2-utils

```sh
$ shards build
$ ./bin/benchy run --csv=output.csv --ndjson=output.ndjson ./sample/http.yml
$ ./bin/benchy run -v --csv=output.csv --ndjson=output.ndjson ./sample/http.yml
```

## Contributing
Expand Down
10 changes: 9 additions & 1 deletion src/benchy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Benchy
getter repeat : Int32
getter loader : String?

def initialize(manifest : Manifest, base_dir : Path)
def initialize(manifest : Manifest, base_dir : Path, @verbose : Bool = false)
@base_dir = base_dir
@name = manifest.name
@context = Hash(String, String).new
Expand Down Expand Up @@ -105,6 +105,7 @@ module Benchy
save_pid_and_wait = @loader ? " & echo $! > #{main_pid_file} & wait" : ""
instrumented_main = "#{Benchy::BIN_TIME} /bin/sh -c '#{main}#{save_pid_and_wait}'"

debug_cmd instrumented_main, configuration[:env]
main_process = Process.new(command: instrumented_main,
env: configuration[:env],
shell: true,
Expand All @@ -116,6 +117,7 @@ module Benchy
loader_status = nil

if loader = @loader
debug_cmd loader, configuration[:env]
loader_process = Process.new(command: loader,
env: configuration[:env],
shell: true,
Expand Down Expand Up @@ -183,6 +185,7 @@ module Benchy
end

private def exec(cmd : String, configuration : Configuration?) : String
debug_cmd cmd, configuration.try(&.[:env])
process = Process.new(cmd,
env: configuration.try(&.[:env]),
shell: true,
Expand All @@ -195,6 +198,11 @@ module Benchy
output
end

private def debug_cmd(cmd, env)
return unless @verbose
puts "(benchy) #{env.map { |k, v| "#{k}=#{v}" }.join(" ") if env} #{cmd}"
end

def runnable_configurations(config_selector = nil)
if (index = config_selector.try(&.to_i?))
index = index % configurations.size
Expand Down
20 changes: 15 additions & 5 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ require "json"
require "option_parser"
require "./benchy"

def init_projects(manifest_paths)
def init_projects(manifest_paths, verbose)
manifest_paths.map do |manifest_path|
manifest = Benchy::Manifest.from_yaml(File.read(manifest_path))

Benchy::Project.new(manifest, manifest_path.parent)
Benchy::Project.new(manifest, manifest_path.parent, verbose)
end
end

Expand All @@ -27,6 +27,7 @@ when "run"
cli_csv_file = nil
cli_ndjson_file = nil
cli_keep_logs = false
cli_verbose = false

OptionParser.parse(ARGV[1..]) do |opts|
opts.on("--csv=FILE", "Save results as csv") do |v|
Expand All @@ -41,13 +42,17 @@ when "run"
cli_keep_logs = v
end

opts.on("-v", "--verbose") do |v|
cli_verbose = true
end

opts.unknown_args do |before_dash, after_dash|
cli_manifest_paths = before_dash.map { |f| Path.new(f) }
end
end

if manifest_paths = cli_manifest_paths
init_projects(manifest_paths).each do |project|
init_projects(manifest_paths, cli_verbose).each do |project|
results = project.run(
run_logger: cli_keep_logs ? OutputRecorder.new(Path.new(Dir.current)) : nil
)
Expand Down Expand Up @@ -132,12 +137,17 @@ when /run:(-?\d+)/
cli_manifest_paths = nil
cli_keep_logs = false
cli_repeat = nil
cli_verbose = false

OptionParser.parse(ARGV[1..]) do |opts|
opts.on("--keep-logs", "Save run and loader logs") do |v|
cli_keep_logs = v
end

opts.on("-v", "--verbose") do |v|
cli_verbose = true
end

opts.on("--repeat=N", "Override number of repeats") do |n|
cli_repeat = n.to_i
end
Expand All @@ -149,7 +159,7 @@ when /run:(-?\d+)/

config_selector = $1
if manifest_paths = cli_manifest_paths
init_projects(manifest_paths).each do |project|
init_projects(manifest_paths, cli_verbose).each do |project|
results = project.run(
run_logger: cli_keep_logs ? OutputRecorder.new(Path.new(Dir.current)) : nil,
config_selector: config_selector,
Expand All @@ -174,7 +184,7 @@ when /run:(-?\d+)/
end
when "matrix"
manifest_paths = ARGV[1..].map { |f| Path.new(f) }
init_projects(manifest_paths).each do |project|
init_projects(manifest_paths, false).each do |project|
puts "#{project.name}:"
project.configurations.each_with_index do |config, index|
print "%5d: " % [index]
Expand Down

0 comments on commit 1d0cfcc

Please sign in to comment.