devutility.internal.test.TestExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devutility.internal Show documentation
Show all versions of devutility.internal Show documentation
Utilities for Java development
package devutility.internal.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import devutility.internal.base.Callback;
import devutility.internal.lang.ClassHelper;
import devutility.internal.util.concurrent.CompletionServiceUtils;
import devutility.internal.util.concurrent.ExecutorServiceUtils;
public class TestExecutor {
/**
* Run BaseTest instance
* @param instance: BaseTest instance
*/
public static void run(BaseTest instance) {
if (instance == null) {
return;
}
preExecute(instance.getClass());
long startTime = System.currentTimeMillis();
instance.run();
postExecute(startTime, instance.getClass());
}
/**
* Create a BaseTest instance and run it.
* @param clazz: BaseTest instance
*/
public static void run(Class clazz) {
if (clazz == null) {
return;
}
T instance = ClassHelper.instance(clazz);
if (instance == null) {
System.out.println("Create new instance failed!");
return;
}
preExecute(clazz);
long startTime = System.currentTimeMillis();
instance.run();
postExecute(startTime, clazz);
}
/**
* Create a BaseTest instance and concurrently run it.
* @param clazz: BaseTest instance
*/
public static void concurrentRun(Class clazz) {
if (clazz == null) {
return;
}
T instance = ClassHelper.instance(clazz);
if (instance == null) {
return;
}
Runnable task = () -> {
preExecute(clazz);
long startTime = System.currentTimeMillis();
instance.run();
postExecute(startTime, clazz);
};
ExecutorServiceUtils.threadPoolExecutor().execute(task);
}
/**
* Running instance list concurrently.
* @param instances: Instance list
* @param clazz: BaseTest instance
*/
public static void concurrentRun(List instances, Class clazz) {
concurrentRun(instances, clazz, null);
}
/**
* Running {@code count} instances with class {@code clazz} concurrently.
* @param count: The amount of instances.
* @param clazz: BaseTest instance
* @param callback
*/
public static void concurrentRun(int count, Class clazz, Callback callback) {
if (count <= 0 || clazz == null) {
return;
}
List instances = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
T instance = ClassHelper.instance(clazz);
instances.add(instance);
}
concurrentRun(instances, clazz, callback);
}
/**
* Running instances concurrently.
* @param instances: Instance list
* @param clazz: BaseTest instance
* @param callback
*/
public static void concurrentRun(List instances, Class clazz, Callback callback) {
if (instances == null || clazz == null) {
return;
}
AtomicInteger counter = new AtomicInteger(0);
List tasks = new ArrayList<>(instances.size());
for (T instance : instances) {
tasks.add(() -> {
preExecute(clazz);
long startTime = System.currentTimeMillis();
instance.run();
postExecute(startTime, clazz);
counter.addAndGet(1);
});
}
try {
CompletionServiceUtils.run(tasks);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (callback != null) {
callback.execute(counter);
}
}
/**
* Event pre execute
* @param clazz: Class of executing object.
*/
private static void preExecute(Class> clazz) {
System.out.println(String.format("Start executing %s:", clazz.getSimpleName()));
}
/**
* Post execute
* @param startTime: Start time of execution.
* @param clazz: Class of executing object.
*/
private static void postExecute(long startTime, Class> clazz) {
long endTime = System.currentTimeMillis();
String message = String.format("Executing %s end, cost %d millisecond.", clazz.getSimpleName(), (endTime - startTime));
System.out.println(message);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy