Skip to content

Commit

Permalink
Format formatters and conversion folders
Browse files Browse the repository at this point in the history
  • Loading branch information
devonestes committed May 30, 2018
1 parent b947c98 commit fea476c
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 136 deletions.
56 changes: 30 additions & 26 deletions lib/benchee/conversion/count.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ defmodule Benchee.Conversion.Count do
@one_thousand 1_000

@units %{
billion: %Unit{
name: :billion,
magnitude: @one_billion,
label: "B",
long: "Billion"
},
million: %Unit{
name: :million,
magnitude: @one_million,
label: "M",
long: "Million"
},
billion: %Unit{
name: :billion,
magnitude: @one_billion,
label: "B",
long: "Billion"
},
million: %Unit{
name: :million,
magnitude: @one_million,
label: "M",
long: "Million"
},
thousand: %Unit{
name: :thousand,
magnitude: @one_thousand,
label: "K",
long: "Thousand"
},
one: %Unit{
name: :one,
magnitude: 1,
label: "",
long: ""
},
name: :thousand,
magnitude: @one_thousand,
label: "K",
long: "Thousand"
},
one: %Unit{
name: :one,
magnitude: 1,
label: "",
long: ""
}
}

@doc """
Expand All @@ -60,12 +60,15 @@ defmodule Benchee.Conversion.Count do
def scale(count) when count >= @one_billion do
scale_with_unit(count, :billion)
end

def scale(count) when count >= @one_million do
scale_with_unit(count, :million)
end

def scale(count) when count >= @one_thousand do
scale_with_unit(count, :thousand)
end

def scale(count) do
scale_with_unit(count, :one)
end
Expand Down Expand Up @@ -103,7 +106,7 @@ defmodule Benchee.Conversion.Count do
}
"""
def unit_for(unit) do
Scale.unit_for @units, unit
Scale.unit_for(@units, unit)
end

@doc """
Expand All @@ -125,7 +128,7 @@ defmodule Benchee.Conversion.Count do
"""
def scale(count, unit) do
Scale.scale count, unit, __MODULE__
Scale.scale(count, unit, __MODULE__)
end

@doc """
Expand All @@ -140,7 +143,7 @@ defmodule Benchee.Conversion.Count do
:million
"""
def convert(number_and_unit, desired_unit) do
Scale.convert number_and_unit, desired_unit, __MODULE__
Scale.convert(number_and_unit, desired_unit, __MODULE__)
end

@doc """
Expand All @@ -166,6 +169,7 @@ defmodule Benchee.Conversion.Count do
"""
def best(list, opts \\ [strategy: :best])

def best(list, opts) do
Scale.best_unit(list, __MODULE__, opts)
end
Expand Down
1 change: 0 additions & 1 deletion lib/benchee/conversion/deviation_percent.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Benchee.Conversion.DeviationPercent do

