From a224a33cb8e5df5cf1b3b1a200c76915744a71a6 Mon Sep 17 00:00:00 2001 From: Brian Ewell Date: Fri, 22 Dec 2023 11:35:33 +0000 Subject: [PATCH 1/3] Added initial Solaris support to Benchee.System --- lib/benchee/system.ex | 24 ++++++++++++++++++++++-- test/benchee/system_test.exs | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/benchee/system.ex b/lib/benchee/system.ex index dc2aec09..fc8ff34c 100644 --- a/lib/benchee/system.ex +++ b/lib/benchee/system.ex @@ -4,7 +4,7 @@ defmodule Benchee.System do Includes information such as elixir/erlang version, OS, CPU and memory. - So far supports/should work for Linux, MacOS, FreeBSD and Windows. + So far supports/should work for Linux, MacOS, FreeBSD, Solaris and Windows. """ alias Benchee.Conversion.Memory @@ -36,7 +36,7 @@ defmodule Benchee.System do erlang: String.t(), jit_enabled?: boolean(), num_cores: pos_integer(), - os: :macOS | :Windows | :FreeBSD | :Linux, + os: :macOS | :Windows | :FreeBSD | :Solaris | :Linux, cpu_speed: String.t(), available_memory: String.t() } @@ -105,6 +105,7 @@ defmodule Benchee.System do defp os(:darwin), do: :macOS defp os(:nt), do: :Windows defp os(:freebsd), do: :FreeBSD + defp os(:sunos), do: :Solaris defp os(_), do: :Linux defp cpu_speed, do: cpu_speed(os()) @@ -121,6 +122,10 @@ defmodule Benchee.System do parse_cpu_for(:FreeBSD, system_cmd("sysctl", ["-n", "hw.model"])) end + defp cpu_speed(:Solaris) do + parse_cpu_for(:Solaris, system_cmd("kstat", ["-j", "cpu_info:0::brand"])) + end + defp cpu_speed(:Linux) do parse_cpu_for(:Linux, system_cmd("cat", ["/proc/cpuinfo"])) end @@ -139,6 +144,11 @@ defmodule Benchee.System do def parse_cpu_for(:FreeBSD, raw_output), do: String.trim(raw_output) + def parse_cpu_for(:Solaris, raw_output) do + {:ok, [decoded]} = Jason.decode(raw_output) + decoded["data"]["brand"] + end + def parse_cpu_for(:Linux, raw_output) do match_info = Regex.run(@linux_cpuinfo_regex, raw_output, capture: :all_but_first) @@ -165,6 +175,10 @@ defmodule Benchee.System do parse_memory_for(:FreeBSD, system_cmd("sysctl", ["-n", "hw.physmem"])) end + defp available_memory(:Solaris) do + parse_memory_for(:Solaris, system_cmd("prtconf", ["-m"])) + end + defp available_memory(:Linux) do parse_memory_for(:Linux, system_cmd("cat", ["/proc/meminfo"])) end @@ -187,6 +201,12 @@ defmodule Benchee.System do Memory.format(memory) end + defp parse_memory_for(:Solaris, raw_output) do + {memory_in_megabytes, _} = Integer.parse(raw_output) + {memory_in_bytes, _} = Memory.convert({memory_in_megabytes, :megabyte}, :byte) + Memory.format(memory_in_bytes) + end + defp parse_memory_for(:Linux, raw_output) do ["MemTotal:" <> memory_info] = Regex.run(~r/MemTotal.*kB/, raw_output) diff --git a/test/benchee/system_test.exs b/test/benchee/system_test.exs index dcc47224..2a87186f 100644 --- a/test/benchee/system_test.exs +++ b/test/benchee/system_test.exs @@ -39,7 +39,7 @@ defmodule Benchee.SystemTest do end test ".os returns an atom of the current os" do - assert Enum.member?([:Linux, :FreeBSD, :macOS, :Windows], system(%Suite{}).system.os) + assert Enum.member?([:Linux, :Solaris, :FreeBSD, :macOS, :Windows], system(%Suite{}).system.os) end test ".cpu_speed returns a string (more accurate tests in .parse_cpu_for)" do From 5fb1afc11fad0c1a0261eac1697efd8ac676cb41 Mon Sep 17 00:00:00 2001 From: Brian Ewell Date: Sat, 23 Dec 2023 08:51:33 +0000 Subject: [PATCH 2/3] Removed Jason dependency. --- lib/benchee/system.ex | 11 ++++++++--- test/benchee/system_test.exs | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/benchee/system.ex b/lib/benchee/system.ex index fc8ff34c..2bd49b1d 100644 --- a/lib/benchee/system.ex +++ b/lib/benchee/system.ex @@ -123,7 +123,7 @@ defmodule Benchee.System do end defp cpu_speed(:Solaris) do - parse_cpu_for(:Solaris, system_cmd("kstat", ["-j", "cpu_info:0::brand"])) + parse_cpu_for(:Solaris, system_cmd("kstat", ["-p", "cpu_info:0::brand"])) end defp cpu_speed(:Linux) do @@ -131,6 +131,7 @@ defmodule Benchee.System do end @linux_cpuinfo_regex ~r/model name.*:([\w \(\)\-\@\.]*)/i + @solaris_cpubrand_regex ~r/^cpu_info:0:cpu_info0:brand\s+(.*)\s*$/i @doc false def parse_cpu_for(_, "N/A"), do: "N/A" @@ -145,8 +146,12 @@ defmodule Benchee.System do def parse_cpu_for(:FreeBSD, raw_output), do: String.trim(raw_output) def parse_cpu_for(:Solaris, raw_output) do - {:ok, [decoded]} = Jason.decode(raw_output) - decoded["data"]["brand"] + match_info = Regex.run(@solaris_cpubrand_regex, raw_output, capture: :all_but_first) + + case match_info do + [cpu_info] -> cpu_info + _ -> "Unrecognized processor" + end end def parse_cpu_for(:Linux, raw_output) do diff --git a/test/benchee/system_test.exs b/test/benchee/system_test.exs index 2a87186f..69cad3e6 100644 --- a/test/benchee/system_test.exs +++ b/test/benchee/system_test.exs @@ -39,7 +39,10 @@ defmodule Benchee.SystemTest do end test ".os returns an atom of the current os" do - assert Enum.member?([:Linux, :Solaris, :FreeBSD, :macOS, :Windows], system(%Suite{}).system.os) + assert Enum.member?( + [:Linux, :Solaris, :FreeBSD, :macOS, :Windows], + system(%Suite{}).system.os + ) end test ".cpu_speed returns a string (more accurate tests in .parse_cpu_for)" do From 976da22759459b63fcfa360bc6d17046d0c625be Mon Sep 17 00:00:00 2001 From: Brian Ewell Date: Sat, 23 Dec 2023 09:07:33 +0000 Subject: [PATCH 3/3] Added tests for Solaris platform --- test/benchee/system_test.exs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/benchee/system_test.exs b/test/benchee/system_test.exs index 69cad3e6..1702063c 100644 --- a/test/benchee/system_test.exs +++ b/test/benchee/system_test.exs @@ -74,6 +74,20 @@ defmodule Benchee.SystemTest do output = parse_cpu_for(:Linux, raw_output) assert output == "Unrecognized processor" end + + @solaris_kstat_brand_excerpt """ + cpu_info:0:cpu_info0:brand Intel(r) Xeon(r) CPU E5-2678 v3 @ 2.50GHz + """ + test "for :Solaris handles some normal intel output" do + output = parse_cpu_for(:Solaris, @solaris_kstat_brand_excerpt) + assert output =~ "Intel(r) Xeon(r) CPU E5-2678 v3 @ 2.50GHz" + end + + test "for :Solaris handles unknown architectures" do + raw_output = "Bender Bending Rodriguez" + output = parse_cpu_for(:Solaris, raw_output) + assert output == "Unrecognized processor" + end end test ".available_memory returns the available memory on the computer" do