Skip to content

Commit

Permalink
Format Benchee.BenchmarkTest
Browse files Browse the repository at this point in the history
We've got a lot of formatting to do. Might as well start with some of
our low churn code! I was thinking we'd take it file by file like they
did with the Elixir codebase itself instead of trying to do one huge
piece.

Once we have everything formatted then we can add a formatter check to
CI so we stay that way.
  • Loading branch information
devonestes committed Jan 14, 2018
1 parent d50bf45 commit 2a09b66
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions test/benchee/benchmark_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ defmodule Benchee.BenchmarkTest do
alias Benchee.Configuration
alias Benchee.Benchmark.{Scenario, ScenarioContext}
alias Benchee.Test.FakeBenchmarkPrinter, as: TestPrinter
alias Benchee.Test.FakeBenchmarkRunner, as: TestRunner
alias Benchee.Test.FakeBenchmarkRunner, as: TestRunner
alias Benchee.Suite

describe ".benchmark" do
test "can add jobs with atom keys but converts them to string" do
suite = %Suite{}
|> Benchmark.benchmark("one job", fn -> 1 end)
|> Benchmark.benchmark(:something, fn -> 2 end)
suite =
%Suite{}
|> Benchmark.benchmark("one job", fn -> 1 end)
|> Benchmark.benchmark(:something, fn -> 2 end)

job_names = Enum.map(suite.scenarios, fn(scenario) -> scenario.job_name end)
job_names = Enum.map(suite.scenarios, fn scenario -> scenario.job_name end)
assert job_names == ["one job", "something"]
names = Enum.map(suite.scenarios, fn(scenario) -> scenario.job_name end)
names = Enum.map(suite.scenarios, fn scenario -> scenario.job_name end)
assert names == ["one job", "something"]
end

Expand All @@ -35,54 +36,56 @@ defmodule Benchee.BenchmarkTest do
function = fn -> 1 end
config = %Configuration{inputs: nil}
suite = Benchmark.benchmark(%Suite{configuration: config}, job_name, function)
expected_scenario =
%Scenario{
name: job_name,
job_name: job_name,
function: function,
input: Benchmark.no_input(),
input_name: Benchmark.no_input()
}

expected_scenario = %Scenario{
name: job_name,
job_name: job_name,
function: function,
input: Benchmark.no_input(),
input_name: Benchmark.no_input()
}

assert suite.scenarios == [expected_scenario]
end

test "adds a %Scenario{} to the suite for each input of a job" do
job_name = "one job"
function = fn -> 1 end
config = %Configuration{inputs: %{"large" => 100_000, "small" => 10}}
suite = Benchmark.benchmark(%Suite{configuration: config}, job_name, function)
input_names = Enum.map(suite.scenarios, fn(scenario) -> scenario.input_name end)
inputs = Enum.map(suite.scenarios, fn(scenario) -> scenario.input end)
suite = Benchmark.benchmark(%Suite{configuration: config}, "one_job", fn -> 1 end)
input_names = Enum.map(suite.scenarios, fn scenario -> scenario.input_name end)
inputs = Enum.map(suite.scenarios, fn scenario -> scenario.input end)

assert length(suite.scenarios) == 2
assert input_names == ["large", "small"]
assert inputs == [100_000, 10]
end

test "can deal with the options tuple" do
function = fn -> 1 end
before = fn -> 2 end
function = fn -> 1 end
before = fn -> 2 end
after_scenario = fn -> 3 end

suite =
%Suite{}
|> Benchmark.benchmark("job", {
function, before_each: before, after_scenario: after_scenario})
|> Benchmark.benchmark(
"job",
{function, before_each: before, after_scenario: after_scenario}
)

[scenario] = suite.scenarios

assert %{
job_name: "job",
function: ^function,
before_each: ^before,
after_scenario: ^after_scenario } = scenario
job_name: "job",
function: ^function,
before_each: ^before,
after_scenario: ^after_scenario
} = scenario
end

test "doesn't treat tagged scenarios as duplicates" do
suite =
%Suite{scenarios: [%Scenario{job_name: "job", tag: "what"}]}
|> Benchmark.benchmark("job", fn -> 1 end)
suite = %Suite{scenarios: [%Scenario{job_name: "job", tag: "what"}]}
new_suite = Benchmark.benchmark(suite, "job", fn -> 1 end)

assert length(suite.scenarios) == 2
assert length(new_suite.scenarios) == 2
end
end

Expand All @@ -105,16 +108,17 @@ defmodule Benchee.BenchmarkTest do
end

test "returns a suite with scenarios returned from the runner" do
scenarios = [%Scenario{job_name: "one", function: fn() -> 1 end}]
scenarios = [%Scenario{job_name: "one", function: fn -> 1 end}]
suite = %Suite{scenarios: scenarios}

run_times = suite
|> Benchmark.measure(TestPrinter, TestRunner)
|> (fn(suite) -> suite.scenarios end).()
|> Enum.map(fn(scenario) -> scenario.run_times end)
run_times =
suite
|> Benchmark.measure(TestPrinter, TestRunner)
|> (fn suite -> suite.scenarios end).()
|> Enum.map(fn scenario -> scenario.run_times end)

assert length(run_times) == 1
refute Enum.any?(run_times, fn(run_time) -> Enum.empty?(run_time) end)
refute Enum.any?(run_times, fn run_time -> Enum.empty?(run_time) end)
end
end
end

0 comments on commit 2a09b66

Please sign in to comment.