Skip to content

Commit

Permalink
Merge pull request #18 from PragTob/benchee-update
Browse files Browse the repository at this point in the history
Benchee update
  • Loading branch information
devonestes committed Mar 18, 2019
2 parents 5fa04c1 + 61d035f commit 552cd5b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 65 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.99.0 - unreleases
* Benchee 0.99 support
* BREAKING: data structures changed, no more `run_times`/`memory_usages`/`run_time_statitics`/`memory_usage_statistics` - it's all in `run_time_data`/`memory_usage_data` now under the keys `samples`/`statistics` to mirror the original benchee data structure.

## 0.6.0 - 2019-02-10

### Features
Expand Down
114 changes: 57 additions & 57 deletions lib/benchee/formatters/json.ex
Expand Up @@ -5,19 +5,26 @@ Protocol.derive(Jason.Encoder, Benchee.Statistics)

Protocol.derive(
Jason.Encoder,
Benchee.Benchmark.Scenario,
Benchee.Scenario,
only: [
:name,
:job_name,
:input_name,
:run_time_statistics,
:memory_usage_statistics,
:run_times,
:memory_usages,
:run_time_data,
:memory_usage_data,
:tag
]
)

Protocol.derive(
Jason.Encoder,
Benchee.CollectionData,
only: [
:statistics,
:samples
]
)

defmodule Benchee.Formatters.JSON do
@moduledoc """
Functionality for converting Benchee benchmarking results to JSON so that
Expand Down Expand Up @@ -55,80 +62,73 @@ defmodule Benchee.Formatters.JSON do
## Examples
iex> suite = %Benchee.Suite{
...> scenarios: [
...> %Benchee.Benchmark.Scenario{
...> %Benchee.Scenario{
...> job_name: "My Job",
...> name: "My Job",
...> run_times: [200, 400, 400, 400, 500, 500, 700, 900],
...> memory_usages: [200, 400, 400, 400, 500, 500, 700, 900],
...> input_name: "Some Input",
...> input: "Some Input",
...> run_time_statistics: %Benchee.Statistics{
...> average: 500.0,
...> ips: 2000.0,
...> std_dev: 200.0,
...> std_dev_ratio: 0.4,
...> std_dev_ips: 800.0,
...> median: 450.0,
...> minimum: 200,
...> maximum: 900,
...> mode: 400,
...> sample_size: 8,
...> percentiles: %{99 => 900}
...> run_time_data: %Benchee.CollectionData{
...> samples: [200, 400, 400, 400, 500, 500, 700, 900],
...> statistics: %Benchee.Statistics{
...> average: 500.0,
...> ips: 2000.0,
...> std_dev: 200.0,
...> std_dev_ratio: 0.4,
...> std_dev_ips: 800.0,
...> median: 450.0,
...> minimum: 200,
...> maximum: 900,
...> mode: 400,
...> sample_size: 8,
...> percentiles: %{99 => 900}
...> }
...> },
...> memory_usage_statistics: %Benchee.Statistics{
...> average: 500.0,
...> ips: nil,
...> std_dev: 200.0,
...> std_dev_ratio: 0.4,
...> std_dev_ips: nil,
...> median: 450.0,
...> minimum: 200,
...> maximum: 900,
...> mode: nil,
...> sample_size: 8,
...> percentiles: %{99 => 900}
...> memory_usage_data: %Benchee.CollectionData{
...> samples: [200, 400, 400, 400, 500, 500, 700, 900],
...> statistics: %Benchee.Statistics{
...> average: 500.0,
...> ips: nil,
...> std_dev: 200.0,
...> std_dev_ratio: 0.4,
...> std_dev_ips: nil,
...> median: 450.0,
...> minimum: 200,
...> maximum: 900,
...> mode: nil,
...> sample_size: 8,
...> percentiles: %{99 => 900}
...> }
...> }
...> },
...> ]
...> }
iex> Benchee.Formatters.JSON.format(suite, %{file: "my_file.json"})
"[{\\"input_name\\":\\"Some Input\\",\\"memory_usage_statistics\\":{\\"average\\":500.0,\\"ips\\":null,\\"maximum\\":900,\\"median\\":450.0,\\"minimum\\":200,\\"mode\\":null,\\"percentiles\\":{\\"99\\":900},\\"sample_size\\":8,\\"std_dev\\":200.0,\\"std_dev_ips\\":null,\\"std_dev_ratio\\":0.4},\\"memory_usages\\":[200,400,400,400,500,500,700,900],\\"name\\":\\"My Job\\",\\"run_time_statistics\\":{\\"average\\":500.0,\\"ips\\":2.0e3,\\"maximum\\":900,\\"median\\":450.0,\\"minimum\\":200,\\"mode\\":400,\\"percentiles\\":{\\"99\\":900},\\"sample_size\\":8,\\"std_dev\\":200.0,\\"std_dev_ips\\":800.0,\\"std_dev_ratio\\":0.4},\\"run_times\\":[200,400,400,400,500,500,700,900]}]"
"[{\\"name\\":\\"My Job\\",\\"job_name\\":\\"My Job\\",\\"input_name\\":\\"Some Input\\",\\"run_time_data\\":{\\"statistics\\":{\\"average\\":500.0,\\"ips\\":2.0e3,\\"maximum\\":900,\\"median\\":450.0,\\"minimum\\":200,\\"mode\\":400,\\"percentiles\\":{\\"99\\":900},\\"sample_size\\":8,\\"std_dev\\":200.0,\\"std_dev_ips\\":800.0,\\"std_dev_ratio\\":0.4},\\"samples\\":[200,400,400,400,500,500,700,900]},\\"memory_usage_data\\":{\\"statistics\\":{\\"average\\":500.0,\\"ips\\":null,\\"maximum\\":900,\\"median\\":450.0,\\"minimum\\":200,\\"mode\\":null,\\"percentiles\\":{\\"99\\":900},\\"sample_size\\":8,\\"std_dev\\":200.0,\\"std_dev_ips\\":null,\\"std_dev_ratio\\":0.4},\\"samples\\":[200,400,400,400,500,500,700,900]},\\"tag\\":null}]"
"""
@spec format(Suite.t(), %{file: String.t()}) :: String.t()
def format(%Suite{scenarios: scenarios}, %{file: _}) do
scenarios
|> Enum.map(fn scenario ->
Map.take(scenario, [
:name,
:input_name,
:run_time_statistics,
:memory_usage_statistics,
:run_times,
:memory_usages
])
end)
|> encode!()
end

