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 ff04ee9 commit 4abd6c1
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 36 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
4 changes: 2 additions & 2 deletions test/benchee/formatters/console/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule Benchee.Formatters.Console.MemoryTest do
std_dev_ratio: 0.1,
median: 195.5,
percentiles: %{99 => 300.1},
sample_size: 10
sample_size: 10
},
run_time_statistics: %Statistics{average: 100.0, ips: 1_000.0}
},
Expand Down Expand Up @@ -296,7 +296,7 @@ defmodule Benchee.Formatters.Console.MemoryTest do

test "does nothing when there's no statistics to format" do
scenarios = [%Scenario{memory_usage_statistics: %Statistics{sample_size: 0}}]

assert [] = Memory.format_scenarios(scenarios, %{})
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/benchee/formatters/console/run_time_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ defmodule Benchee.Formatters.Console.RunTimeTest do
std_dev_ratio: 0.1,
median: 90.0,
percentiles: %{99 => 500.1},
sample_size: 400
sample_size: 400
},
memory_usage_statistics: %Statistics{}
},
Expand Down Expand Up @@ -328,9 +328,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},
sample_size: 200
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 @@ -12,7 +12,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
Expand Down
41 changes: 35 additions & 6 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}
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}
Expand All @@ -49,7 +73,12 @@ defmodule Benchee.StatistcsTest do

@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}]
scenarios = [
%Scenario{
run_times: @standard_deviation_sample,
memory_usages: @standard_deviation_sample
}
]
suite = Statistics.statistics(%Suite{scenarios: scenarios})

[%Scenario{run_time_statistics: stats}] = suite.scenarios
Expand Down Expand Up @@ -91,7 +120,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 @@ -462,7 +463,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 @@ -472,7 +476,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 4abd6c1

Please sign in to comment.