summaryrefslogtreecommitdiff
path: root/java/junit-toolkit/src/main/org/apache/qpid/junit/extensions/AsymptoticTestCase.java
blob: 58a7f60f3ce0f89540aabf06c251786bf47f124b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.junit.extensions;

import junit.framework.TestCase;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * AsymptoticTestCase is an extension of TestCase for writing unit tests to analyze asymptotic time and space behaviour.
 *
 * <p>ParameterizedTestCases allow tests to be defined which have test methods that take a single int argument. Normal
 * JUnit test methods do not take any arguments. This int argument can be interpreted in any way by the test but it is
 * intended to denote the 'size' of the test to be run. For example, when testing the performance of a data structure
 * for different numbers of data elements held in the data structure the int parameter should be interpreted as the
 * number of elements. Test timings for different numbers of elements can then be captured and the asymptotic behaviour
 * of the data structure with respect to time analyzed. Any non-parameterized tests defined in extensions of this class
 * will also be run.
 *
 * <p>TestCases derived from this class may also define tear down methods to clean up their memory usage. This is
 * intended to be used in conjunction with memory listeners that report the amount of memory a test uses. The idea is
 * to write a test that allocates memory in the main test method in such a way that it leaves that memory still
 * allocated at the end of the test. The amount of memory used can then be measured before calling the tear down method
 * to clean it up. In the data structure example above, a test will allocate as many elements as are requested by the
 * int parameter and deallocate them in the tear down method. In this way memory readings for different numbers of
 * elements can be captured and the asymptotic behaviour of the data structure with respect to space analyzed.
 *
 * <p><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Store the current int parameter value. <td> {@link TKTestResult} and see {@link AsymptoticTestDecorator} too.
 * <tr><td> Invoke parameterized test methods.
 * </table>
 *
 * @todo If possible try to move the code that invokes the test and setup/teardown methods into {@link TKTestResult} or
 *       {@link AsymptoticTestDecorator} rather than this class. This would mean that tests don't have to extend this
 *       class to do time and space performance analysis, these methods could be added to any JUnit TestCase class
 *       instead. This would be an improvement because existing unit tests wouldn't have to extend a different class to
 *       work with this extension, and also tests that extend other junit extension classes could have parameterized
 *       and tear down methods too.
 *
 * @author Rupert Smith
 */
public class AsymptoticTestCase extends TestCase implements InstrumentedTest
{
    /** Used for logging. */
    private static final Logger log = Logger.getLogger(AsymptoticTestCase.class);

    /** The name of the test case. */
    private String testCaseName;

    /** Thread local for holding measurements on a per thread basis. */
    ThreadLocal<TestMeasurements> threadLocalMeasurement =
        new ThreadLocal<TestMeasurements>()
        {
            /**
             * Sets up a default set test measurements (zeroed, apart from the size param which defaults to 1).
             *
             * @return A set of default test measurements.
             */
            protected synchronized TestMeasurements initialValue()
            {
                return new TestMeasurements();
            }
        };

    /**
     * Constructs a test case with the given name.
     *
     * @param name The name of the test.
     */
    public AsymptoticTestCase(String name)
    {
        super(name);

        log.debug("public AsymptoticTestCase(String " + name + "): called");
        testCaseName = name;
    }

    /**
     * Gets the current value of the integer parameter to be passed to the parameterized test.
     *
     * @return The current value of the integer parameter.
     */
    public int getN()
    {
        log.debug("public int getN(): called");
        int n = threadLocalMeasurement.get().n;

        log.debug("return: " + n);

        return n;
    }

    /**
     * Sets the current value of the integer parameter to be passed to the parameterized test.
     *
     * @param n The new current value of the integer parameter.
     */
    public void setN(int n)
    {
        log.debug("public void setN(int " + n + "): called");
        threadLocalMeasurement.get().n = n;
    }

    /**
     * Reports how long the test took to run.
     *
     * @return The time in milliseconds that the test took to run.
     */
    public long getTestTime()
    {
        log.debug("public long getTestTime(): called");
        long startTime = threadLocalMeasurement.get().startTime;
        long endTime = threadLocalMeasurement.get().endTime;
        long testTime = endTime - startTime;

        log.debug("return: " + testTime);

        return testTime;
    }

