Skip to content

Commit

Permalink
Remove custom collectors that didn't really collect
Browse files Browse the repository at this point in the history
They are better served by `Runner.run_once` so we don't need to
have pretend collector implementations.
  • Loading branch information
PragTob committed Jan 5, 2025
1 parent 27fda51 commit ad085fb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 45 deletions.
21 changes: 0 additions & 21 deletions lib/benchee/benchmark/collect/profile.ex

This file was deleted.

15 changes: 0 additions & 15 deletions lib/benchee/benchmark/collect/return_value.ex

This file was deleted.

20 changes: 16 additions & 4 deletions lib/benchee/benchmark/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ defmodule Benchee.Benchmark.Runner do
# This will run the given scenario exactly once, including the before and
# after hooks, to ensure the function can execute without raising an error.
defp pre_check(scenario, scenario_context) do
run_once(scenario, scenario_context, Collect.ReturnValue)
run_once(scenario, scenario_context)
end

def run_once(scenario, scenario_context, collector) do
def run_once(scenario, scenario_context) do
scenario_input = Hooks.run_before_scenario(scenario, scenario_context)
scenario_context = %ScenarioContext{scenario_context | scenario_input: scenario_input}
collected = collect(scenario, scenario_context, collector)
return_value = collect_return_value(scenario, scenario_context)
_ = Hooks.run_after_scenario(scenario, scenario_context)
collected
return_value
end

defp all_same(scenarios, scenario_context) do
Expand Down Expand Up @@ -301,6 +301,18 @@ defmodule Benchee.Benchmark.Runner do
defp updated_measurements(nil, measurements), do: measurements
defp updated_measurements(measurement, measurements), do: [measurement | measurements]

# Support functionality that just runs once via `run_once` and does not care about measurements.
# At the time of this writing that's pre checks and profilers.
defp collect_return_value(scenario, scenario_context) do
new_input = Hooks.run_before_each(scenario, scenario_context)
function = main_function(scenario.function, new_input)

return_value = function.()

Hooks.run_after_each(return_value, scenario, scenario_context)
return_value
end

@doc """
Takes one measure with the given collector.
Expand Down
10 changes: 5 additions & 5 deletions lib/benchee/profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule Benchee.Profile do
"""

alias Benchee.Benchmark.BenchmarkConfig
alias Benchee.Benchmark.Collect
alias Benchee.Benchmark.Runner
alias Benchee.Benchmark.ScenarioContext
alias Benchee.Output.ProfilePrinter, as: Printer
Expand Down Expand Up @@ -99,10 +98,11 @@ defmodule Benchee.Profile do
) do
printer.profiling(scenario.name, profiler)

Runner.run_once(
scenario,
%ScenarioContext{config: BenchmarkConfig.from(config)},
{Collect.Profile, [profiler_module: profiler_module, profiler_opts: profiler_opts]}
profiler_module.profile(
fn ->
Runner.run_once(scenario, %ScenarioContext{config: BenchmarkConfig.from(config)})
end,
profiler_opts
)
end

Expand Down

0 comments on commit ad085fb

Please sign in to comment.