| 1 | --- |
| 2 | tags: technical, intro, python, properties |
| 3 | date: 2016-04-29 11:00 |
| 4 | title: Testing performance optimizations |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | Once you've |
| 9 | [flushed out the basic crashing bugs](../getting-started-with-hypothesis/) |
| 10 | in your code, you're going to want to look for more interesting things to test. |
| 11 | |
| 12 | The next easiest thing to test is code where you know what the right answer is for every input. |
| 13 | |
| 14 | Obviously in theory you think you know what the right answer is - you can just run the code. That's not very |
| 15 | helpful though, as that's the answer you're trying to verify. |
| 16 | |
| 17 | But sometimes there is more than one way to get the right answer, and you choose the one you run in production |
| 18 | not because it gives a different answer but because it gives the same answer *faster*. |
| 19 | |
| 20 | <!--more--> |
| 21 | |
| 22 | For example: |
| 23 | |
| 24 | * There might be a fancy but fast version of an algorithm and a simple but slow version of an algorithm. |
| 25 | * You might have a caching layer and be able to run the code with and without caching turned on, or with a |
| 26 | different cache timeout. |
| 27 | * You might be moving to a new database backend to improve your scalability, but you still have the code for |
| 28 | the old backend until you've completed your migration. |
| 29 | |
| 30 | There are plenty of other ways this can crop up, but those are the ones that seem the most common. |
| 31 | |
| 32 | Anyway, this creates an *excellent* use case for property based testing, because if two functions are supposed |
| 33 | to always return the same answer, you can test that: Just call both functions with the same data and assert |
| 34 | that their answer is the same. |
| 35 | |
| 36 | Let's look at this in the fancy algorithm case. Suppose we implemented [merge sort]( |
| 37 | https://en.wikipedia.org/wiki/Merge_sort): |
| 38 | |
| 39 | ```python |
| 40 | def merge_sort(ls): |
| 41 | if len(ls) <= 1: |
| 42 | return ls |
| 43 | else: |
| 44 | k = len(ls) // 2 |
| 45 | return merge_sorted_lists(merge_sort(ls[:k]), merge_sort(ls[k:])) |
| 46 | |
| 47 | |
| 48 | def merge_sorted_lists(x, y): |
| 49 | result = [] |
| 50 | i = 0 |
| 51 | j = 0 |
| 52 | while i < len(x) and j < len(y): |
| 53 | if x[i] <= y[j]: |
| 54 | result.append(x[i]) |
| 55 | i += 1 |
| 56 | else: |
| 57 | result.append(y[j]) |
| 58 | j += 1 |
| 59 | return result |
| 60 | ``` |
| 61 | |
| 62 | We want a reference implementation to test it against, so lets also implement [bubble sort]( |
| 63 | https://en.wikipedia.org/wiki/Bubble_sort): |
| 64 | |
| 65 | ```python |
| 66 | def bubble_sort(ls): |
| 67 | ls = list(ls) |
| 68 | needs_sorting = True |
| 69 | while needs_sorting: |
| 70 | needs_sorting = False |
| 71 | for i in range(1, len(ls)): |
| 72 | if ls[i - 1] > ls[i]: |
| 73 | needs_sorting = True |
| 74 | ls[i - 1], ls[i] = ls[i], ls[i - 1] |
| 75 | return ls |
| 76 | ``` |
| 77 | |
| 78 | These *should* always give the same answer, so let's test that: |
| 79 | |
| 80 | ```python |
| 81 | @given(lists(integers())) |
| 82 | def test_bubble_sorting_is_same_as_merge_sorting(ls): |
| 83 | assert bubble_sort(ls) == merge_sort(ls) |
| 84 | ``` |
| 85 | |
| 86 | This gives us an error: |
| 87 | |
| 88 | ``` |
| 89 | @given(lists(integers())) |
| 90 | def test_bubble_sorting_is_same_as_merge_sorting(ls): |
| 91 | > assert bubble_sort(ls) == merge_sort(ls) |
| 92 | E assert [0, 0] == [0] |
| 93 | E Left contains more items, first extra item: 0 |
| 94 | E Use -v to get the full diff |
| 95 | |
| 96 | foo.py:43: AssertionError |
| 97 | ----- Hypothesis ----- |
| 98 | Falsifying example: test_bubble_sorting_is_same_as_merge_sorting(ls=[0, 0]) |
| 99 | ``` |
| 100 | |
| 101 | What's happened is that we messed up our implementation of merge\_sorted\_lists, because we forgot |
| 102 | to include the elements left over in the other list once we've reached the end of one of them. As a |
| 103 | result we ended up losing elements from the list, a problem that our simpler implementation lacks. |
| 104 | We can fix this as follows and then the test passes: |
| 105 | |
| 106 | ```python |
| 107 | def merge_sorted_lists(x, y): |
| 108 | result = [] |
| 109 | i = 0 |
| 110 | j = 0 |
| 111 | while i < len(x) and j < len(y): |
| 112 | if x[i] <= y[j]: |
| 113 | result.append(x[i]) |
| 114 | i += 1 |
| 115 | else: |
| 116 | result.append(y[j]) |
| 117 | j += 1 |
| 118 | result.extend(x[i:]) |
| 119 | result.extend(y[j:]) |
| 120 | return result |
| 121 | ``` |
| 122 | |
| 123 | This technique combines especially well with |
| 124 | [Hypothesis's stateful testing](../rule-based-stateful-testing/), because |
| 125 | you can use it to then test different implementations of complex APIs. For example, Hypothesis uses this |
| 126 | property together with stateful testing to [verify that the different implementations of its example database |
| 127 | behave identically](https://github.com/HypothesisWorks/hypothesis/blob/master/hypothesis-python/tests/nocover/test_database_agreement.py). |