P
Philipp
Hello,
In my code, I must sometimes implement peformance optimizations. By
definition, the functional behavior does not change through this,
which can be tested through the public interface (regression tests).
But I would also like to test, that the optimization is used in the
correct places. Even using reflection to gain private access to
fields, I find this is difficult without introducing code (e.g. flags)
which only use is testing.
As an example consider the below code found in Arrays.mergeSort of the
Sun JDK. How would you go about testing, that the insertion sort is
used in the correct cases?
Is there any specific tool available for JUnit (or any other) to test
performance automatically? Can we test that the code passes at certain
points or that certain methods have been called? If not, is there a
nice way to check in the log for a sign indicating the use of the
optimization?
Best regards
Phil
PS: Does Sun remove all log statements from its JDK src before
distribution or are they developing without logs (and should thus be
considered half-gods)?
--- Code from Arrays.java in Sun JDK 1.6 ---
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src to dest. This is
an
// optimization that results in faster sorts for nearly ordered
lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])
<=0)
dest = src[p++];
else
dest = src[q++];
}
}
In my code, I must sometimes implement peformance optimizations. By
definition, the functional behavior does not change through this,
which can be tested through the public interface (regression tests).
But I would also like to test, that the optimization is used in the
correct places. Even using reflection to gain private access to
fields, I find this is difficult without introducing code (e.g. flags)
which only use is testing.
As an example consider the below code found in Arrays.mergeSort of the
Sun JDK. How would you go about testing, that the insertion sort is
used in the correct cases?
Is there any specific tool available for JUnit (or any other) to test
performance automatically? Can we test that the code passes at certain
points or that certain methods have been called? If not, is there a
nice way to check in the log for a sign indicating the use of the
optimization?
Best regards
Phil
PS: Does Sun remove all log statements from its JDK src before
distribution or are they developing without logs (and should thus be
considered half-gods)?
--- Code from Arrays.java in Sun JDK 1.6 ---
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src to dest. This is
an
// optimization that results in faster sorts for nearly ordered
lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])
<=0)
dest = src[p++];
else
dest = src[q++];
}
}