Wednesday, March 17, 2010

Running JUnit tests to generate load on the system

JUnitTester1,2 etc are all our Test classes.
This code will create 5 threads running each all tests forever, with 500 ms pause at the end of each test sequence.
With this we can create load on the system.



package com.pierre.testall;

import com.pierre.junittester.JUnitTester1;
import com.pierre.junittester.JUnitTester2;

public class TestCatalog {
    public static Class[] ALL_TESTS = new Class[] {JUnitTester1.class, JUnitTester2.class};
}



package com.pierre.testall;

import junit.textui.TestRunner;

public class AllTests implements Runnable {
    public static void main(String[] args) {
        AllTests allTests = new AllTests();
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(allTests);
            thread.start();
        }
    }

    public void run() {
        while (true) {
            for (Class clazz : TestCatalog.ALL_TESTS) {
                TestRunner.run(clazz);
            }
            sleep(500);
        }
    }

    public void sleep(int ms) {
        try {
            Thread.sleep(ms);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


This  http://code.google.com/p/javatoolsforweblogic/source/browse/trunk/StressTest/src/com/pierre/test/StressTest.java
is also a good source of inspiration.

No comments: