Skip to content

Commit

Permalink
Apply credo config to test files (good and new default)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob committed Apr 11, 2018
1 parent af17746 commit bff06eb
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 35 deletions.
3 changes: 2 additions & 1 deletion test/benchee/benchmark/runner_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Benchee.Benchmark.RunnerTest do
use ExUnit.Case, async: true
import Benchee.TestHelpers
import ExUnit.CaptureIO
alias Benchee.{Suite, Benchmark, Configuration, Statistics}
alias Benchee.Benchmark.Scenario
alias Benchee.Test.FakeBenchmarkPrinter, as: TestPrinter
Expand Down Expand Up @@ -153,7 +154,7 @@ defmodule Benchee.Benchmark.RunnerTest do

test "very fast functions print a warning" do
output =
ExUnit.CaptureIO.capture_io(fn ->
capture_io(fn ->
%Suite{configuration: %{print: %{fast_warning: true}}}
|> test_suite()
|> Benchmark.benchmark("", fn -> 1 end)
Expand Down
10 changes: 7 additions & 3 deletions test/benchee/conversion/count_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ defmodule Benchee.Conversion.CountTest do
assert best(@list_with_mostly_ones, strategy: :largest) == unit_for(:thousand)
end

@list_with_thousands_and_millions_tied_for_most [0.0001, 1, 1_000, 100_000, 1_000_000, 10_000_000, 1_000_000_000]
@list_with_thousands_and_millions_tied_for_most [
0.0001, 1, 1_000, 100_000, 1_000_000, 10_000_000, 1_000_000_000
]

test "when list has thousands and millions tied for most, billions highest" do
assert best(@list_with_thousands_and_millions_tied_for_most) == unit_for(:million)
end

test "when list has thousands and millions tied for most, billions highest, strategy: :smallest" do
assert best(@list_with_thousands_and_millions_tied_for_most, strategy: :smallest) == unit_for(:one)
best_unit = best(@list_with_thousands_and_millions_tied_for_most, strategy: :smallest)
assert best_unit == unit_for(:one)
end

test "when list has thousands and millions tied for most, billions highest, strategy: :largest" do
assert best(@list_with_thousands_and_millions_tied_for_most, strategy: :largest) == unit_for(:billion)
best_unit = best(@list_with_thousands_and_millions_tied_for_most, strategy: :largest)
assert best_unit == unit_for(:billion)
end

@list_with_mostly_thousands [1_000, 2_000, 30_000, 999]
Expand Down
10 changes: 6 additions & 4 deletions test/benchee/conversion/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ defmodule Benchee.Conversion.MemoryTest do
end
end

@list_with_mostly_megabytes [1, 200, 3_000_000, 4_000_000, 50_000_000, 50_000_000, 77_000_000_000]
@list_with_mostly_megabytes [
1, 200, 3_000_000, 4_000_000, 50_000_000, 50_000_000, 77_000_000_000
]

describe ".best" do
test "when list is mostly megabytes" do
Expand Down Expand Up @@ -62,13 +64,13 @@ defmodule Benchee.Conversion.MemoryTest do
actual_value = convert({8, @kilobyte_unit}, @kilobyte_unit)
assert actual_value == {8, @kilobyte_unit}
end

test "convert kb to mb returns correct result" do
actual_value = convert({8, @kilobyte_unit}, @megabyte_unit)
expected_value = 8 / 1024
assert actual_value == {expected_value, @megabyte_unit}
end

test "convert mb to kb returns correct result" do
actual_value = convert({8, @megabyte_unit}, @kilobyte_unit)
expected_value = 8 * 1024
Expand All @@ -87,4 +89,4 @@ defmodule Benchee.Conversion.MemoryTest do
assert actual_value == {expected_value, @kilobyte_unit}
end
end
end
end
6 changes: 3 additions & 3 deletions test/benchee/formatters/console/run_time_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ defmodule Benchee.Formatters.Console.RunTimeTest do
%Scenario{
name: "Job",
run_time_statistics: %Statistics{
average: 11000.0,
ips: 12000.0,
std_dev_ratio: 13000.0,
average: 11_000.0,
ips: 12_000.0,
std_dev_ratio: 13_000.0,
median: 140_000.0,
percentiles: %{99 => 200_000.1}
},
Expand Down
2 changes: 1 addition & 1 deletion test/benchee/output/benchmark_printer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Benchee.Output.BenchmarkPrintertest do
os: :macOS,
num_cores: 4,
cpu_speed: "Intel(R) Core(TM) i5-4260U CPU @ 1.40GHz",
available_memory: 8568392814}
available_memory: 8_568_392_814}

test ".duplicate_benchmark_warning" do
output = capture_io fn ->
Expand Down
49 changes: 41 additions & 8 deletions test/benchee/statistics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ defmodule Benchee.StatistcsTest do
describe ".statistics" do
test "computes the statistics for all jobs correctly" do
scenarios = [
%Scenario{input: "Input", input_name: "Input", job_name: "Job 1", run_times: @sample_1, memory_usages: @sample_1},
%Scenario{input: "Input", input_name: "Input", job_name: "Job 2", run_times: @sample_2, memory_usages: @sample_2}
%Scenario{
input: "Input",
input_name: "Input",
job_name: "Job 1",
run_times: @sample_1,
memory_usages: @sample_1
},
%Scenario{
input: "Input",
input_name: "Input",
job_name: "Job 2",
run_times: @sample_2,
memory_usages: @sample_2
}
]

suite = %Suite{scenarios: scenarios, configuration: %{measure_memory: true}}
Expand All @@ -24,8 +36,20 @@ defmodule Benchee.StatistcsTest do

test "computes statistics correctly for multiple inputs" do
scenarios = [
%Scenario{input: "Input 1", input_name: "Input 1", job_name: "Job", run_times: @sample_1, memory_usages: @sample_1},
%Scenario{input: "Input 2", input_name: "Input 2", job_name: "Job", run_times: @sample_2, memory_usages: @sample_2}
%Scenario{
input: "Input 1",
input_name: "Input 1",
job_name: "Job",
run_times: @sample_1,
memory_usages: @sample_1
},
%Scenario{
input: "Input 2",
input_name: "Input 2",
job_name: "Job",
run_times: @sample_2,
memory_usages: @sample_2
}
]

suite = %Suite{scenarios: scenarios, configuration: %{measure_memory: true}}
Expand All @@ -41,16 +65,25 @@ defmodule Benchee.StatistcsTest do
@mode_sample [55, 40, 67, 55, 44, 40, 10, 8, 55, 90, 67]
test "mode is calculated correctly" do
scenarios = [%Scenario{run_times: @mode_sample, memory_usages: @mode_sample}]
suite = Statistics.statistics(%Suite{scenarios: scenarios, configuration: %{measure_memory: false}})
suite =
%Suite{scenarios: scenarios, configuration: %{measure_memory: false}}
|> Statistics.statistics()

[%Scenario{run_time_statistics: stats}] = suite.scenarios
assert stats.mode == 55
end

@standard_deviation_sample [600, 470, 170, 430, 300]
test "statistical standard deviation is calculated correctly" do
scenarios = [%Scenario{run_times: @standard_deviation_sample, memory_usages: @standard_deviation_sample}]
suite = Statistics.statistics(%Suite{scenarios: scenarios, configuration: %{measure_memory: false}})
scenarios = [
%Scenario{
run_times: @standard_deviation_sample,
memory_usages: @standard_deviation_sample
}
]
suite =
%Suite{scenarios: scenarios, configuration: %{measure_memory: false}}
|> Statistics.statistics()

[%Scenario{run_time_statistics: stats}] = suite.scenarios
assert_in_delta stats.std_dev, 164.7, 0.1
Expand Down Expand Up @@ -91,7 +124,7 @@ defmodule Benchee.StatistcsTest do
assert stats.average == 14.0
assert_in_delta stats.std_dev, 5.76, 0.01
assert_in_delta stats.std_dev_ratio, 0.41, 0.01
assert_in_delta stats.ips, 71428, 1
assert_in_delta stats.ips, 71_428, 1
assert stats.median == 14.0
assert stats.minimum == 7
assert stats.maximum == 23
Expand Down
2 changes: 1 addition & 1 deletion test/benchee/utility/repeat_n_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ defmodule Benchee.Utility.RepeatNTest do
refute_receive :called
end
defp assert_called_exactly_times(n) do
Enum.each(Enum.to_list(1..n), fn (_)-> assert_receive :called end)
Enum.each(Enum.to_list(1..n), fn _ -> assert_receive :called end)
end
end
23 changes: 15 additions & 8 deletions test/benchee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule BencheeTest do
alias Benchee.Statistics
alias Benchee.Formatters.Console
alias Benchee.Suite
alias Benchee.Conversion.Duration

doctest Benchee

@header_regex ~r/^Name.+ips.+average.+deviation.+median.+99th %$/m
Expand Down Expand Up @@ -109,7 +111,7 @@ defmodule BencheeTest do
fn -> list |> Enum.map(map_fun) |> List.flatten end)
|> Benchee.measure
|> Benchee.statistics
|> Benchee.Formatters.Console.format
|> Console.format
|> IO.puts
end