    /**
     * Reports the memory usage at the start of the test.
     *
     * @return The memory usage at the start of the test.
     */
    public long getTestStartMemory()
    {
        // log.debug("public long getTestStartMemory(): called");
        long startMem = threadLocalMeasurement.get().startMem;

        // log.debug("return: " + startMem);

        return startMem;
    }

    /**
     * Reports the memory usage at the end of the test.
     *
     * @return The memory usage at the end of the test.
     */
    public long getTestEndMemory()
    {
        // log.debug("public long getTestEndMemory(): called");
        long endMem = threadLocalMeasurement.get().endMem;

        // log.debug("return: " + endMem);
        return endMem;
    }

    /**
     * Resets the instrumentation values to zero, and nulls any references to held measurements so that the memory
     * can be reclaimed.
     */
    public void reset()
    {
        log.debug("public void reset(): called");
        threadLocalMeasurement.remove();
    }

    /**
     * Runs the test method for this test case.
     *
     * @throws Throwable Any Throwables from the test methods invoked are allowed to fall through.
     */
    protected void runTest() throws Throwable
    {
        log.debug("protected void runTest(): called");

        // Check that a test name has been set. This is used to define which method to run.
        assertNotNull(testCaseName);
        log.debug("testCaseName = " + testCaseName);

        // Try to get the method with matching name.
        Method runMethod = null;
        boolean isParameterized = false;

        // Check if a parameterized test method is available.
        try
        {
            // Use getMethod to get all public inherited methods. getDeclaredMethods returns all
            // methods of this class but excludes the inherited ones.
            runMethod = getClass().getMethod(testCaseName, int.class);
            isParameterized = true;
        }
        catch (NoSuchMethodException e)
        {
            // log.debug("Parameterized method \"" + testCaseName + "\" not found.");
            // Set run method to null (it already will be but...) to indicate that no parameterized method
            // version could be found.
            runMethod = null;
        }

        // If no parameterized method is available, try and get the unparameterized method.
        if (runMethod == null)
        {
            try
            {
                runMethod = getClass().getMethod(testCaseName);
                isParameterized = false;

            }
            catch (NoSuchMethodException e)
            {
                fail("Method \"" + testCaseName + "\" not found.");
            }
        }

        // Check that the method is publicly accessable.
        if (!Modifier.isPublic(runMethod.getModifiers()))
        {
            fail("Method \"" + testCaseName + "\" should be public.");
        }

        // Try to execute the method, passing it the current int parameter value. Allow any invocation exceptions or
        // resulting exceptions from the method to fall through.
        try
        {
            Integer paramN = getN();
            log.debug("paramN = " + paramN);

            // Calculate parameters for parameterized tests so new does not get called during memory measurement.
            Object[] params = new Object[] { paramN };

            // Take the test start memory and start time.
            threadLocalMeasurement.get().startMem = 0; // SizeOf.getUsedMemory();

            threadLocalMeasurement.get().startTime = System.nanoTime();

            if (isParameterized)
            {
                runMethod.invoke(this, params);
            }
            else
            {
                runMethod.invoke(this);
            }
        }
        catch (InvocationTargetException e)
        {
            e.fillInStackTrace();
            throw e.getTargetException();
        }
        catch (IllegalAccessException e)
        {
            e.fillInStackTrace();
            throw e;
        }
        finally
        {
            // Take the test end memory and end time and calculate how long it took to run.
            long endTime = System.nanoTime();
            threadLocalMeasurement.get().endTime = endTime;
            log.debug("startTime = " + threadLocalMeasurement.get().startTime + ", endTime = " + endTime + ", testTime = "
                + getTestTime());

            threadLocalMeasurement.get().endMem = 0; // SizeOf.getUsedMemory();
        }
    }

    /**
     * The test parameters, encapsulated as a unit for attaching on a per thread basis.
     */
    private static class TestMeasurements
    {
        /** Holds the current value of the integer parameter to run tests on. */
        public int n = 1;

        /** Holds the test start memory. */
        public long startTime = 0;

        /** Holds the test end memory. */
        public long endTime = 0;

        /** Holds the test start memory. */
        public long startMem = 0;

        /** Holds the test end memory. */
        public long endMem = 0;
    }
}