Analysis of Lithium's algorithm
I’ve recently been working on automated testcase reduction tools and thus I had the chance to study Jesse Ruderman’s Lithium tool, itself inspired from the ddmin algorithm. This paper contains good ideas, like for example the fact that the reduction could be improved if we rely on the testcase structure like XML nodes or grammar tokens instead of just characters/lines. However, the authors of the ddmin paper really don’t analyse precisely the complexity of the algorithm, except the best and worst case and there is a large gap between the two. Jesse’s analysis is much better and in particular introduces the concepts of monotonic testcase and clustered reduction where the algorithm performs the best and which intuitively seems the usual testcases that we meet in practice. However, the monotonic+clustered case complexity is only “guessed” and the bound for a monotonic testcase (of size with final reduction of size ) is not optimal. For example if the final reduction is relatively small compared to , say then and we can’t say that the number of verifications is small compared to . In particular, Jesse can not deduce from his bound that Lithium’s algorithm is better than an approach based on binary search executions! In this blog post, I shall give the optimal bound for the monotonic case and formalize that in some sense the clustered reduction is near the best case. I’ll also compare Lithium’s algorithm with the binary search approach and with the ddmin algorithm. I shall explain that Lithium is the best in the monotonic case (or actually matches the ddmin in that case).
Thus suppose that we are given a large testcase exhibiting an unwanted behavior. We want to find a smaller test case exhibiting the same behavior and one way is to isolate subtestcases that can not be reduced any further. A testcase can be quite general so here are basic definitions to formalize a bit the problem:
-
•
A testcase is a nonempty finite sets of elements (lines, characters, tree nodes, user actions) exhibiting an “interesting” behavior (crash, hang and other bugs…)
-
•
A reduction of is a testcase with the same “interesting” behavior as .
-
•
A testcase is minimal if .
Note that by definition, is a reduction of itself and is not a reduction of . Also the relation “is a reduction of” is transitive that is a reduction of a reduction of is a reduction of .
We assume that verifying one subset to check if it has the “interesting” behavior is what takes the most time (think e.g. testing a hang or user actions) so we want to optimize the number of testcases verified. Moreover, the original testcase is large and so a fast reduction algorithm would be to have a complexity in . Of course, we also expect to find a small reduction that is .
Without information on the structure on a given testcase or on the properties of the reduction , we must consider the subsets , to find a minimal reduction. And we only know how to do that in operations (or with Grover’s algorithm ;-)). Similarly, even to determine whether is minimal would require testing subsets which is not necessarily (e.g. ). Hence we consider the following definitions:
-
•
For any integer , is -minimal if .
-
•
In particular, is -minimal if .
-
•
is monotonic if .
Finding a -minimal reduction will give a minimal testcase that is no longer interesting if we remove any portion of size at most . Clearly, is minimal if it is -minimal for all . Moreover, is always -minimal for any . We still need to test exponentially many subsets to find a -minimal reduction. To decide whether is -minimal, we need to consider subsets obtained by removing portions of size that is subsets. In particular whether is -minimal is and so if . If is monotonic then so is any reduction of . Moreover, if is a reduction of and , then and so is a reduction of . Hence when is monotonic, is -minimal if and only if it is minimal. We will target -minimal reduction in what follows.
Let’s consider Lithium’s algorithm. We assume that is ordered and so can be identified with the interval (think for example line numbers). For simplicity, let’s first assume that the size of the original testcase is a power of two, that is . Lithium starts by steps . At step , we consider the chunks among the intervals of size . Lithium verifies if removing each chunk provides a reduction. If so, it permanently removes that chunk and tries another chunk. Because is not a reduction of , we immediately increment if it remains only one chunk. The -th step is the same, with chunk of size 1 but we stop only when we are sure that the current testcase is -minimal that is when after attempts, we have not reduced any further. If is not a power of 2 then where . In that case, we apply the same algorithm as (i.e. as if there were dummy elements at the end) except that we don’t need to remove the chunks that are entirely in that additional part. This saves testing at most subtests (those that would be obtained by removing the dummy chunks at the end of sizes ). Hence in general if is the number of subsets of tried by Lithium, we have . Let be the size of the -minimal testcase found by Lithium and .
Lithium will always perform the initial steps above and check at least one subset at each step. At the end, it needs to do operations to be sure that the testcase is -minimal. So . Now, consider the case where monotonic and has one minimal reduction . Then is included in the chunk from step . Because is monotonic, this means that at step , we do two verifications and the second chunk is removed because it does not contain the (and the third one too if is not a power of two), at step it remains two chunks, we do two verifications and the second chunk is removed etc until . For , the number of chunk can grow again: 2, 4, 8… that is we handle at most chunks from step to . At step , a first round of at most verifications ensure that the testcase is of size and a second round of verifications ensure that it is -minimal. So and after simplification . Hence the lower bound is optimal. The previous example suggests the following generalization: a testcase is -clustered if it can be written as the union of nonempty closed intervals . If the minimal testcase found by Lithium is -clustered, each is of length at most and so intersects at most 2 chunks of length from the step . So intersects at most chunks from the step and a fortiori from all the steps . Suppose that is monotonic. Then if is a chunk that does not contain any element of then is a reduction of and so Lithium will remove the chunk . Hence at each step , at most chunks survive and so there are at most chunks at the next step. A computation similar to what we have done for shows that if the final testcase found by Lithium is -clustered. Note that we always have and . So if then is small as wanted. Also, the final testcase is always -clustered (union of intervals that are singletons!) so we found that the monotonic case is . We shall give a better bound below.
Now, for each step , Lithium splits the testcase in at most chunk and try to remove each chunk. Then it does at most steps before stopping or removing one chunk (so the testcase becomes of size at most ), then it does at most steps before stopping or removing one more chunk (so the testcase becomes of size at most ), …, then it does at most steps before stopping or removing one more chunk (so the testcase becomes of size at most ). Then the testcase is exactly of size and Lithium does at most additional verifications. This gives verifications. This bound is optimal if (this is asymptotically true since we assume ): consider the cases where the proper reductions of are exactly the segments and . The testcase will be preserved during the first phase. Then we will keep browsing at least the first half to remove elements at position . So .
We now come back to the case where is monotonic. We will prove that the worst case is and so our assumption gives as we expected. During the steps , we test at most chunks. When , chunks but at most distinct chunks contain an element from the final reduction. By monocity, at most chunks will survive and there are at most chunks at step . Again, only chunks will survive at step and so on until . A the final step, it remains at most elements. Again by monocity a first round of tests will make elements survive and we finally need additional tests to ensure that the test case is minimal. Hence . This bound is optimal: if , consider the case where is the only minimal testcase (and monotonic) ; if is not a power of two, consider the same with points removed at odd positions. Then for each step , no chunks inside are removed. Then some chunks in are removed (none if is a power of two) at step and it remains chunks. Then for steps there are always exactly chunks to handle. So .
We note that we have used two different methods to bound the number of verifications in the general monotonic case, or when the testcase is -clustered. One naturally wonders what happens when we combine the two techniques. So let . From step to , the best bound we found was ; from step to , it was ; from step to it was again ; from step to , it was and finally from step to , including final verifications, it was . Taking the sum, we get Because , this becomes . If , then we get . At the opposite, if , we get . If is not but then and and so the expression can be simplified to . Hence we have obtained an intermediate result between the worst and best monotonic cases and shown how the role played by the number of clusters: the less the final testcase is clustered, the faster Lithium finds it. The results are summarized in the following table:
Number of tests | |
---|---|
Best case | |
is monotonic ; is -clustered | |
is monotonic ; is -clustered ( and unbounded) | |
is monotonic | |
Worst case |
In the ddmin algorithm, at each step we add a preliminary round where we try to immediately reduce to a single chunk (or equivalently to remove the complement of a chunk). Actually, the ddmin algorithm only does this preliminary round at steps where there are more than 2 chunks for otherwise it would do twice the same work. For each step , if one chunk is a reduction of then for some chunk at the previous step . Now if is monotonic then, at level , removing all but the chunk gives a subset that contains and so a reduction of by monocity. Hence on chunk survive at level and there are exactly 2 chunks at level and so the ddmin algorithm is exactly Lithium’s algorithm when is monotonic. The ddmin algorithm keeps in memory the subsets that we didn’t find interesting in order to avoid repeating them. However, if we only reduce to the complement of a chunk, then we can never repeat the same subset and so this additional work is useless. That’s the case if is monotonic.
Finally, if is monotonic Jesse proposes a simpler approach based on a binary search. Suppose first that there is only one minimal testcase . If then intersects and so . Then is not a reduction of for otherwise a minimal reduction of would be a minimal reduction of distinct from which we exclude by hypothesis. If instead then does not intersect and is a reduction of because is monotonic. So we can use a binary search to find by testing at most testcases (modulo some constant). Then we try with intervals to find the second least element of in at most . We continue until we find the -th element of . Clearly, this gives verifications which sounds equivalent to Jesse’s bound with even a better constant factor. Note that the algorithm still works if we remove the assumption that there is only one minimal testcase . We start by and find : if then contains at least one mininal reduction with least element and so is a reduction because is monotonic. If then is not a reduction of or a minimal reduction of would be a minimal reduction of whose least element is greater than . So is a reduction of . The algorithm continues to find etc and finally returns a minimal reduction of . However, it is not clear that this approach can work if is not monotonic while we can hope that Lithium is still efficient if is “almost” monotonic. We remark that when there is only one minimal testcase , the binary search approach would require something like . So that would be the worst case of the binary search approach whereas Lithium handles this case very nicely in ! In general, if there is only one minimal testcase of size then can be anywhere between and if is placed at random, with probability at least . So the average complexity of the binary search approach in that case will be at least which is still not as good as Lithium’s optimal worst case of …