Expand Down Expand Up @@ -185,7 +187,7 @@ defmodule BencheeTest do
fn -> list |> Enum.map(map_fun) |> List.flatten end
}, time: 0.01,
warmup: 0.005,
formatters: [Benchee.Formatters.Console])
formatters: [Console])
end

readme_sample_asserts output
Expand All @@ -202,7 +204,7 @@ defmodule BencheeTest do
fn -> list |> Enum.map(map_fun) |> List.flatten end
}, time: 0.01,
warmup: 0.005,
formatters: [&Benchee.Formatters.Console.output/1])
formatters: [&Console.output/1])
end

readme_sample_asserts output
Expand Down Expand Up @@ -235,15 +237,15 @@ defmodule BencheeTest do
formatter_one = fn(suite) ->
run_time = suite.scenarios
|> (fn([scenario | _]) -> List.last(scenario.run_times) end).()
|> Benchee.Conversion.Duration.format
|> Duration.format

IO.puts "Run time: #{run_time}"
end

formatter_two = fn(suite) ->
average = suite.scenarios
|> (fn([scenario | _]) -> scenario.run_time_statistics.average end).()
|> Benchee.Conversion.Duration.format
|> Duration.format
IO.puts "Average: #{average}"
end

Expand Down Expand Up @@ -296,7 +298,6 @@ defmodule BencheeTest do

