Skip to content

Commit

Permalink
Allow just measuring memory
Browse files Browse the repository at this point in the history
Made best_unit more robust as that's where it was blowing up
  • Loading branch information
PragTob committed May 1, 2018
1 parent 71cfd80 commit 48fdd74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/benchee/conversion/scale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,32 @@ defmodule Benchee.Conversion.Scale do
iex> list = [1, 101, 1_001, 10_001, 100_001, 1_000_001]
iex> Benchee.Conversion.Scale.best_unit(list, Benchee.Conversion.Count, strategy: :largest).name
:million
iex> list = []
iex> Benchee.Conversion.Scale.best_unit(list, Benchee.Conversion.Count, strategy: :best).name
:one
iex> list = [nil]
iex> Benchee.Conversion.Scale.best_unit(list, Benchee.Conversion.Count, strategy: :best).name
:one
iex> list = [nil, nil, nil, nil]
iex> Benchee.Conversion.Scale.best_unit(list, Benchee.Conversion.Count, strategy: :best).name
:one
iex> list = [nil, nil, nil, nil, 2_000]
iex> Benchee.Conversion.Scale.best_unit(list, Benchee.Conversion.Count, strategy: :best).name
:thousand
"""
def best_unit([], module, _) do
def best_unit(measurements, module, options) do
do_best_unit(Enum.reject(measurements, &is_nil/1), module, options)
end

defp do_best_unit([], module, _) do
module.base_unit
end

def best_unit(list, module, opts) do
defp do_best_unit(list, module, opts) do
case Keyword.get(opts, :strategy, :best) do
:best -> best_unit(list, module)
:largest -> largest_unit(list, module)
Expand Down
14 changes: 14 additions & 0 deletions test/benchee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,20 @@ defmodule BencheeTest do
refute output =~ "never execute me"
end

test "does not blow up when only measuring memory times" do
output = capture_io fn ->
Benchee.run(
%{
"something" => fn -> Enum.map(1..100, fn i -> i + 1 end) end
},
time: 0, warmup: 0, memory_time: 0.001
)
end

refute output =~ ~r/ips/i # no runtime statistics displayed
assert output =~ ~r/memory.+statistics/i
end

describe "save & load" do
test "saving the suite to disk and restoring it" do
save = [save: [path: "save.benchee", tag: "master"]]
Expand Down

0 comments on commit 48fdd74

Please sign in to comment.