Skip to content

Commit

Permalink
Format utility and statistics folders
Browse files Browse the repository at this point in the history
  • Loading branch information
devonestes committed May 30, 2018
1 parent 876e9d6 commit b947c98
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
23 changes: 13 additions & 10 deletions lib/benchee/statistics/mode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ defmodule Benchee.Statistics.Mode do
"""
def mode(samples) do
samples
|> Enum.reduce(%{}, fn(sample, counts) ->
Map.update(counts, sample, 1, fn(old_value) -> old_value + 1 end)
end)
|> Enum.reduce(%{}, fn sample, counts ->
Map.update(counts, sample, 1, fn old_value -> old_value + 1 end)
end)
|> max_multiple
|> decide_mode
end
Expand All @@ -29,22 +29,25 @@ defmodule Benchee.Statistics.Mode do
end

defp max_multiple([{sample, count} | rest], ref = [{_, max_count} | _]) do
new_ref = cond do
count < max_count -> ref
count == max_count -> [{sample, count} | ref]
true -> [{sample, count}]
end
new_ref =
cond do
count < max_count -> ref
count == max_count -> [{sample, count} | ref]
true -> [{sample, count}]
end

max_multiple(rest, new_ref)
end

defp max_multiple([], ref) do
ref
end

defp decide_mode([{nil, _}]), do: nil
defp decide_mode([{_, 1} | _rest]), do: nil
defp decide_mode([{sample, _count}]), do: sample

defp decide_mode(multi_modes) do
Enum.map multi_modes, fn({sample, _}) -> sample end
Enum.map(multi_modes, fn {sample, _} -> sample end)
end

end
9 changes: 5 additions & 4 deletions lib/benchee/statistics/percentile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ defmodule Benchee.Statistics.Percentile do
def percentiles(samples, percentile_ranks) do
number_of_samples = length(samples)
sorted_samples = Enum.sort(samples)

percentile_ranks
|> List.wrap
|> List.wrap()
|> Enum.reduce(%{}, fn percentile_rank, acc ->
perc = percentile(sorted_samples, number_of_samples, percentile_rank)
Map.put(acc, percentile_rank, perc)
Expand All @@ -55,7 +56,7 @@ defmodule Benchee.Statistics.Percentile do
end

defp percentile(sorted_samples, number_of_samples, percentile_rank) do
rank = (percentile_rank / 100) * max(0, number_of_samples + 1)
rank = percentile_rank / 100 * max(0, number_of_samples + 1)
percentile_value(sorted_samples, rank)
end

Expand Down Expand Up @@ -83,7 +84,7 @@ defmodule Benchee.Statistics.Percentile do
# Nothing beyond the split: use the last value before the split
defp calculate_percentile_value(_, previous_values, []) do
previous_values
|> Enum.reverse
|> Enum.reverse()
|> hd
|> to_float
end
Expand All @@ -102,6 +103,6 @@ defmodule Benchee.Statistics.Percentile do
end

defp to_float(maybe_integer) do
:erlang.float maybe_integer
:erlang.float(maybe_integer)
end
end
6 changes: 3 additions & 3 deletions lib/benchee/utility/deep_convert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ defmodule Benchee.Utility.DeepConvert do

defp do_to_map(kwlist = [{_key, _value} | _tail]) do
kwlist
|> Enum.map(fn({key, value}) -> {key, do_to_map(value)} end)
|> Map.new
|> Enum.map(fn {key, value} -> {key, do_to_map(value)} end)
|> Map.new()
end
defp do_to_map(no_list), do: no_list

defp do_to_map(no_list), do: no_list
end
42 changes: 25 additions & 17 deletions lib/benchee/utility/file_creation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ defmodule Benchee.Utility.FileCreation do
fn(file, content) -> IO.write(file, content) end)
"""
def each(names_to_content, filename, function \\ &default_each/3) do
ensure_directory_exists filename
Enum.each names_to_content, fn({input_name, content}) ->
ensure_directory_exists(filename)

Enum.each(names_to_content, fn {input_name, content} ->
input_filename = interleave(filename, input_name)
File.open! input_filename, [:write, :utf8], fn(file) ->

File.open!(input_filename, [:write, :utf8], fn file ->
function.(file, content, input_filename)
end
end
end)
end)
end

defp default_each(file, content, input_filename) do
:ok = IO.write file, content
IO.puts "Generated #{input_filename}"
:ok = IO.write(file, content)
IO.puts("Generated #{input_filename}")
end

@doc """
Make sure the directory for the given file name exists.
"""
def ensure_directory_exists(filename) do
directory = Path.dirname filename
File.mkdir_p! directory
directory = Path.dirname(filename)
File.mkdir_p!(directory)
end

@doc """
Expand Down Expand Up @@ -106,13 +108,16 @@ defmodule Benchee.Utility.FileCreation do
"abc_something_cool_comparison.csv"
"""
def interleave(filename, names) when is_list(names) do
file_names = names
|> Enum.map(&to_filename/1)
|> prepend(Path.rootname(filename))
|> Enum.reject(fn(string) -> String.length(string) < 1 end)
|> Enum.join("_")
file_names =
names
|> Enum.map(&to_filename/1)
|> prepend(Path.rootname(filename))
|> Enum.reject(fn string -> String.length(string) < 1 end)
|> Enum.join("_")

file_names <> Path.extname(filename)
end

def interleave(filename, name) do
interleave(filename, [name])
end
Expand All @@ -122,10 +127,13 @@ defmodule Benchee.Utility.FileCreation do
end

defp to_filename(name_string) do
no_input = Benchmark.no_input
no_input = Benchmark.no_input()

case name_string do
^no_input -> ""
_ ->
^no_input ->
""

_ ->
String.downcase(String.replace(name_string, ~r/[^0-9A-Z]/i, "_"))
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/benchee/utility/parallel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Benchee.Utility.Parallel do
@doc """
A utility function for mapping over an enumerable collection in parallel.
"""
@spec map(Enum.t, fun) :: list
@spec map(Enum.t(), fun) :: list
def map(collection, func) do
collection
|> Enum.map(fn(element) -> Task.async(fn() -> func.(element) end) end)
|> Enum.map(fn element -> Task.async(fn -> func.(element) end) end)
|> Enum.map(&Task.await(&1, :infinity))
end
end
2 changes: 2 additions & 0 deletions lib/benchee/utility/repeat_n.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ defmodule Benchee.Utility.RepeatN do
def repeat_n(_function, 0) do
# noop
end

def repeat_n(function, 1) do
function.()
end

def repeat_n(function, count) do
function.()
repeat_n(function, count - 1)
Expand Down

0 comments on commit b947c98

Please sign in to comment.