Skip to content

Commit

Permalink
Make system gathering more resilient
Browse files Browse the repository at this point in the history
This is in relation to #442 - we'd previously fail if a command
wasn't available at all which definitely is wrong.

This is a quicker fix vs. getting better CPU/memory information
from elsewhere.
  • Loading branch information
PragTob committed Jan 12, 2025
1 parent 4f49671 commit decce43
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
11 changes: 10 additions & 1 deletion lib/benchee/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,16 @@ defmodule Benchee.System do

@doc false
def system_cmd(cmd, args, system_func \\ &System.cmd/2) do
{output, exit_code} = system_func.(cmd, args)
{output, exit_code} =
try do
system_func.(cmd, args)
rescue
e ->
message = Exception.format(:error, e, __STACKTRACE__)

# this mimics an error return code from the command, which is the same behavior we want to have

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

View workflow job for this annotation

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

Line is too long (max is 98, was 105).

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

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 27.2)

Line is too long (max is 98, was 105).

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

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 26.2)

Line is too long (max is 98, was 105).

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

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 27.2)

Line is too long (max is 98, was 105).

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

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 26.2)

Line is too long (max is 98, was 105).

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

View workflow job for this annotation

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

Line is too long (max is 98, was 105).
{message, 1}
end

if exit_code > 0 do
IO.puts("Something went wrong trying to get system information:")
Expand Down
37 changes: 24 additions & 13 deletions test/benchee/system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -104,20 +103,32 @@ defmodule Benchee.SystemTest do
assert rest =~ ~r/GB/
end

test ".system_cmd handles errors gracefully" do
system_func = fn _, _ -> {"ERROR", 1} end
describe ".system_cmd/3" do
test "handles error return values gracefully" do
system_func = fn _, _ -> {"ERROR", 1} end

captured_io =
capture_io(fn ->
system_cmd("cat", "dev/null", system_func)
end)
captured_io =
capture_io(fn ->
system_cmd("cat", "dev/null", system_func) == "N/A"
end)

assert captured_io =~ "Something went wrong"
assert captured_io =~ "ERROR"
end

assert captured_io =~ "Something went wrong"
assert captured_io =~ "ERROR"
# programs might not be installed or deprecated, see https://github.com/bencheeorg/benchee/issues/442
test "handles exceptions gracefully" do
# this raises an ERlang error: :enoent - but in case that changes I wanted to trigger it directly

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

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

Line is too long (max is 98, was 103).

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 27.2)

Line is too long (max is 98, was 103).

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 26.2)

Line is too long (max is 98, was 103).

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 27.2)

Line is too long (max is 98, was 103).

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

GitHub Actions / Test on Ubuntu (Elixir 1.18.1, OTP 26.2)

Line is too long (max is 98, was 103).

Check warning on line 121 in test/benchee/system_test.exs

View workflow job for this annotation

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

Line is too long (max is 98, was 103).
system_func = fn _, _ -> System.cmd("definitely-not-existing-command32832", ["bogus"]) end

capture_io(fn ->
assert system_cmd("cat", "dev/null", system_func) == "N/A"
end)
captured_io =
capture_io(fn ->
system_cmd("does", "not/matter", system_func) == "N/A"
end)

assert captured_io =~ "Something went wrong"
assert captured_io =~ "enoent"
end
end

describe "all_protocols_consolidated?/1" do
Expand Down Expand Up @@ -157,7 +168,7 @@ defmodule Benchee.SystemTest do
end
end

@system %System{
@system %Benchee.System{
elixir: "1.4.0",
erlang: "19.1",
jit_enabled?: false,
Expand Down

0 comments on commit decce43

Please sign in to comment.