Skip to content

Commit

Permalink
Make System a struct
Browse files Browse the repository at this point in the history
Adding fields was painful before and I missed where I should add
them, this should make it easier to have the compiler check it
for you.
  • Loading branch information
PragTob committed Nov 16, 2023
1 parent fd4efbc commit 17f7370
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* System information now includes whether or not the JIT is enabled ([erlang docs](https://www.erlang.org/doc/apps/erts/beamasm)).

### Features (Plugins)
* `jit_enabled?` is exposed as part of the `suite.system` map
* `jit_enabled?` is exposed as part of the `suite.system` struct
* Yes, `Benchee.System` is now a struct so feel easier about relying on the fields

## 1.2.0 (2023-11-09)

Expand Down
4 changes: 2 additions & 2 deletions lib/benchee/output/benchmark_printer.ex
@@ -1,7 +1,7 @@
defmodule Benchee.Output.BenchmarkPrinter do
@moduledoc false

alias Benchee.{Benchmark, Conversion.Duration}
alias Benchee.{Benchmark, Conversion.Duration, System}

@doc """
Shown when you try benchmark an evaluated function.
Expand Down Expand Up @@ -41,7 +41,7 @@ defmodule Benchee.Output.BenchmarkPrinter do
suite_information(scenarios, config)
end

defp system_information(%{
defp system_information(%System{
erlang: erlang_version,
elixir: elixir_version,
jit_enabled?: jit_enabled?,
Expand Down
2 changes: 1 addition & 1 deletion lib/benchee/suite.ex
Expand Up @@ -27,7 +27,7 @@ defmodule Benchee.Suite do
"""
@type t :: %__MODULE__{
configuration: Benchee.Configuration.t() | nil,
system: map | nil,
system: Benchee.System.t() | nil,
scenarios: [] | [Benchee.Scenario.t()]
}
end
Expand Down
42 changes: 41 additions & 1 deletion lib/benchee/system.ex
Expand Up @@ -11,14 +11,44 @@ defmodule Benchee.System do
alias Benchee.Suite
alias Benchee.Utility.ErlangVersion

@enforce_keys [
:elixir,
:erlang,
:jit_enabled?,
:num_cores,
:os,
:available_memory,
:cpu_speed
]

defstruct [
:elixir,
:erlang,
:jit_enabled?,
:num_cores,
:os,
:available_memory,
:cpu_speed
]

@type t :: %__MODULE__{
elixir: String.t(),
erlang: String.t(),
jit_enabled?: boolean(),
num_cores: pos_integer(),
os: :macOS | :Windows | :FreeBSD | :Linux,
cpu_speed: String.t(),
available_memory: String.t()
}

@doc """
Adds system information to the suite (currently elixir and erlang versions).
"""
@spec system(Suite.t()) :: Suite.t()
def system(suite = %Suite{}) do
erlang_version = erlang()

system_info = %{
system_info = %__MODULE__{
elixir: elixir(),
erlang: erlang_version,
jit_enabled?: jit_enabled?(erlang_version),
Expand Down Expand Up @@ -217,3 +247,13 @@ defmodule Benchee.System do
end
end
end

defimpl DeepMerge.Resolver, for: Benchee.System do
def resolve(original, override = %Benchee.System{}, resolver) do
Map.merge(original, override, resolver)
end

def resolve(original, override, resolver) when is_map(override) do
Map.merge(original, override, resolver)
end
end
4 changes: 2 additions & 2 deletions test/benchee/benchmark/runner_test.exs
Expand Up @@ -4,7 +4,7 @@ defmodule Benchee.Benchmark.RunnerTest do
import Benchee.TestHelpers
import ExUnit.CaptureIO

alias Benchee.{Benchmark, Configuration, Scenario, Suite}
alias Benchee.{Benchmark, Configuration, Scenario, Suite, System}
alias Benchee.Test.FakeBenchmarkPrinter, as: TestPrinter

@config %Configuration{
Expand All @@ -16,7 +16,7 @@ defmodule Benchee.Benchmark.RunnerTest do
print: %{fast_warning: false, configuration: true},
measure_function_call_overhead: false
}
@system %{
@system %System{
elixir: "1.4.0",
erlang: "19.1",
jit_enabled?: false,
Expand Down
4 changes: 2 additions & 2 deletions test/benchee/output/benchmark_printer_test.exs
@@ -1,12 +1,12 @@
defmodule Benchee.Output.BenchmarkPrintertest do
use ExUnit.Case, async: true

alias Benchee.{Benchmark, Configuration, Scenario}
alias Benchee.{Benchmark, Configuration, Scenario, System}

import ExUnit.CaptureIO
import Benchee.Output.BenchmarkPrinter

@system_info %{
@system_info %System{
elixir: "1.15.7",
erlang: "26.1",
jit_enabled?: true,
Expand Down
19 changes: 15 additions & 4 deletions test/benchee/suite_test.exs
@@ -1,11 +1,22 @@
defmodule Benchee.SuiteTest do
use ExUnit.Case, async: true

alias Benchee.Suite
import DeepMerge

alias Benchee.Suite
alias Benchee.System

@system %System{
elixir: "1.4.0",
erlang: "19.2",
jit_enabled?: false,
num_cores: "4",
os: "Super Duper",
available_memory: "8 Trillion",
cpu_speed: "light speed"
}
@empty_suite %Suite{
system: %{elixir: "1.4.2", erlang: "19.2"},
system: @system,
scenarios: []
}

Expand Down Expand Up @@ -46,7 +57,7 @@ defmodule Benchee.SuiteTest do
if Code.ensure_loaded?(Table.Reader) do
describe "Table.Reader protocol" do
@suite_with_data %Suite{
system: %{elixir: "1.4.2", erlang: "19.2"},
system: @system,
configuration: %Benchee.Configuration{
percentiles: [50, 99]
},
Expand Down Expand Up @@ -147,7 +158,7 @@ defmodule Benchee.SuiteTest do
}

@suite_with_reductions %Suite{
system: %{elixir: "1.14.2", erlang: "25.2"},
system: @system,
configuration: %Benchee.Configuration{
percentiles: [50, 99]
},
Expand Down
17 changes: 17 additions & 0 deletions test/benchee/system_test.exs
Expand Up @@ -5,6 +5,7 @@ defmodule Benchee.SystemTest do
import Benchee.System

alias Benchee.Suite
alias Benchee.System
alias Benchee.Utility.ErlangVersion

test ".system adds the content to a given suite" do
Expand Down Expand Up @@ -136,4 +137,20 @@ defmodule Benchee.SystemTest do
refute jit_enabled?
end
end

@system %System{
elixir: "1.4.0",
erlang: "19.1",
jit_enabled?: false,
num_cores: "4",
os: "Super Duper",
available_memory: "8 Trillion",
cpu_speed: "light speed"
}
describe "deep_merge behaviour" do
test "it merges with a map preserving other keys" do
assert %{elixir: "1.15.7", erlang: "19.1"} =
DeepMerge.deep_merge(@system, %{elixir: "1.15.7"})
end
end
end

0 comments on commit 17f7370

Please sign in to comment.