Skip to content

Commit

Permalink
Log when we're calculating statistics or formatting
Browse files Browse the repository at this point in the history
Helps see which step went wrong and at what point we are right
now.

So, if say memory blows up or something takes super long it's
easier to discern whether that is a benchmark currently running,
the statistics calculation or the formatter. #389
  • Loading branch information
PragTob committed Nov 16, 2023
1 parent 17f7370 commit 8c90be6
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 52 deletions.
2 changes: 2 additions & 0 deletions lib/benchee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ for {module, moduledoc} <- [{Benchee, elixir_doc}, {:benchee, erlang_doc}] do
@doc false
defdelegate collect(suite, printer), to: Benchee.Benchmark
defdelegate statistics(suite), to: Benchee.Statistics
@doc false
defdelegate statistics(suite, printer), to: Benchee.Statistics
defdelegate relative_statistics(suite), to: Benchee.RelativeStatistics
defdelegate load(suite), to: Benchee.ScenarioLoader
defdelegate profile(suite), to: Benchee.Profile
Expand Down
12 changes: 12 additions & 0 deletions lib/benchee/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Benchee.Formatter do
through `output/3`.
"""

alias Benchee.Output.ProgressPrinter
alias Benchee.{Suite, Utility.Parallel}
alias Benchee.Utility.DeepConvert

Expand Down Expand Up @@ -50,6 +51,8 @@ defmodule Benchee.Formatter do
"""
@spec output(Suite.t()) :: Suite.t()
def output(suite = %{configuration: %{formatters: formatters}}) do
print_formatting(suite.configuration)

{parallelizable, serial} =
formatters
|> Enum.map(&normalize_module_configuration/1)
Expand All @@ -64,6 +67,15 @@ defmodule Benchee.Formatter do
suite
end

# This is a bit of a hack, but we can't DI the printer like we usually do as we made the _great_
# decisions to use both `output/1` and `output/2` as part of the public interface. Adding the
# printer to `output/1` would clash with `output/2` and its second parameter is _also_ a module.
# So, we're abusing the `assigns` in the config a bit.
defp print_formatting(config) do
printer = config.assigns[:test][:progress_printer] || ProgressPrinter
printer.formatting(config)
end

defp normalize_module_configuration(formatter) when is_function(formatter, 1), do: formatter

defp normalize_module_configuration({module, opts}) do
Expand Down
15 changes: 15 additions & 0 deletions lib/benchee/output/progress_printer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Benchee.Output.ProgressPrinter do
@moduledoc false

def calculating_statistics(%{print: %{benchmarking: false}}), do: nil

def calculating_statistics(_config) do
IO.puts("Calculating statistics...")
end

def formatting(%{print: %{benchmarking: false}}), do: nil

def formatting(_config) do
IO.puts("Formatting results..")
end
end
7 changes: 5 additions & 2 deletions lib/benchee/statistics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Benchee.Statistics do
"""

alias Benchee.{CollectionData, Conversion.Duration, Scenario, Suite, Utility.Parallel}
alias Benchee.Output.ProgressPrinter

require Integer

Expand Down Expand Up @@ -115,7 +116,7 @@ defmodule Benchee.Statistics do
...> }
...> ]
iex> suite = %Benchee.Suite{scenarios: scenarios}
iex> statistics(suite)
iex> statistics(suite, Benchee.Test.FakeProgressPrinter)
%Benchee.Suite{
scenarios: [
%Benchee.Scenario{
Expand Down Expand Up @@ -161,7 +162,9 @@ defmodule Benchee.Statistics do
"""
@spec statistics(Suite.t()) :: Suite.t()
def statistics(suite) do
def statistics(suite, printer \\ ProgressPrinter) do
printer.calculating_statistics(suite.configuration)

percentiles = suite.configuration.percentiles

update_in(suite.scenarios, fn scenarios ->
Expand Down

0 comments on commit 8c90be6

Please sign in to comment.