Skip to content

Commit fcf93e6

Browse files
committed
Benchee integration test for outlier exclusion
1 parent 79b1e6b commit fcf93e6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/benchee_test.exs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,70 @@ defmodule BencheeTest do
11061106
end
11071107
end
11081108

1109+
describe "exclude_outliers" do
1110+
test "even with it the high level README example still passes its asserts" do
1111+
output =
1112+
capture_io(fn ->
1113+
list = Enum.to_list(1..10_000)
1114+
map_fun = fn i -> [i, i * i] end
1115+
1116+
Benchee.run(
1117+
%{
1118+
"flat_map" => fn -> Enum.flat_map(list, map_fun) end,
1119+
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end
1120+
},
1121+
Keyword.merge(@test_configuration, exclude_outliers: true)
1122+
)
1123+
end)
1124+
1125+
readme_sample_asserts(output)
1126+
end
1127+
1128+
# The easiest way to create an outlier is to just run something once
1129+
# and then take a "stable" measurement like reductions or memory
1130+
test "removes outliers" do
1131+
{:ok, agent} = Agent.start(fn -> 0 end)
1132+
1133+
output =
1134+
capture_io(fn ->
1135+
suite =
1136+
Benchee.run(
1137+
%{
1138+
"flawed" => fn ->
1139+
# Produce some garbage but only once
1140+
# can't use process dictionary as it's a different process every time
1141+
if Agent.get(agent, & &1) < 1 do
1142+
Enum.map(1..100, fn i -> "garbage #{i}" end)
1143+
Agent.update(agent, &(&1 + 1))
1144+
end
1145+
end
1146+
},
1147+
time: 0,
1148+
warmup: 0,
1149+
reduction_time: 0.005,
1150+
exclude_outliers: true
1151+
)
1152+
1153+
%{scenarios: [%{reductions_data: %{samples: samples, statistics: stats}}]} = suite
1154+
1155+
assert [outlier] = stats.outliers
1156+
assert outlier >= stats.upper_outlier_bound
1157+
# since the outlier is removed, all values are the same
1158+
assert stats.std_dev == 0
1159+
assert stats.minimum == stats.maximum
1160+
1161+
# It's a big outlier!
1162+
assert 10 * stats.average < outlier
1163+
1164+
# The outlier is only removed from the stats, but not from the samples
1165+
assert outlier in samples
1166+
end)
1167+
1168+
# As the outlier is removed, all measurements are the same
1169+
assert output =~ ~r/all.*reduction.*same/i
1170+
end
1171+
end
1172+
11091173
describe "warn when functions are evaluated" do
11101174
test "warns when run in iex" do
11111175
# test env to avoid repeated compilation on CI

0 commit comments

Comments
 (0)