Junitperf
Summary
JUnitPerf is a performance test tool for the performance and execution efficiency of measured code, which can automate this process by writing unit test code for JUnitPerf. From another perspective, it is an extension in JUnit.
If you are interested in this type of automation test, you can refer to my book "Pragmatic Project Automation"
You can also spend two days to visit the Site "Test-Driven Development with Junit Workshop", here there is some good JUnit's learning channels and some of the Mike Clark's symbols and practice guidelines help you understand the test more deeply. Drive development.
table of Contents
· Introduction
· purpose of usage
· JUnitperf download
· JUnitPerf installation
· JUnitperf build and test
· How to use JunitPerf
· Writing a valid JUnitPerf test
· Limitations
· Technical Support
· Donation
· Training and guidance
· License Information
Thank you
· related resources
Introduction
JUnitPerf is an automated test framework (tool) based on a metric performance and execution efficiency based on JUnit.
JUnitPerf contains two main classes (extended JUnit):
· TIMEDTEST
TIMEDTEST is used to perform tests and return to the time used to perform this test.
A maximum acceptable execution time is required in the TIMEDTEST construction method. By default, the test is performed when the method is executed, and if the time actually used exceeds the specified maximum time, the identification test failed. In addition, you can also do this test by specifying the actual execution time when the actual execution time exceeds the maximum acceptable time, and the identification test is not passed.
Loadtest
LoadTest is used to simulate multiple computational users multiple iterations to perform tests.
purpose of usage
Obviously, JUnitPerf is an extension to the JUnit test framework. This way is extended to dynamically increase the JUnit test case to perform performance testing, which does not affect the previous test. This way you can quickly and easily construct a performance test kit.
Performance test kits can be performed automatically, independent of other JUnit test cases. In actual use, you should generally try to organize the JUnit test case and JUnitPerf test cases, so that the test kit can be performed more independently, and can be executed in different order. Long duration performance tests may extend the test time, resulting in unwilling to perform all unit tests. Therefore, this requires you to perform this test from time to time without having to affect other work.
JUnitPerf tends to maintain such a target test for a clear performance requirement or performing efficiency requirement, and to ensure that the code is rebuild. For example, you can use JunitPERF testing to ensure that performance will not be reduced due to change algorithms under the same conditions. You can also use it to ensure that it does not cause a resource pool to reduce the efficiency reduction in the case (this guarantee is based on the execution time and efficiency before and after the comparison conditions, only provide a measure of the basis).
It is quite important to maintain a focus effective test from the perspective of investment output. Traditional performance metrics and technologies will first identify potential sources of performance issues, while JunitPerf is used to constantly test and check demand and actual results.
The following is an example of actual use scene:
You have a good program and pass the necessary JUnit test suite. From this perspective, you have reached the goal of design imagination.
Then use a performance metric tool to analyze which part of the program is longer executed. Based on design knowledge, you have already had a good tool to actually evaluate the program. And the code after the reconstruction is clear and concise, the next job is to adjust a small part of code. Next, you can write the JUnitPerf test case, specify acceptable performance and efficiency parameters for this part of the code. If you do not do any changes to the code, you will not pass the test, it will not pass, prove that the test case is correct. Then do some small adjustments to the code.
Rebate and run the JUnitPerf test after each adjustment. If the actual performance reaches the expected indicator, the test is passed. If the actual performance does not reach the expected indicator, continue the adjustment process until the test passes. If you reconstruct your code again, you can also re-run the test. If the test is not passed, while the previous performance standard is also improved, it takes back to the original and continues to be refactored until the test passes.
Junitperf download
JUnitPerf 1.9 is the current latest version. Contains all previous versions of the feature.
This version requires Java 2 and JUnit 3.5 or above.
The release package contains a JAR file, source code, sample code, API document, and this document.
JunitPerf installation
Windows
Install the following steps on Windows:
1. Decompress the junitperf-
2. Add JUnitPerf to the ClassPath path:
Set classpath =% classpath%;% junitperf_home% / lib / junitperf-
UNIX (Bash)
Install the following steps on UNIX:
1. Unzip JUnitPerf-
2. Modify the permission of the file:
CHMOD -R A X $ JUNITPERF_HOME
3. Add JUnitPerf to the ClassPath path:
Export ClassPath = $ ClassPath: $ junitperf_home / lib / junitperf-
Construction and testing
In $ junitperf_home / lib / junitperf-
Construct
$ Junitperf_home / build.xml file is an ANT build file.
You can build JUnitPerf using the following command:
CD $ junitperf_home
ANT JAR
test
An instance of JUnit test cases for use with JUnitPerf is included in the JUnitPerf installation package.
You can enter the following command to verify that the JUnitPerf installation is normal:
CD $ junitperf_home
Ant Test
How to use JunitPerf
The best way is to use an example of attachment in JUnitPerf, which includes various types of tests.
The $ junitperf_home / Samples directory contains all sample code described in this article.
TIMEDTEST
The TIMEDTEST construction method has two parameters, one is an existing JUnit test case, and the other is the expected maximum execution time.
For example, you want to create an execution time test for an ExampleTestCase.TestoneseCondResponse () method and wait for the method to perform, if the time is more than 1 second, it is not passed.
Long maxlapsedtime = 1000;
Test Testcase = New Exampletestcase ("TestonesecondResponse"); test timedtest = new timedTest (Testcase, Maxelapsedtime);
Similarly, if you want a process, if you exceed the expected time, this test can be added to the TimedTest constructor, as follows:
Long maxlapsedtime = 1000;
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test Timedtest = New TimedTest (Testcase, Maxelapsedtime, False);
The following code creates an execution time test to test the time that is defined in the unit tests the function of the EXAMPLETESTCASE.TESTOSECondResponse () method execution.
Perform efficiency test example
Import com.clarkware.junitperf. *;
Import junit.framework.test;
Public class exampletimedtest {
Public static test suite () {
Long maxlapsedtime = 1000;
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test TimedTest = New TimedTest (Testcase, Maxelapsedtime);
Return TimedTest;
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run (Suite ());
}
}
The particle size of the test is determined in the JUnit test case and is used by JUnitperf, so there is a certain limit. The final resulting time is executed in the test case, including the execution time of the Testxxx () method, including setup (), testxxx (), and the TEARDOWN () method execution time. The time to perform the test suite contains setup (), testxxx (), and Teardown () methods execution time of all test examples in the test suite. Therefore, the expected time should also be set according to the execution time of SET-UP and TEAR-DOWN (taken into account).
Loadtest
Loadtest is used to follow multiple users and perform multiple times to test.
The simplest constructor of LoadTest has only two parameters, test cases, and users, by default iteration iterate once.
For example, create a 10 user concurrently execute an ExampleTestCase.testonesecondResponse () method:
INT users = 10;
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test loadtest = New loadtest (Testcase, Users);
The load testing process can also specify an additional counter instance to specify the delay time between the user concurrent execution. The ConstantTimer class constructor contains a constant parameter that specifies the delay time, if specified as 0, all users start at the same time. The Randomtimer class can construct a random delay time.
For example: Creating a load test, 10 concurrent users execute an ExampleTestCase.testoneseCondResponse () method, and each user is delayed for 1 second. INT users = 10;
Timer Timer = New ConstantTimer (1000);
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test LoadTest = New Loadtest (Testcase, Users, Timer);
In order to effectively effectively perform tests with the specified iterative number, the LoadTest class constructor contains the REPEATEDTEST parameter. This allows each test case to specify the number of iterations.
For example: Creating a load test, 10 concurrent users, each user iteration executes an ExampleTestCase.TestoneSecondResponse () method 20 times, each delay between 1 second.
INT users = 10;
INT ITERATIONS = 20;
Timer Timer = New ConstantTimer (1000);
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test ReatedTest = New RepeatedTest (Testcase, Items);
Test loadingTest = New LoadTest (RepeatedTest, Users, Timer);
Or let's write:
INT users = 10;
INT ITERATIONS = 20;
Timer Timer = New ConstantTimer (1000);
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test loadingTest = New Loadtest (Testcase, Users, Items, Timer);
If the load test requires testing in the Setup () method, you should use the TestFactory class to ensure that each concurrent user thread uses a local thread test instance. For example, create a 10-user concurrent test, each user runs a local thread of the ExamplestateFultest class, so you can write:
INT users = 10;
TEST FACTORY = New TestFactory (ExamplestateFultest.class);
Test loadtest = new loadtest (factory, users);
If you test one of the methods, you can do this:
INT users = 10;
Test Factory = New TestMethodFactory (ExamplestateFultest.class, "Testsomething");
Test loadtest = new loadtest (factory, users);
The following example is a load test for the test unit to test the functionality of the exampleTestCase.TestOneSecondResponse () method to test the performance efficiency of the function. There are 10 concurrent users, no delay, each user only runs once. LoadTest itself uses TimedTest to get the actual operational capabilities of the ExamplestCase.testoneseCondResponse () method in the load. If all execution time exceeds 1.5 seconds, it is considered not passed. 10 concurrent processes were passed in 1.5 seconds. Examples of capacity testing under load
Import com.clarkware.junitperf. *;
Import junit.framework.test;
Public Class ExamplethRoughputUnderLoadTest {
Public static test suite () {
INT MAXUSERS = 10;
Long maxlapsedtime = 1500;
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test loadtest = new loadtest (Testcase, MaxUsers);
Test TimedTest = New TimedTest (loadingTest, Maxelapsedtime);
Return TimedTest;
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run (Suite ());
}
}
In the following example, the test is reversed, the TimedTest metric is executed for execution time of the exampletestcase.testoneSecondResponse () method. Then Loadtest is nesting TimedTest to follow 10 concurrent users to execute the ExampleSecondResponse () method. If the execution time of a user exceeds 1 second, it is considered not passed.
Load response time test example
Import com.clarkware.junitperf. *;
Import junit.framework.test;
Public class exampleresponsetimeunderloadtest {
Public static test suite () {
INT MAXUSERS = 10;
Long maxlapsedtime = 1000;
Test Testcase = New ExampleTestCase ("TestonesecondResponse");
Test TimedTest = New TimedTest (Testcase, Maxelapsedtime);
Test loadingTest = New loadtest (TimedTest, MaxUsers);
Return loadtest;
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run (Suite ());
}
}
Performance test kit
In the following test case example combines ExampleTIMedTest and ExampleLoadTest in a test suite, so that all relevant performance tests can be automatically performed:
EXAMPLE Performance Test Suite
Import junit.framework.test;
Import junit.framework.testsuite;
Public class exampleperftestsuite {
Public static test suite () {
Testsuite suite = new testsuite (); suite.addtest (exampletimedtest.suite);
Suite.addtest (ExampleLoadTest.suite ());
Return suite;
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run (Suite ());
}
}
Write a valid JUnitPerf test
Timed Tests
Waiting Timed Tests
By default, if the actual test time exceeds the expected time, the JUnit is processed. This WAITITING TIMED TEST always allows JUnit tests to accumulate all test results until the test is complete and check all the test results.
If the test execution is waiting for the use case to be tested directly or indirectly derived multiple threads, then this test is only returned to the TIMED TEST in this test. On the other hand, this test will be waiting in an indefinitely. In general, unit testing should wait for all derived threads to execute, such as using thread.join () methods to accurately determine the results.
Non-waiting timed tests
In addition, TIMEDTEST also provides a construction method that is immediately exposed when the actual time exceeds the expected time. This type of test If the execution time exceeds the expected maximum time, it will not wait for the test to continue. This type of test is more effective than the previous way, saving time as needed, will no longer wait for the program to perform and immediately identify the test. However, with one type above, this type of test does not continue to perform the latter test if there is a test in the middle without passing.
Load tests
Non-Atomic Load Tests
By default, if the test is derived or indirectly derived directly or indirectly, it does not force this thread and execute (just as defined in the transaction). This type of test assumes that its extended test is completed interactively when it returns control. For example, if the derived thread and control of the extended test return, the expansion test is assumed to be done one-time.
In general, in order to accurately determine the results in unit test, the derived thread should be sent, for example, using thread.join () methods, however, not necessarily in some cases. For example, for EJB distributed query results, the application server may derive a new thread to process this request. If the new thread runs the Decoated test in the same thread group (default), a non-atomic pressure test is just waiting for the pressure test. Directly derived threads, the newly generated thread will be ignored.
In summary, non-atomic pressure tests simulate multiple concurrent users only waiting for the threads directly derived in the pressure test.
Atomic Load Tests
If multiple threads specify a Decorated test successfully, this means that only the thread execution in all Decorated tests is completed, which is considered to be completed. This test () can be combined using StenforceteStator (TRUE). This will effectively promote this test waiting for all threads of the thread group belonging to the Decorated test. Atomic pressure testing will also make any premature exit threads as failure. If a thread suddenly crashes, then other threads belonging to the same thread group will immediately stop execution.
If the Decorated test derived thread belongs to the same thread group, the Decoated test is executed by default, so that the atomic pressure test will be completed indefinitely wait for the derived thread.
In summary, atomic pressure test will wait for all threads that belong to the same thread group, and the pressure test directly derived threads to simulate multiple users.
limitation
JUnitPerf is known to have the following flaws:
· TimeDtest returned time is the TestXXXX () method of the test case, including the total time of setup (), testxxx (), and TEARDOWN (), which is the minimum test particle size that can be provided in any test instance. Therefore, the desired time should also consider the runtime of SET-UP and TEAR-DOWN. (Translator Note: Or you can use the System.CurrentTimeMillis () method in the JUnit test case to calculate the execution time of a step)
· JunitPerf is not a complete pressure and performance test tool, and it will not be used to replace other similar tools. It is only used to write local unit performance tests to help developers are refactored.
· The Performance of Your Tests CAN Degrade Signiffantly IF Too Many Concurrent Users Are Cooperating in A Load Test. The Actual Threshold Number IS JVM Specific.
· If there are too many users in the pressure test, the test is getting worse. The number of users should be specified with reference to the JVM specification.
Technical Support
If you have any questions about JUnitPerf, you need to improve the requirements, successful experience or bug, or get notifications when there is a new version release, please send email to mike@cline.com. Your personal information will not be disclosed.
You can also discuss the JUnitPerf through the Mail list (http://groups.yahoo.com/group/junitperf/), and receive notifications when there is a new version release.
Donation
You can support JUnitPerf continued development by purchasing a book of Pragmatic Project Automation (http://www.pragmaticprogrammer.com.
Training and guidance
You can learn how to quickly create test code is learned by accessing site http://clineware.com/course/tddwithjunit.html.
Here is also the guidance of JUnit (http://cline.com/ethoring.html) to help you improve the test.
If you want more information, please contact me (Mailto: Mike@clarkware.com).
License information
JUnitperf is licensed under the bsd license.
thank
I am very grateful to Ervin Varga helps from thread robust and atomic testing. He proposed a thread group to capture and handle the exception of the thread in these respects, and it is implemented as a failure implementation if the execution time is timeout in TimeTest and TestFactory. I am very grateful to the favor and suggestions of JunitPerf.
translation
MSN: WyingQuan At Hotmail Dot COM Complete Time:
2005-4-19
Related resources and reference documents
· JUnit Primermike Clark, Clarkware Consulting, Inc.
This article briefly explains how to use the JUnit test framework to write and run simple test cases and kits.
· Continuous Performance Testing with JunitPerfmike Clark (Javapronews, 2003)
This article explains how to write JUnitPerf tests from time to time to check performance and measurability.
· Test-Driven Development: A Practical Guide David ASTELS (Prentice Hall, 2003) includes a chapter how to use JunitPerf to continuous performance test by this author.
· Java Extreme Programming Cookbookeric Burke, Brian Coyner (O'Reilly & Associates, 2003)
One of them specifically tells the use of JUnitPerf.
· Java Tools for Extreme Programming: Mastering Open Source Tools include, Junit, And Cactusrichard Hightower, Nicholas Lesiecki (John Wiley & Sons, 2001)
A chapter describes how to use JunitPerf with HTTPUnit.