Debian Codegraph @

website/content/2017-09-28-threshold-problem.md

raw
1 ---
2 tags: technical, details, python
3 date: 2017-09-28 11:00
4 title: The Threshold Problem
5 author: drmaciver
6 ---
7
8 In [my last post](../multi-bug-discovery/) I mentioned
9 the problem of bug slippage: When you start with one bug, reduce the test case, and end
10 up with another bug.
11
12 I've run into another related problem twice now, and it's not one I've seen talked about
13 previously.
14
15 The problem is this: Sometimes shrinking makes a bug seem much less interesting than it
16 actually is.
17
18 <!--more-->
19
20 I first noticed this problem when [Ned Batchelder](https://nedbatchelder.com/) asked me
21 about some confusing behaviour he was seeing: He was testing some floating point code and
22 had an assertion that the error was not greater than some threshold. Let's say 0.5 (I could
23 dig up the IRC logs, but the exact number doesn't matter).
24
25 Hypothesis said "Ah ha! Here is an example where the error is 0.500001. A bug!".
26
27 Ned sighed and thought "Oh great, floating point precision issues", but on further
28 investigation it turned out that that wasn't it at all. The error could be arbitrarily large,
29 it's just that Hypothesis reliably gave an example where it was almost as small as it could
30 possibly be and still fail.
31
32 This wasn't a bug, either. This is how Hypothesis, QuickCheck, and all of the other tools
33 in this family are designed to work.
34
35 The problem is that test case reduction is designed to produce the simplest example
36 possible to demonstrate the bug. If the bug can be expressed as happening when some
37 score exceeds some threshold, and the score is one that tends to increase with example
38 size, then the failing example that a property-based testing library gives you will tend
39 to be one where the score is barely above that threshold, making the problem look much
40 less bad than it actually is.
41
42 This isn't even a bug in Hypothesis - QuickCheck or any other property-based testing would
43 do the same. It's literally working as intended.
44
45 Arguably it's not even really a problem: Hypothesis has demonstrated the bug, and it's
46 done so with a simple example which should thus be easy to understand.
47
48 But I can't help but feel that we could do better. It definitely produces misleading
49 examples even if they technically demonstrate the right problem, and misleading examples
50 are a great way to waste the user's time.
51
52 I also ran into this problem again recently, where it was more of a problem because it
53 was resulting in flaky tests.
54
55 I recently introduced [a deadline feature](https://hypothesis.readthedocs.io/en/latest/settings.html?highlight=deadline#hypothesis.settings.deadline)
56 as part of the work on Hypothesis performance legibility that [Smarkets](https://smarkets.com/)
57 are funding. This causes slow examples to be treated as failures: If an example passes
58 but took longer than your deadline to run, it raises `DeadlineExceeded`. This is treated
59 as a normal error and Hypothesis shrinks it like anything else (including allowing it to
60 participate in the multi-shrinking process).
61
62 The problem is that it's exactly this sort of threshold problem: You literally have a
63 score (the run time) and a threshold (the deadline) such that when the score exceeds
64 the threshold the test fails. Large examples are certainly likely to be slower, so you
65 will consistently get examples which are right on the boundary of being too slow.
66
67 Which is fine, except that Hypothesis relies on repeatability to display test errors -
68 once it has a minimized example, it replays the test so it can show you the example,
69 print the exception, etc. And test run times are not actually repeatable - a test that
70 takes 201ms on first running might take 199ms on the next run. This then results in
71 Hypothesis thinking the test is flaky - it previously raised `DeadlineExceeded`, and now it
72 doesn't. This lead to [Issue 892](http://github.com/HypothesisWorks/hypothesis-python/issues/892),
73 where Florian Bruhin ran into precisely this problem when testing [Qutebrowser](https://www.qutebrowser.org/).
74
75 The [solution I've ended up opting for there](https://github.com/HypothesisWorks/hypothesis-python/pull/899)
76 is to temporarily raise the deadline during shrinking
77 to something halfway between the actual deadline and the largest runtime we've seen. This
78 ensures that we shrink to a larger threshold than the deadline, and then when we replay
79 we should comfortably exceed the real deadline unless the test performance actually *is*
80 really flaky (in which case I've also improved the error message).
81
82 This solution is currently very specific to the problem of the deadlines, and that's
83 fine - there's no need to rush to a fully general solution, and deadlines have slightly
84 different constraints than other variants of this due to the unreliability of timing -
85 but it is something I'd like to see solved more generally.
86
87 One thing I have thought about for a while is adding some notion of scoring to
88 Hypothesis - e.g. letting people record some score that recorded your progress
89 in testing (testing games where the score could be e.g. the level you've reached, or
90 your literal score in the game, was one use case I had in mind). This would seem to be
91 another good example for that - if you could make your score available to Hypothesis
92 in some way (or if Hypothesis could figure it out automatically!), then a similar
93 solution to the above could be used: If Hypothesis notices that the score of the
94 shrunk example is drastically different from the score of the starting example, it
95 could try rerunning the shrinking process with the additional constraint that the
96 score should stay closer to that of the original example, and display the newly shrunk
97 example with the larger (or smaller) score alongside it. This would work as part
98 of the new multiple failures reporting, so you would see both examples side by side.
99
100 This needs more thought before I jump in and implement something, but I think this is
101 an important problem to solve to improve the usability of Hypothesis in particular and
102 property-based testing in general. Shrinking is a great start to making the problems
103 testing exposes legible to users, but it's only a start, and we need to do more to
104 try to improve developers' productivity when debugging the problems we show them.