|
1 | 1 | defmodule Benchee.RelativeStatistcsTest do |
2 | 2 | use ExUnit.Case, async: true |
3 | 3 |
|
4 | | - alias Benchee.{CollectionData, Scenario, Statistics, Suite} |
| 4 | + alias Benchee.{CollectionData, Configuration, Scenario, Statistics, Suite} |
5 | 5 | import Benchee.RelativeStatistics |
6 | 6 |
|
7 | 7 | describe "computing relative statistics" do |
@@ -43,7 +43,7 @@ defmodule Benchee.RelativeStatistcsTest do |
43 | 43 | assert stats.relative_less == nil |
44 | 44 | end |
45 | 45 |
|
46 | | - test "handles the fastest value being zerio alright" do |
| 46 | + test "handles the fastest value being zero alright" do |
47 | 47 | suite = %Suite{ |
48 | 48 | scenarios: [ |
49 | 49 | scenario_with_average(0.0), |
@@ -98,6 +98,55 @@ defmodule Benchee.RelativeStatistcsTest do |
98 | 98 | assert stats2.relative_less == 1.0 |
99 | 99 | assert stats2.absolute_difference == 0.0 |
100 | 100 | end |
| 101 | + |
| 102 | + test "setting the reference job to be the slowest" do |
| 103 | + suite = %Suite{ |
| 104 | + scenarios: [ |
| 105 | + named_scenario_with_average("A", 394.0, nil), |
| 106 | + named_scenario_with_average("B", 14.0, nil) |
| 107 | + ], |
| 108 | + configuration: %Configuration{reference_job: "A"} |
| 109 | + } |
| 110 | + |
| 111 | + suite = relative_statistics(suite) |
| 112 | + # reference scenario always comes out on top even if slowest |
| 113 | + assert [a_stats, b_stats] = stats_from(suite) |
| 114 | + |
| 115 | + assert a_stats.relative_more == nil |
| 116 | + assert a_stats.relative_less == nil |
| 117 | + assert a_stats.absolute_difference == nil |
| 118 | + |
| 119 | + assert b_stats.absolute_difference == -380.0 |
| 120 | + assert_in_delta b_stats.relative_more, 0.0355, 0.001 |
| 121 | + assert_in_delta b_stats.relative_less, 28.14, 0.1 |
| 122 | + end |
| 123 | + |
| 124 | + test "sandwich reference job" do |
| 125 | + suite = %Suite{ |
| 126 | + scenarios: [ |
| 127 | + named_scenario_with_average("A", 1.0, nil), |
| 128 | + named_scenario_with_average("B", 2.0, nil), |
| 129 | + named_scenario_with_average("C", 3.0, nil) |
| 130 | + ], |
| 131 | + configuration: %Configuration{reference_job: "B"} |
| 132 | + } |
| 133 | + |
| 134 | + suite = relative_statistics(suite) |
| 135 | + # reference scenario always comes out on top even if slowest |
| 136 | + assert [b_stats, a_stats, c_stats] = stats_from(suite) |
| 137 | + |
| 138 | + assert b_stats.relative_more == nil |
| 139 | + assert b_stats.relative_less == nil |
| 140 | + assert b_stats.absolute_difference == nil |
| 141 | + |
| 142 | + assert a_stats.absolute_difference == -1.0 |
| 143 | + assert_in_delta a_stats.relative_more, 0.5, 0.001 |
| 144 | + assert_in_delta a_stats.relative_less, 2.0, 0.01 |
| 145 | + |
| 146 | + assert c_stats.absolute_difference == 1.0 |
| 147 | + assert_in_delta c_stats.relative_more, 1.5, 0.001 |
| 148 | + assert_in_delta c_stats.relative_less, 0.66, 0.01 |
| 149 | + end |
101 | 150 | end |
102 | 151 |
|
103 | 152 | describe "sorting behaviour" do |
@@ -138,6 +187,24 @@ defmodule Benchee.RelativeStatistcsTest do |
138 | 187 |
|
139 | 188 | assert Enum.map(sorted, fn scenario -> scenario.name end) == ["1", "2", "3"] |
140 | 189 | end |
| 190 | + |
| 191 | + test "given a configured reference_job returns that as the first regardless of performance" do |
| 192 | + fourth = named_scenario_with_average("4", 400.1, nil) |
| 193 | + second = named_scenario_with_average("2", 200.0, nil) |
| 194 | + third = named_scenario_with_average("3", 400.0, nil) |
| 195 | + first = named_scenario_with_average("1", 100.0, nil) |
| 196 | + reference = named_scenario_with_average("Ref", 500.0, nil) |
| 197 | + |
| 198 | + scenarios = [fourth, second, third, first, reference] |
| 199 | + |
| 200 | + sorted = |
| 201 | + relative_statistics(%Suite{ |
| 202 | + scenarios: scenarios, |
| 203 | + configuration: %Configuration{reference_job: "Ref"} |
| 204 | + }).scenarios |
| 205 | + |
| 206 | + assert Enum.map(sorted, fn scenario -> scenario.name end) == ["Ref", "1", "2", "3", "4"] |
| 207 | + end |
141 | 208 | end |
142 | 209 |
|
143 | 210 | describe "dealing correctly with different inputs" do |
@@ -187,6 +254,35 @@ defmodule Benchee.RelativeStatistcsTest do |
187 | 254 | ] |
188 | 255 | end |
189 | 256 |
|
| 257 | + test "calculate the correct relatives based on input names even given a reference_job" do |
| 258 | + suite = %Suite{ |
| 259 | + scenarios: [ |
| 260 | + named_input_scenario_with_average("A", "1", 100.0), |
| 261 | + named_input_scenario_with_average("B", "1", 200.0), |
| 262 | + named_input_scenario_with_average("A", "10", 1_000.0), |
| 263 | + named_input_scenario_with_average("B", "10", 10_000.0) |
| 264 | + ], |
| 265 | + configuration: %Configuration{reference_job: "B"} |
| 266 | + } |
| 267 | + |
| 268 | + suite = relative_statistics(suite) |
| 269 | + |
| 270 | + stats = |
| 271 | + suite.scenarios |
| 272 | + |> Enum.map( |
| 273 | + &{&1.name, &1.input_name, &1.run_time_data.statistics.absolute_difference, |
| 274 | + &1.run_time_data.statistics.relative_more} |
| 275 | + ) |
| 276 | + |> Enum.sort() |
| 277 | + |
| 278 | + assert stats == [ |
| 279 | + {"A", "1", -100.0, 0.5}, |
| 280 | + {"A", "10", -9_000, 0.1}, |
| 281 | + {"B", "1", nil, nil}, |
| 282 | + {"B", "10", nil, nil} |
| 283 | + ] |
| 284 | + end |
| 285 | + |
190 | 286 | test "calculate the correct relatives based on different ordering" do |
191 | 287 | suite = %Suite{ |
192 | 288 | scenarios: [ |
|
0 commit comments