Skip to content

Commit

Permalink
Only let the user know once that we are benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob committed Oct 15, 2017
1 parent c697596 commit 4285f7f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 53 deletions.
3 changes: 1 addition & 2 deletions lib/benchee/benchmark/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ defmodule Benchee.Benchmark.Runner do
printer: printer,
config: config
}) do
printer.input_information(input_name, config)
printer.benchmarking(job_name, config)
printer.benchmarking(job_name, input_name, config)
Parallel.map(1..config.parallel, fn(_task_number) ->
run_scenario(scenario, scenario_context)
end)
Expand Down
24 changes: 7 additions & 17 deletions lib/benchee/output/benchmark_printer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ defmodule Benchee.Output.BenchmarkPrinter do
@doc """
Prints a notice which job is currently being benchmarked.
"""
def benchmarking(_, %{print: %{benchmarking: false}}), do: nil
def benchmarking(name, _config) do
IO.puts "Benchmarking #{name}..."
def benchmarking(_, _, %{print: %{benchmarking: false}}), do: nil
def benchmarking(name, input_name, _config) do
IO.puts "Benchmarking #{name}#{input_information(input_name)}..."
end

@no_input Benchmark.no_input
defp input_information(@no_input), do: ""
defp input_information(input_name), do: " with input #{input_name}"

@doc """
Prints a warning about accuracy of benchmarks when the function is super fast.
"""
Expand All @@ -85,18 +89,4 @@ defmodule Benchee.Output.BenchmarkPrinter do
You may disable this warning by passing print: [fast_warning: false] as configuration options.
"""
end

@doc """
Prints an informative message about which input is currently being
benchmarked, when multiple inputs were specified.
"""
def input_information(_, %{print: %{benchmarking: false}}) do
nil
end
def input_information(input_name, _config) do
if input_name != Benchmark.no_input do
IO.puts "\nBenchmarking with input #{input_name}:"
end
end

end
5 changes: 3 additions & 2 deletions test/benchee/benchmark/runner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ defmodule Benchee.Benchmark.RunnerTest do
refute_receive :called
end

@no_input Benchmark.no_input()
test "asks to print what is currently benchmarking" do
test_suite()
|> Benchmark.benchmark("Something", fn -> :timer.sleep 10 end)
|> Benchmark.measure(TestPrinter)

assert_receive {:benchmarking, "Something"}
assert_receive {:benchmarking, "Something", @no_input}
end

@inputs %{"Arg 1" => "Argument 1", "Arg 2" => "Argument 2"}
Expand All @@ -185,7 +186,7 @@ defmodule Benchee.Benchmark.RunnerTest do
|> Benchmark.measure(TestPrinter)

Enum.each @inputs, fn({name, _value}) ->
assert_receive {:input_information, ^name}
assert_received {:benchmarking, "one", ^name}
end
end

Expand Down
37 changes: 11 additions & 26 deletions test/benchee/output/benchmark_printer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Benchee.Output.BenchmarkPrintertest do
import ExUnit.CaptureIO
import Benchee.Output.BenchmarkPrinter
alias Benchee.Benchmark.Scenario
alias Benchee.Benchmark

@system_info %{elixir: "1.4",
erlang: "19.2",
os: :macOS,
Expand Down Expand Up @@ -92,44 +94,27 @@ defmodule Benchee.Output.BenchmarkPrintertest do
end

describe ".benchmarking" do
test "prints information that it's currently benchmarking" do
output = capture_io fn ->
benchmarking("Something", %{})
end

assert output =~ ~r/Benchmarking.+Something/i
end

test "doesn't print if it's deactivated" do
output = capture_io fn ->
benchmarking "A", %{print: %{benchmarking: false}}
end

assert output == ""
end
end

describe ".input_information" do
test "notifies of the input being used" do
@no_input Benchmark.no_input()
test "prints information that it's currently benchmarking without input" do
output = capture_io fn ->
input_information("Big List", %{})
benchmarking("Something", @no_input, %{})
end

assert output =~ ~r/with input Big List/i
assert output =~ ~r/Benchmarking.+Something/i
end

test "does nothing when it's the no input marker" do
marker = Benchee.Benchmark.no_input
test "prints information that it's currently benchmarking with input" do
output = capture_io fn ->
input_information marker, %{}
benchmarking("Something", "great input", %{})
end

assert output == ""
assert output =~ ~r/Benchmarking.+Something with input great input/i
end

test "does not print if disabled" do
test "doesn't print if it's deactivated" do
output = capture_io fn ->
input_information("Big List", %{print: %{benchmarking: false}})
benchmarking "A", "some", %{print: %{benchmarking: false}}
end

assert output == ""
Expand Down
8 changes: 2 additions & 6 deletions test/support/fake_benchmark_printer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ defmodule Benchee.Test.FakeBenchmarkPrinter do
send self(), :configuration_information
end

def benchmarking(name, _) do
send self(), {:benchmarking, name}
def benchmarking(name, input_information, _) do
send self(), {:benchmarking, name, input_information}
end

def fast_warning do
send self(), :fast_warning
end

def input_information(name, _config) do
send self(), {:input_information, name}
end
end

0 comments on commit 4285f7f

Please sign in to comment.