configuration = Keyword.merge @test_times, inputs


Benchee.run(%{
"flat_map" => fn(input) -> Enum.flat_map(input, map_fun) end,
"map.flatten" =>
Expand Down Expand Up @@ -449,7 +450,10 @@ defmodule BencheeTest do
Benchee.run %{
"sleeper" => {
fn -> :timer.sleep 1 end,
before_each: fn(input) -> send(myself, :local_before); input end,
before_each: fn(input) ->
send(myself, :local_before)
input
end,
after_each: fn(_) -> send myself, :local_after end,
before_scenario: fn(input) ->
send myself, :local_before_scenario
Expand All @@ -459,7 +463,10 @@ defmodule BencheeTest do
"sleeper 2" => fn -> :timer.sleep 1 end
}, time: 0.0001,
warmup: 0,
before_each: fn(input) -> send(myself, :global_before); input end,
before_each: fn(input) ->
send(myself, :global_before)
input
end,
after_each: fn(_) -> send myself, :global_after end,
before_scenario: fn(input) ->
send myself, :global_before_scenario
Expand Down
2 changes: 2 additions & 0 deletions test/support/fake_benchmark_printer.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Benchee.Test.FakeBenchmarkPrinter do
@moduledoc false

def duplicate_benchmark_warning(name) do
send(self(), {:duplicate, name})
end
Expand Down
2 changes: 2 additions & 0 deletions test/support/fake_benchmark_runner.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Benchee.Test.FakeBenchmarkRunner do
@moduledoc false

def run_scenarios(scenarios, scenario_context) do
send(self(), {:run_scenarios, scenarios, scenario_context})

Expand Down
2 changes: 2 additions & 0 deletions test/support/fake_formatter.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Benchee.Test.FakeFormatter do
@moduledoc false

@behaviour Benchee.Formatter

def format(_) do
Expand Down
12 changes: 6 additions & 6 deletions test/support/test_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Benchee.TestHelpers do
@moduledoc false

import ExUnit.Assertions

@default_retries 10
Expand All @@ -12,12 +14,10 @@ defmodule Benchee.TestHelpers do
end

def retrying(asserting_function, n) do
try do
asserting_function.()
rescue
ExUnit.AssertionError ->
retrying(asserting_function, n - 1)
end
asserting_function.()
rescue
ExUnit.AssertionError ->
retrying(asserting_function, n - 1)
end

def assert_received_exactly(expected) do
Expand Down

0 comments on commit bff06eb

Please sign in to comment.