Skip to content

Commit

Permalink
More statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob committed May 22, 2016
1 parent 3914204 commit 60fba66
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/benchee.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Benchee do
@default_config %{time: 5}

alias Benchee.{Statistics, Time}

@doc """
Returns the initial benchmark suite data structure for Benshee.
Given an optional map of configuration options, converts seconds in there
Expand All @@ -20,7 +22,7 @@ defmodule Benchee do
@seconds_to_microseconds 1_000_000
defp convert_time_to_micro_s(config) do
{_, config} = Map.get_and_update! config, :time, fn(seconds) ->
{seconds, seconds * @seconds_to_microseconds}
{seconds, Time.seconds_to_microseconds(seconds)}
end
config
end
Expand Down Expand Up @@ -58,14 +60,12 @@ defmodule Benchee do
Creates a report of the benchmark suite run.
"""
def report(%{jobs: jobs}) do

Enum.map jobs, fn(%{name: name, run_times: times}) ->
"#{name} #{Enum.count(times)} #{average(times)}"
%{average: average, ips: ips, std_dev: std_dev} = Statistics.statistics(times)
"#{name} #{ips} #{average}μs #{std_dev}"
end
end

defp average(series) do
total = Enum.sum(series)
iterations = Enum.count(series)
total / iterations
end

end
35 changes: 35 additions & 0 deletions lib/benchee/statistics.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Benchee.Statistics do
alias Benchee.Time

@doc """
Calculates statistical data based on a series of run times in microseconds.
iex> Benchee.Statistics.statistics([200, 400, 400, 400, 500, 500, 700, 900])
%{average: 500.0, std_dev: 200.0, ips: 2000.0}
"""
def statistics(run_times) do
total_time = Enum.sum(run_times)
iterations = Enum.count(run_times)
average_time = total_time / iterations
iterations_per_second = iterations_per_second(iterations, total_time)
standard_deviation = standard_deviation(run_times, average_time, iterations)

%{
average: average_time,
ips: iterations_per_second,
std_dev: standard_deviation
}
end

defp iterations_per_second(iterations, time_microseconds) do
iterations / (Time.microseconds_to_seconds(time_microseconds))
end

defp standard_deviation(samples, average, iterations) do
total_variance = Enum.reduce samples, 0, fn(sample, total) ->
total + :math.pow((sample - average), 2)
end
variance = total_variance / iterations
:math.sqrt variance
end
end
11 changes: 11 additions & 0 deletions lib/benchee/time.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Benchee.Time do
@seconds_to_microseconds 1_000_000

def microseconds_to_seconds(microseconds) do
microseconds / @seconds_to_microseconds
end

def seconds_to_microseconds(seconds) do
seconds * @seconds_to_microseconds
end
end
5 changes: 5 additions & 0 deletions test/benchee/statistics_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Benchee.StatistcsTest do
use ExUnit.Case
doctest Benchee.Statistics

end

0 comments on commit 60fba66

Please sign in to comment.