Skip to content

Commit dffec5c

Browse files
committed
Make system gathering more resilient
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.
1 parent 4f49671 commit dffec5c

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/benchee/system.ex

+11-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,17 @@ defmodule Benchee.System do
232232

233233
@doc false
234234
def system_cmd(cmd, args, system_func \\ &System.cmd/2) do
235-
{output, exit_code} = system_func.(cmd, args)
235+
{output, exit_code} =
236+
try do
237+
system_func.(cmd, args)
238+
rescue
239+
e ->
240+
message = Exception.format(:error, e, __STACKTRACE__)
241+
242+
# this mimics an error return code from the command,
243+
# which is the same behavior we want to have
244+
{message, 1}
245+
end
236246

237247
if exit_code > 0 do
238248
IO.puts("Something went wrong trying to get system information:")

test/benchee/system_test.exs

+24-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ defmodule Benchee.SystemTest do
55
import Benchee.System
66

77
alias Benchee.Suite
8-
alias Benchee.System
98
alias Benchee.Utility.ErlangVersion
109

1110
test ".system adds the content to a given suite" do
@@ -104,20 +103,32 @@ defmodule Benchee.SystemTest do
104103
assert rest =~ ~r/GB/
105104
end
106105

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

110-
captured_io =
111-
capture_io(fn ->
112-
system_cmd("cat", "dev/null", system_func)
113-
end)
110+
captured_io =
111+
capture_io(fn ->
112+
system_cmd("cat", "dev/null", system_func) == "N/A"
113+
end)
114+
115+
assert captured_io =~ "Something went wrong"
116+
assert captured_io =~ "ERROR"
117+
end
114118

115-
assert captured_io =~ "Something went wrong"
116-
assert captured_io =~ "ERROR"
119+
# programs might not be installed or deprecated, see https://github.com/bencheeorg/benchee/issues/442
120+
test "handles exceptions gracefully" do
121+
# this raises an Erlang error: :enoent - but in case that changes I wanted to trigger it
122+
system_func = fn _, _ -> System.cmd("definitely-not-existing-command32832", ["bogus"]) end
117123

118-
capture_io(fn ->
119-
assert system_cmd("cat", "dev/null", system_func) == "N/A"
120-
end)
124+
captured_io =
125+
capture_io(fn ->
126+
system_cmd("does", "not/matter", system_func) == "N/A"
127+
end)
128+
129+
assert captured_io =~ "Something went wrong"
130+
assert captured_io =~ "enoent"
131+
end
121132
end
122133

123134
describe "all_protocols_consolidated?/1" do
@@ -157,7 +168,7 @@ defmodule Benchee.SystemTest do
157168
end
158169
end
159170

160-
@system %System{
171+
@system %Benchee.System{
161172
elixir: "1.4.0",
162173
erlang: "19.1",
163174
jit_enabled?: false,

0 commit comments

Comments
 (0)