Skip to content

Commit

Permalink
after_scenario with inputs implementation
Browse files Browse the repository at this point in the history
Pondered whether after_scenario should behave like before_* i.e.
the input can be altered and passed on. Didn't see much sense
in that though as the last value (global_after_scenario) will go
nowher and the values aren't passed to any benchmarking function.

Also for consistency with `after_each` I decided to just pass them
the output of the last before_scenario (just like all after_each's
just get the return value of the benchmarking function).
  • Loading branch information
PragTob committed Oct 12, 2017
1 parent e75f057 commit a1f4df8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
7 changes: 4 additions & 3 deletions lib/benchee/benchmark/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ defmodule Benchee.Benchmark.Runner do
after_scenario: local_after_scenario
},
%{
config: %{after_scenario: global_after_scenario}
config: %{after_scenario: global_after_scenario},
scenario_input: input
}) do
if local_after_scenario, do: local_after_scenario.()
if global_after_scenario, do: global_after_scenario.()
if local_after_scenario, do: local_after_scenario.(input)
if global_after_scenario, do: global_after_scenario.(input)
end

defp measure_runtimes(scenario, context, run_time, fast_warning)
Expand Down
44 changes: 29 additions & 15 deletions test/benchee/benchmark/runner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ defmodule Benchee.Benchmark.RunnerTest do
before_each: fn(input) -> send(me, :before); input end,
after_each: fn(_) -> send(me, :after) end,
before_scenario: fn(input) -> send(me, :before_scenario); input end,
after_scenario: fn -> send(me, :after_scenario) end})
after_scenario: fn(_) -> send(me, :after_scenario) end})
|> Benchmark.measure(TestPrinter)

assert_received_exactly [
Expand All @@ -297,7 +297,7 @@ defmodule Benchee.Benchmark.RunnerTest do
before_each: fn(input) -> send(me, :before); input end,
after_each: fn(_) -> send(me, :after) end,
before_scenario: fn(input) -> send(me, :before_scenario); input end,
after_scenario: fn -> send(me, :after_scenario) end})
after_scenario: fn(_) -> send(me, :after_scenario) end})
|> Benchmark.measure(TestPrinter)

assert_received_exactly [
Expand All @@ -317,7 +317,7 @@ defmodule Benchee.Benchmark.RunnerTest do
send(me, :global_before_scenario)
input
end,
after_scenario: fn -> send me, :global_after_scenario end,
after_scenario: fn(_) -> send me, :global_after_scenario end,
inputs: %{"one" => 1, "two" => 2}
}
}
Expand All @@ -330,7 +330,7 @@ defmodule Benchee.Benchmark.RunnerTest do
send(me, :local_before_scenario)
input
end,
after_scenario: fn -> send(me, :local_after_scenario) end})
after_scenario: fn(_) -> send(me, :local_after_scenario) end})
|> Benchmark.measure(TestPrinter)

assert_received_exactly [
Expand All @@ -349,7 +349,7 @@ defmodule Benchee.Benchmark.RunnerTest do
time: 100,
before_each: fn(input) -> send(me, :global_before); input end,
after_each: fn(_) -> send me, :global_after end,
after_scenario: fn -> send me, :global_after_scenario end
after_scenario: fn(_) -> send me, :global_after_scenario end
}
}
|> test_suite
Expand Down Expand Up @@ -393,7 +393,7 @@ defmodule Benchee.Benchmark.RunnerTest do
fn -> :timer.sleep 1 end,
before_each: fn(input) -> send(me, :local_2_before); input end,
after_each: fn(_) -> send me, :local_2_after end,
after_scenario: fn -> send me, :local_2_after_scenario end})
after_scenario: fn(_) -> send me, :local_2_after_scenario end})
|> Benchmark.measure(TestPrinter)

assert_received_exactly [
Expand All @@ -416,7 +416,7 @@ defmodule Benchee.Benchmark.RunnerTest do
send me, :global_before_scenario
input
end,
after_scenario: fn -> send me, :global_after_scenario end,
after_scenario: fn(_) -> send me, :global_after_scenario end,
}
}
result =
Expand All @@ -430,7 +430,7 @@ defmodule Benchee.Benchmark.RunnerTest do
send(me, :local_before_scenario)
input
end,
after_scenario: fn -> send(me, :local_after_scenario) end})
after_scenario: fn(_) -> send(me, :local_after_scenario) end})
|> Benchmark.measure(TestPrinter)

{:messages, messages} = Process.info self(), :messages
Expand Down Expand Up @@ -563,7 +563,7 @@ defmodule Benchee.Benchmark.RunnerTest do
assert_received {:local, :value}
end

test "before_* is passed the input and can adapt it to pass it on" do
test "hooks dealing with inputs can adapt it and pass it on" do
me = self()
%Suite{
configuration: %{
Expand All @@ -577,6 +577,9 @@ defmodule Benchee.Benchmark.RunnerTest do
send(me, {:global_each, input})
input + 1
end,
after_scenario: fn(input) ->
send(me, {:global_after_scenario, input})
end,
inputs: %{"basic input" => 0}
}
}
Expand All @@ -591,19 +594,23 @@ defmodule Benchee.Benchmark.RunnerTest do
input + 1
end,
before_each: fn(input) ->
send(me, {:local_each, input})
input + 1
end})
send(me, {:local_each, input})
input + 1
end,
after_scenario: fn(input) ->
send(me, {:local_after_scenario, input})
end})
|> Benchmark.measure(TestPrinter)

assert_received_exactly [
{:global_scenario, 0}, {:local_scenario, 1},
{:global_each, 2}, {:local_each, 3}, {:runner, 4},
{:global_each, 2}, {:local_each, 3}, {:runner, 4}
{:global_each, 2}, {:local_each, 3}, {:runner, 4},
{:local_after_scenario, 2}, {:global_after_scenario, 2}
]
end

test "before_* still works when there is no input given" do
test "hooks dealing with inputs still work when there is no input given" do
me = self()
%Suite{
configuration: %{
Expand All @@ -616,6 +623,9 @@ defmodule Benchee.Benchmark.RunnerTest do
before_each: fn(input) ->
send(me, {:global_each, input})
input
end,
after_scenario: fn(input) ->
send(me, {:global_after_scenario, input})
end
}
}
Expand All @@ -631,6 +641,9 @@ defmodule Benchee.Benchmark.RunnerTest do
before_each: fn(input) ->
send(me, {:local_each, input})
input
end,
after_scenario: fn(input) ->
send(me, {:local_after_scenario, input})
end})
|> Benchmark.measure(TestPrinter)

Expand All @@ -639,7 +652,8 @@ defmodule Benchee.Benchmark.RunnerTest do
assert_received_exactly [
{:global_scenario, no_input}, {:local_scenario, no_input},
{:global_each, no_input}, {:local_each, no_input},
{:global_each, no_input}, {:local_each, no_input}
{:global_each, no_input}, {:local_each, no_input},
{:local_after_scenario, no_input}, {:global_after_scenario, no_input}
]
end

Expand Down
6 changes: 3 additions & 3 deletions test/benchee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,17 @@ defmodule BencheeTest do
send myself, :local_before_scenario
input
end,
after_scenario: fn -> send myself, :local_after_scenario end},
after_scenario: fn(_) -> send myself, :local_after_scenario end},
"sleeper 2" => fn -> :timer.sleep 1 end
}, time: 0.0001,
warmup: 0,
before_each: fn(input) -> send(myself, :global_before); input end,
after_each: fn(_) -> send myself, :global_after end,
after_each: fn(_) -> send myself, :global_after end,
before_scenario: fn(input) ->
send myself, :global_before_scenario
input
end,
after_scenario: fn -> send myself, :global_after_scenario end
after_scenario: fn(_) -> send myself, :global_after_scenario end
end

assert_received_exactly [
Expand Down

0 comments on commit a1f4df8

Please sign in to comment.