def format(_, _) do
raise """
You need to specify a file to write the JSON to in the configuration a
formatter option:
formatters: [{Benchee.Formatters.JSON, file: \"my.json\"}]
"""
@spec format(Suite.t(), map) :: String.t()
def format(%Suite{scenarios: scenarios}, _) do
encode!(scenarios)
end

@doc """
Uses the return value of `Benchee.Formatters.JSON.format/1` to write it to the
JSON file defined in the initial configuration.
"""
@spec write(String.t(), map) :: :ok
@spec write(String.t(), %{file: String.t()}) :: :ok
def write(data, %{file: file}) do
File.write!(file, data)
end

def write(_, _) do
raise """
You need to specify a file to write the JSON to in the configuration a
formatter option:
formatters: [{Benchee.Formatters.JSON, file: \\"my.json\\"}]
"""
end

@doc """
Simple wrapper for encoding a map/benchee struct to JSON.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -34,7 +34,7 @@ defmodule BencheeJSON.Mixfile do

defp deps do
[
{:benchee, "~> 0.14"},
{:benchee, "~> 0.14", github: "PragTob/benchee"},
{:jason, "~> 1.0"},
{:excoveralls, "~> 0.8", only: :test},
{:credo, "~> 1.0", only: :dev},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
@@ -1,5 +1,5 @@
%{
"benchee": {:hex, :benchee, "0.14.0", "f771f587c48b4824b497e2a3e374f75e93ef01fc329873b089a3f5dd961b80b8", [:mix], [{:deep_merge, "~> 0.1", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm"},
"benchee": {:git, "https://github.com/PragTob/benchee.git", "de9e99462f6e5ad03001460391a8bd82dbbba773", []},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "1.0.2", "88bc918f215168bf6ce7070610a6173c45c82f32baa08bdfc80bf58df2d103b6", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
12 changes: 8 additions & 4 deletions test/benchee/formatters/json_integration_test.exs
Expand Up @@ -44,16 +44,20 @@ defmodule Benchee.Formatters.JSONIntegrationTest do

assert %{
"name" => "Sleep",
"run_time_statistics" => %{"average" => _, "ips" => _},
"run_times" => [run_time | _]
"run_time_data" => %{
"statistics" => %{"average" => _, "ips" => _},
"samples" => [run_time | _]
}
} = sleep

assert run_time > 10_000_000

assert %{
"name" => "List",
"memory_usage_statistics" => %{"average" => _, "ips" => _},
"memory_usages" => [376 | _]
"memory_usage_data" => %{
"statistics" => %{"average" => _, "ips" => _},
"samples" => [376 | _]
}
} = list
end)
after
Expand Down
4 changes: 2 additions & 2 deletions test/benchee/formatters/json_test.exs
Expand Up @@ -4,10 +4,10 @@ defmodule Benchee.Formatters.JSONTest do

alias Benchee.{Formatters.JSON, Suite}

describe "format/1" do
describe "write/1" do
test "raises an exception if there is no file configured" do
assert_raise RuntimeError, fn ->
JSON.format(%Suite{}, %{})
JSON.write(%Suite{}, %{})
end
end
end
Expand Down

0 comments on commit 552cd5b

Please sign in to comment.