Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial Solaris support to Benchee.System #422

Merged
merged 3 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/benchee/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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())
Expand All @@ -121,11 +122,16 @@ 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", ["-p", "cpu_info:0::brand"]))
end

defp cpu_speed(:Linux) do
parse_cpu_for(:Linux, system_cmd("cat", ["/proc/cpuinfo"]))
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"
Expand All @@ -139,6 +145,15 @@ defmodule Benchee.System do

def parse_cpu_for(:FreeBSD, raw_output), do: String.trim(raw_output)

def parse_cpu_for(:Solaris, raw_output) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a test may be neat, that's the reason that function is public :) It's also nice for me to have an example of the output: https://github.com/bencheeorg/benchee/blob/main/test/benchee/system_test.exs#L49

Copy link
Contributor Author

@brianewell brianewell Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Illumos kstat can be flagged to output data in json format:

$ kstat -j cpu_info:0::brand
[{
        "module": "cpu_info",
        "instance": 0,
        "name": "cpu_info0",
        "class": "misc",
        "type": 1,
        "snaptime": 3916379.363662097,
        "data": {
                "brand": "Intel(r) Xeon(r) CPU E5-2678 v3 @ 2.50GHz"
        }
}]

More universally, the -p (parsable) flag can be used to reduce the output to key value pairs:

$ kstat -p cpu_info:0:cpu_info0:brand
cpu_info:0:cpu_info0:brand      Intel(r) Xeon(r) CPU E5-2678 v3 @ 2.50GHz

This second command is effective across pretty much every variant of Solaris.

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
match_info = Regex.run(@linux_cpuinfo_regex, raw_output, capture: :all_but_first)

Expand All @@ -165,6 +180,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
Expand All @@ -187,6 +206,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)

Expand Down
19 changes: 18 additions & 1 deletion test/benchee/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ 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
Expand Down Expand Up @@ -71,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
Expand Down
Loading