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 1 commit
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
24 changes: 22 additions & 2 deletions lib/benchee/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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 @@
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 @@
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,6 +122,10 @@
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
Expand All @@ -139,6 +144,11 @@

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.

{:ok, [decoded]} = Jason.decode(raw_output)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.13.4, OTP 23.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.13.4, OTP 24.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.13.4, OTP 25.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.14.5, OTP 23.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.14.5, OTP 24.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.14.5, OTP 25.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.8.2, OTP 20.3)

function Jason.decode/1 is undefined (module Jason is not available)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.10.4, OTP 21.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.11.4, OTP 22.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.12.3, OTP 23.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)

Check warning on line 148 in lib/benchee/system.ex

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.13.4, OTP 22.3)

Jason.decode/1 is undefined (module Jason is not available or is yet to be defined)
Copy link
Member

Choose a reason for hiding this comment

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

This works by accident. Benchee does not depend on Jason and should not depend on it. It's available in the dev and test environments due to dev/test dependencies:

tobi@qiqi:~/github/benchee(main)$ mix deps.tree
benchee
├── credo ~> 1.7.2-rc.0 (Hex package)
│   ├── bunt ~> 0.2.1 or ~> 1.0 (Hex package)
│   ├── file_system ~> 0.2 or ~> 1.0 (Hex package)
│   └── jason ~> 1.0 (Hex package)
├── deep_merge ~> 1.0 (Hex package)
├── dialyxir ~> 1.0 (Hex package)
│   └── erlex >= 0.2.6 (Hex package)
├── ex_doc >= 0.0.0 (Hex package)
│   ├── earmark_parser ~> 1.4.39 (Hex package)
│   ├── makeup_elixir ~> 0.14 (Hex package)
│   │   ├── makeup ~> 1.0 (Hex package)
│   │   │   └── nimble_parsec ~> 1.2.2 or ~> 1.3 (Hex package)
│   │   └── nimble_parsec ~> 1.2.3 or ~> 1.3 (Hex package)
│   └── makeup_erlang ~> 0.1 (Hex package)
│       └── makeup ~> 1.0 (Hex package)
├── ex_guard ~> 1.3 (Hex package)
│   └── fs ~> 8.6.1 (Hex package)
├── excoveralls ~> 0.13 (Hex package)
│   └── jason ~> 1.0 (Hex package)
├── statistex ~> 1.0 (Hex package)
└── table ~> 0.1.0 (Hex package)

so, we need make do here without Jason :)

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)

Expand All @@ -165,6 +175,10 @@
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 +201,12 @@
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
2 changes: 1 addition & 1 deletion test/benchee/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading