Skip to content

Commit

Permalink
Format everything (#227)
Browse files Browse the repository at this point in the history
* Format some files

* Format more test files

* Finish the tests and move on the the lib folder

* Format the higher level files

* Format utility and statistics folders

* Format formatters and conversion folders

* Finish formatting and add formatting to CI
  • Loading branch information
devonestes authored and PragTob committed May 31, 2018
1 parent e885f6b commit b0353c7
Show file tree
Hide file tree
Showing 43 changed files with 1,018 additions and 783 deletions.
8 changes: 8 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
inputs: [
"lib/**/*.{ex,exs}",
"test/**/*.{ex,exs}",
"mix/**/*.{ex,exs}",
"./mix.exs"
]
]
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ elixir:
otp_release:
- 19.3
- 20.3
matrix:
exclude:
- elixir: 1.6.4
otp_release: 18.3

before_script:
- MIX_ENV=test mix compile --warnings-as-errors
- travis_wait mix dialyzer --plt
script:
- mix credo --strict
- # skip dialyzer for elixir 1.4 and erlang 18 as it produces weird errors, see #69
- if ! ([[ "$TRAVIS_ELIXIR_VERSION" == "1.4"* ]] && [[ "$TRAVIS_OTP_RELEASE" == "18"* ]]); then mix dialyzer --halt-exit-status; fi
- if [[ "$TRAVIS_ELIXIR_VERSION" == "1.6"* ]]; then mix format --check-formatted; fi
- mix dialyzer --halt-exit-status
- mix safe_coveralls.travis
after_script:
- mix deps.get --only docs
Expand Down
36 changes: 20 additions & 16 deletions lib/benchee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ for {module, moduledoc} <- [{Benchee, elixir_doc}, {:benchee, erlang_doc}] do
"""
def run(jobs, config \\ [])

def run(jobs, config) when is_list(config) do
do_run(jobs, config)
end

def run(config, jobs) when is_map(jobs) do
# pre 0.6.0 way of passing in the config first and as a map
do_run(jobs, config)
Expand All @@ -54,22 +56,23 @@ for {module, moduledoc} <- [{Benchee, elixir_doc}, {:benchee, erlang_doc}] do

defp run_benchmarks(jobs, config) do
config
|> Benchee.init
|> Benchee.system
|> Benchee.init()
|> Benchee.system()
|> add_benchmarking_jobs(jobs)
|> Benchee.measure
|> Benchee.statistics
|> Benchee.load
|> Benchee.measure()
|> Benchee.statistics()
|> Benchee.load()
end

defp output_results(suite = %{configuration: %{formatters: formatters}}) do
{parallelizable, serial} = Enum.split_with(formatters, &is_formatter_module?/1)

# why do we ignore this suite? It shouldn't be changed anyway.
_suite = Formatter.parallel_output(suite, parallelizable)
Enum.each serial, fn(output_function) ->

Enum.each(serial, fn output_function ->
output_function.(suite)
end
end)

suite
end
Expand All @@ -81,22 +84,23 @@ for {module, moduledoc} <- [{Benchee, elixir_doc}, {:benchee, erlang_doc}] do
|> Keyword.get(:behaviour, [])
|> Enum.member?(Benchee.Formatter)
end

defp is_formatter_module?(_), do: false

defp add_benchmarking_jobs(suite, jobs) do
Enum.reduce jobs, suite, fn({key, function}, suite_acc) ->
Enum.reduce(jobs, suite, fn {key, function}, suite_acc ->
Benchee.benchmark(suite_acc, key, function)
end
end)
end

defdelegate init(), to: Benchee.Configuration
defdelegate init(config), to: Benchee.Configuration
defdelegate system(suite), to: Benchee.System
defdelegate measure(suite), to: Benchee.Benchmark
defdelegate measure(suite, printer), to: Benchee.Benchmark
defdelegate init(), to: Benchee.Configuration
defdelegate init(config), to: Benchee.Configuration
defdelegate system(suite), to: Benchee.System
defdelegate measure(suite), to: Benchee.Benchmark
defdelegate measure(suite, printer), to: Benchee.Benchmark
defdelegate benchmark(suite, name, function), to: Benchee.Benchmark
defdelegate statistics(suite), to: Benchee.Statistics
defdelegate load(suite), to: Benchee.ScenarioLoader
defdelegate statistics(suite), to: Benchee.Statistics
defdelegate load(suite), to: Benchee.ScenarioLoader
defdelegate benchmark(suite, name, function, printer), to: Benchee.Benchmark
end
end
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
18 changes: 9 additions & 9 deletions lib/benchee/benchmark/hooks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ defmodule Benchee.Benchmark.Hooks do
|> run_before_function(local_before_each)
end

def run_after_each(
return_value,
%{
after_each: local_after_each
},
%{
config: %{after_each: global_after_each}
}
) do
def run_after_each(
return_value,
%{
after_each: local_after_each
},
%{
config: %{after_each: global_after_each}
}
) do
if local_after_each, do: local_after_each.(return_value)
if global_after_each, do: global_after_each.(return_value)
end
Expand Down
42 changes: 21 additions & 21 deletions lib/benchee/benchmark/repeated_measurement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ defmodule Benchee.Benchmark.RepeatedMeasurement do
@minimum_execution_time 10
@times_multiplier 10
def determine_n_times(
scenario,
scenario_context = %ScenarioContext{
num_iterations: num_iterations,
printer: printer
},
fast_warning,
measurer \\ Measure.NativeTime
) do
scenario,
scenario_context = %ScenarioContext{
num_iterations: num_iterations,
printer: printer
},
fast_warning,
measurer \\ Measure.NativeTime
) do
run_time = measure_iteration(scenario, scenario_context, measurer)

if run_time >= @minimum_execution_time do
Expand Down Expand Up @@ -67,23 +67,23 @@ defmodule Benchee.Benchmark.RepeatedMeasurement do
end

defp measure_iteration(
scenario,
scenario_context = %ScenarioContext{
num_iterations: 1
},
measurer
) do
scenario,
scenario_context = %ScenarioContext{
num_iterations: 1
},
measurer
) do
Runner.measure(scenario, scenario_context, measurer)
end

defp measure_iteration(
scenario,
scenario_context = %ScenarioContext{
num_iterations: iterations
},
measurer
)
when iterations > 1 do
scenario,
scenario_context = %ScenarioContext{
num_iterations: iterations
},
measurer
)
when iterations > 1 do
# When we have more than one iteration, then the repetition and calling
# of hooks is already included in the function, for reference/reasoning see
# `build_benchmarking_function/2`
Expand Down
4 changes: 2 additions & 2 deletions lib/benchee/benchmark/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ defmodule Benchee.Benchmark.Runner do
defp run_memory_benchmark(_, %ScenarioContext{config: %{memory_time: 0.0}}) do
[]
end

defp run_memory_benchmark(
scenario,
scenario_context = %ScenarioContext{
Expand All @@ -110,7 +111,6 @@ defmodule Benchee.Benchmark.Runner do
}
}
) do

end_time = current_time() + memory_time

new_context = %ScenarioContext{
Expand Down Expand Up @@ -187,7 +187,7 @@ defmodule Benchee.Benchmark.Runner do
def measure(
scenario = %Scenario{function: function},
scenario_context = %ScenarioContext{
num_iterations: 1,
num_iterations: 1
},
measurer
) do
Expand Down
34 changes: 17 additions & 17 deletions lib/benchee/benchmark/scenario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ defmodule Benchee.Benchmark.Scenario do
]

@type t :: %__MODULE__{
name: String.t,
job_name: String.t,
function: fun,
input_name: String.t | nil,
input: any | nil,
run_times: [float],
run_time_statistics: Benchee.Statistics.t | nil,
memory_usages: [non_neg_integer],
memory_usage_statistics: Benchee.Statistics.t | nil,
before_each: fun | nil,
after_each: fun | nil,
before_scenario: fun | nil,
after_scenario: fun | nil,
tag: String.t | nil
}
name: String.t(),
job_name: String.t(),
function: fun,
input_name: String.t() | nil,
input: any | nil,
run_times: [float],
run_time_statistics: Benchee.Statistics.t() | nil,
memory_usages: [non_neg_integer],
memory_usage_statistics: Benchee.Statistics.t() | nil,
before_each: fun | nil,
after_each: fun | nil,
before_scenario: fun | nil,
after_scenario: fun | nil,
tag: String.t() | nil
}

@doc """
Returns the correct name to display of the given scenario data.
Expand All @@ -63,8 +63,8 @@ defmodule Benchee.Benchmark.Scenario do
iex> Scenario.display_name(%{job_name: "flat_map"})
"flat_map"
"""
@spec display_name(map) :: String.t
@spec display_name(map) :: String.t()
def display_name(%{job_name: job_name, tag: nil}), do: job_name
def display_name(%{job_name: job_name, tag: tag}), do: "#{job_name} (#{tag})"
def display_name(%{job_name: job_name}), do: job_name
def display_name(%{job_name: job_name}), do: job_name
end
17 changes: 9 additions & 8 deletions lib/benchee/benchmark/scenario_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ defmodule Benchee.Benchmark.ScenarioContext do
:printer,
:current_time,
:end_time,
:scenario_input, # before_scenario can alter the original input
# before_scenario can alter the original input
:scenario_input,
num_iterations: 1
]

@type t :: %__MODULE__{
config: Benchee.Configuration.t,
printer: module,
current_time: pos_integer | nil,
end_time: pos_integer | nil,
scenario_input: any,
num_iterations: pos_integer
}
config: Benchee.Configuration.t(),
printer: module,
current_time: pos_integer | nil,
end_time: pos_integer | nil,
scenario_input: any,
num_iterations: pos_integer
}
end
Loading

0 comments on commit b0353c7

Please sign in to comment.