Code efficiency is a very concern for many developers. Moreover, the high low of code execution efficiency is most intuitive for the application's users. It is often one of the key indicators for a successful application. So do you have tools that reflect the code execution efficiency? We can try a try
Junitperf
.
purpose of usage
Test unit test execution efficiency. Since the actual call is actually called in the unit test, the test unit test can be directly reactive efficiency. JUnitPerf is actually a Decorator in Junit, and we can automate this test process by writing units for JUnitPerf. The current version is 1.9, can be downloaded from: http://www.clarkware.com/software/junitperf.htm.
usage
1. Basic use steps:
- Writing JUnit unit test;
- If the unit test execution efficiency is lower than the expected low, write another unit test, isolation possible code;
- Test the corresponding JUnitPerf for this unit, if the test passes, indicating that the isolation code is not a problem. At this point, you need to repeat the above steps. If the test does not pass, you need to reconsider the implementation of the quarantined code to increase efficiency and repeat the entire process.
2. Use TimedTest to test the code to complete the job within the specified time. TimedTest's constructor has three parameters: Test to be tested, the expectation test is complete, and whether the time is waiting to be completed.
- Test a single test method
// Create a Suite method for easy execution using JUnit
Public static test suite () {
// Create a TestCase, in which the constructor is transmitted by the test method to test.
Test TestCase = New Stringutiltest ("TestMethod");
// Create a TestSuite
Testsuite suite = new testsuite ();
// Add a TimedTest
Suite.Addtest (New TimedTest (Testcase, 2000, False);
Return suite;
}
- Test TestSuite
// Create a Suite method for easy execution using JUnit
Public static test suite () {
// Create a TestSuite
Testsuite suite = new testsuite ();
// Add a TimedTest
Suite.Addtest (New TestSuite (Stringutiltest.class), 2000, False);
Return suite;
}
3. Use loadtest to perform a pressure test. LoadTest is used to simulate a certain number of user parallel use unit tests. It has four constructor, from the constructor, you can see the main features of LoadTest:
- LoadTest (Test Test, INT Users), creating users of users, each with each request.
- LoadTest (Test Test, INT Users, int ution), creating users of users, each user requests Items.
- LoadTest (Test Test, INT Users, Int Items, Timer Timer), creating users users, using Timer request delay, each user requests ITerations. - LoadTest (Test Test, INT Users, Timer Timer), creating users using the number of users, using Timer specifying the delay, each user requests 1 time.
Please refer to the corresponding Javadoc in the Timer interface. It is similar to TimedTest. The example is as follows:
......
Test Test = New Stringutiltest ("TestMethod");
Testsuite suite = new testsuite ();
Suite.Addtest (New Loadtest (TEST, 100, 10));
......
4. Combine tests and play the potential of Decorator. JUnitPerf is actually a JUnit's Decorator. From Decorator's definition, you can use these two mutually Decorate.
- Use TimedTest Decorate Loadtest to test whether requests for parallel indicators meet the efficiency indicators.
......
Test Test = New Stringutil ("TestMethod");
Testsuite suite = new testsuite ();
Suite.Addtest (New TimedTest (New Loadtest (Test, 100, 10), 3000, False);
......
- Use loadTest Decorate Timetest to test whether a single request to meet the efficiency indicator meets the parallel indicator.
......
Test Test = New Stringutil ("TestMethod");
Testsuite suite = new testsuite ();
Suite.Addtest (New Loadtest (New TimedTest (Test, 300, False), 1000, 10));
......
5. Use Ant to automate testing. JUnit provides Ant Test, which automates Unit Test, we can use this task to automate JUnitPerf (because it is decorator). However, it is not the same as the side focus of Unit Test, so it is necessary to distinguish them. From JUnit's introduction, you can see that Ant Task is mainly to find the test class to be executed by naming rules, because in order to distinguish them, we only need to make the naming rules of these two tests are different. As for other aspects, it is exactly the same as JUnit.
checklist
The following is a problem that needs attention when using JUnitPerf:
- TimedTest's test time is not very accurate, but a rough value. For a test method, the time it measures is to refer to the total time of Setup, test method and Teardown.
- When performing loadingtest, please carefully select the request for the request, if it extends the conference, it is not truly parallel.
- Distinguish ordinary JUnit using different naming rules.
- JUnitperf is not used to replace those performing efficiency test tools, which should only be used for localized efficiency testing and better reconstruction. (From the JUnitperf document).