Skip to content

Commit

Permalink
Format the higher level files
Browse files Browse the repository at this point in the history
  • Loading branch information
devonestes committed May 30, 2018
1 parent 165f16b commit 876e9d6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
58 changes: 42 additions & 16 deletions lib/benchee/benchmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Benchee.Benchmark do
alias Benchee.Benchmark.{Scenario, ScenarioContext, Runner}
alias Benchee.Utility.DeepConvert

@type job_name :: String.t | atom
@type job_name :: String.t() | atom
@no_input :__no_input

@doc """
Expand All @@ -23,9 +23,10 @@ defmodule Benchee.Benchmark do
suite's config, a scenario will be added for the given function for each
input.
"""
@spec benchmark(Suite.t, job_name, fun, module) :: Suite.t
@spec benchmark(Suite.t(), job_name, fun, module) :: Suite.t()
def benchmark(suite = %Suite{scenarios: scenarios}, job_name, function, printer \\ Printer) do
normalized_name = to_string(job_name)

if duplicate?(scenarios, normalized_name) do
printer.duplicate_benchmark_warning(normalized_name)
suite
Expand All @@ -35,28 +36,50 @@ defmodule Benchee.Benchmark do
end

defp duplicate?(scenarios, job_name) do
Enum.any?(scenarios, fn(scenario) -> scenario.name == job_name end)
Enum.any?(scenarios, fn scenario -> scenario.name == job_name end)
end

defp add_scenario(suite = %Suite{scenarios: scenarios, configuration: config},
job_name, function) do
defp add_scenario(
suite = %Suite{scenarios: scenarios, configuration: config},
job_name,
function
) do
new_scenarios = build_scenarios_for_job(job_name, function, config)
%Suite{suite | scenarios: List.flatten([scenarios | new_scenarios])}
end

defp build_scenarios_for_job(job_name, function, config)

defp build_scenarios_for_job(job_name, function, nil) do
[build_scenario(%{job_name: job_name, function: function, input: @no_input,
input_name: @no_input})]
[
build_scenario(%{
job_name: job_name,
function: function,
input: @no_input,
input_name: @no_input
})
]
end

defp build_scenarios_for_job(job_name, function, %{inputs: nil}) do
[build_scenario(%{job_name: job_name, function: function, input: @no_input,
input_name: @no_input})]
[
build_scenario(%{
job_name: job_name,
function: function,
input: @no_input,
input_name: @no_input
})
]
end