@moduledoc """
Helps with formattiong for the standard deviation ratio converting it into the
more common percent form.
Expand Down
95 changes: 50 additions & 45 deletions lib/benchee/conversion/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,47 @@ defmodule Benchee.Conversion.Duration do
@minutes_per_hour 60

@nanoseconds_per_millisecond @nanoseconds_per_microsecond * @microseconds_per_millisecond
@nanoseconds_per_second @nanoseconds_per_millisecond *
@milliseconds_per_second
@nanoseconds_per_second @nanoseconds_per_millisecond * @milliseconds_per_second
@nanoseconds_per_minute @nanoseconds_per_second * @seconds_per_minute
@nanoseconds_per_hour @nanoseconds_per_minute * @minutes_per_hour

@units %{
hour: %Unit{
name: :hour,
magnitude: @nanoseconds_per_hour,
label: "h",
long: "Hours"
},
minute: %Unit{
name: :minute,
magnitude: @nanoseconds_per_minute,
label: "min",
long: "Minutes"
},
second: %Unit{
name: :second,
magnitude: @nanoseconds_per_second,
label: "s",
long: "Seconds"
},
hour: %Unit{
name: :hour,
magnitude: @nanoseconds_per_hour,
label: "h",
long: "Hours"
},
minute: %Unit{
name: :minute,
magnitude: @nanoseconds_per_minute,
label: "min",
long: "Minutes"
},
second: %Unit{
name: :second,
magnitude: @nanoseconds_per_second,
label: "s",
long: "Seconds"
},
millisecond: %Unit{
name: :millisecond,
magnitude: @nanoseconds_per_millisecond,
label: "ms",
long: "Milliseconds"
},
name: :millisecond,
magnitude: @nanoseconds_per_millisecond,
label: "ms",
long: "Milliseconds"
},
microsecond: %Unit{
name: :microsecond,
magnitude: @nanoseconds_per_microsecond,
label: "μs",
long: "Microseconds"
},
nanosecond: %Unit{
name: :nanosecond,
magnitude: 1,
label: "ns",
long: "Nanoseconds"
}
name: :microsecond,
magnitude: @nanoseconds_per_microsecond,
label: "μs",
long: "Microseconds"
},
nanosecond: %Unit{
name: :nanosecond,
magnitude: 1,
label: "ns",
long: "Nanoseconds"
}
}

@doc """
Expand All @@ -83,22 +82,27 @@ defmodule Benchee.Conversion.Duration do
:hour
"""
def scale(duration) when duration >= @nanoseconds_per_hour do
scale_with_unit duration, :hour
scale_with_unit(duration, :hour)
end

def scale(duration) when duration >= @nanoseconds_per_minute do
scale_with_unit duration, :minute
scale_with_unit(duration, :minute)
end

def scale(duration) when duration >= @nanoseconds_per_second do
scale_with_unit duration, :second
scale_with_unit(duration, :second)
end

def scale(duration) when duration >= @nanoseconds_per_millisecond do
scale_with_unit duration, :millisecond
scale_with_unit(duration, :millisecond)
end

def scale(duration) when duration >= @nanoseconds_per_microsecond do
scale_with_unit duration, :microsecond
scale_with_unit(duration, :microsecond)
end

def scale(duration) do
scale_with_unit duration, :nanosecond
scale_with_unit(duration, :nanosecond)
end

# Helper function for returning a tuple of {value, unit}
Expand Down Expand Up @@ -134,7 +138,7 @@ defmodule Benchee.Conversion.Duration do
}
"""
def unit_for(unit) do
Scale.unit_for @units, unit
Scale.unit_for(@units, unit)
end

@doc """
Expand All @@ -153,7 +157,7 @@ defmodule Benchee.Conversion.Duration do
"""
def scale(count, unit) do
Scale.scale count, unit, __MODULE__
Scale.scale(count, unit, __MODULE__)
end

@doc """
Expand All @@ -168,7 +172,7 @@ defmodule Benchee.Conversion.Duration do
:hour
"""
def convert(number_and_unit, desired_unit) do
Scale.convert number_and_unit, desired_unit, __MODULE__
Scale.convert(number_and_unit, desired_unit, __MODULE__)
end

@doc """
Expand Down Expand Up @@ -215,6 +219,7 @@ defmodule Benchee.Conversion.Duration do
:second
"""
def best(list, opts \\ [strategy: :best])

def best(list, opts) do
Scale.best_unit(list, __MODULE__, opts)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/benchee/conversion/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Benchee.Conversion.Format do
Formats a number as a string, with a unit label. See `Benchee.Conversion.Count`
and `Benchee.Conversion.Duration` for examples
"""
@callback format(number) :: String.t
@callback format(number) :: String.t()

# Generic formatting functions

Expand Down Expand Up @@ -44,6 +44,7 @@ defmodule Benchee.Conversion.Format do
def format({count, unit = %Unit{}}, module) do
format(count, label(unit), separator(module))
end

def format({count, unit_atom}, module) do
format({count, module.unit_for(unit_atom)}, module)
end
Expand Down
Loading

0 comments on commit fea476c

Please sign in to comment.