defp build_scenarios_for_job(job_name, function, %{inputs: inputs}) do
Enum.map(inputs, fn({input_name, input}) ->
build_scenario(%{job_name: job_name, function: function, input: input,
input_name: input_name})
Enum.map(inputs, fn {input_name, input} ->
build_scenario(%{
job_name: job_name,
function: function,
input: input,
input_name: input_name
})
end)
end

Expand All @@ -66,6 +89,7 @@ defmodule Benchee.Benchmark do
|> Map.merge(DeepConvert.to_map(options))
|> build_scenario
end

defp build_scenario(scenario_data) do
struct!(Scenario, add_scenario_name(scenario_data))
end
Expand All @@ -80,10 +104,12 @@ defmodule Benchee.Benchmark do
information on how bencharmks are actually run, see
`Benchee.Benchmark.Runner.run_scenarios/2`.
"""
@spec measure(Suite.t, module, module) :: Suite.t
def measure(suite = %Suite{scenarios: scenarios, configuration: config},
printer \\ Printer,
runner \\ Runner) do
@spec measure(Suite.t(), module, module) :: Suite.t()
def measure(
suite = %Suite{scenarios: scenarios, configuration: config},
printer \\ Printer,
runner \\ Runner
) do
printer.configuration_information(suite)
scenario_context = %ScenarioContext{config: config, printer: printer}
scenarios = runner.run_scenarios(scenarios, scenario_context)
Expand Down
2 changes: 1 addition & 1 deletion lib/benchee/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ defmodule Benchee.Configuration do
defp update_measure_memory(config = %{memory_time: memory_time}) do
otp_version = List.to_integer(:erlang.system_info(:otp_release))

if (memory_time > 0) and otp_version <= 18 do
if memory_time > 0 and otp_version <= 18 do
print_memory_measure_warning()
Map.put(config, :memory_time, 0)
else
Expand Down
21 changes: 11 additions & 10 deletions lib/benchee/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ defmodule Benchee.Formatter do
of multiple formatters in parallel. The result will then be passed to
`write/1`.
"""
@callback format(Suite.t) :: any
@callback format(Suite.t()) :: any

@doc """
Takes the return value of `format/1` and then performs some I/O for the user
to actually see the formatted data (UI, File IO, HTTP, ...)
"""
@callback write(any) :: :ok | {:error, String.t}
@callback write(any) :: :ok | {:error, String.t()}

@doc """
Combines `format/1` and `write/1` into a single convenience function that is
also chainable (as it takes a suite and returns a suite).
"""
@callback output(Suite.t) :: Suite.t
@callback output(Suite.t()) :: Suite.t()

defmacro __using__(_) do
quote location: :keep do
Expand All @@ -44,11 +44,12 @@ defmodule Benchee.Formatter do
Combines `format/1` and `write/1` into a single convenience function that
is also chainable (as it takes a suite and returns a suite).
"""
@spec output(Benchee.Suite.t) :: Benchee.Suite.t
@spec output(Benchee.Suite.t()) :: Benchee.Suite.t()
def output(suite) do
:ok = suite
|> format
|> write
:ok =
suite
|> format
|> write

suite
end
Expand All @@ -60,11 +61,11 @@ defmodule Benchee.Formatter do
behaviour. The output for all formatters are generated in parallel, and then
the results of that formatting are written in sequence.
"""
@spec parallel_output(Suite.t, [module]) :: Suite.t
@spec parallel_output(Suite.t(), [module]) :: Suite.t()
def parallel_output(suite, modules) do
modules
|> Parallel.map(fn(module) -> {module, module.format(suite)} end)
|> Enum.each(fn({module, output}) -> module.write(output) end)
|> Parallel.map(fn module -> {module, module.format(suite)} end)
|> Enum.each(fn {module, output} -> module.write(output) end)

suite
end
Expand Down
14 changes: 8 additions & 6 deletions lib/benchee/scenario_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ defmodule Benchee.ScenarioLoader do

defp load_scenarios(false), do: []
defp load_scenarios(path) when is_binary(path), do: load_scenarios([path])

defp load_scenarios(paths) do
Enum.flat_map paths, fn(path_or_glob) ->
Enum.flat_map Path.wildcard(path_or_glob), &load_scenario/1
end
Enum.flat_map(paths, fn path_or_glob ->
Enum.flat_map(Path.wildcard(path_or_glob), &load_scenario/1)
end)
end

defp load_scenario(path) do
loaded_suite = path
|> File.read!
|> :erlang.binary_to_term
loaded_suite =
path
|> File.read!()
|> :erlang.binary_to_term()

loaded_suite.scenarios
end
Expand Down
7 changes: 4 additions & 3 deletions lib/benchee/statistics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,15 @@ defmodule Benchee.Statistics do

defp add_ips(statistics = %__MODULE__{sample_size: 0}), do: statistics
defp add_ips(statistics = %__MODULE__{average: 0.0}), do: statistics

defp add_ips(statistics) do
ips = Duration.convert_value({1, :second}, :nanosecond) / statistics.average
standard_dev_ips = ips * statistics.std_dev_ratio

%__MODULE__{
statistics |
ips: ips,
std_dev_ips: standard_dev_ips
statistics
| ips: ips,
std_dev_ips: standard_dev_ips
}
end

Expand Down

0 comments on commit 876e9d6

Please sign in to comment.