All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.puresoltechnologies.purifinity.server.common.test.PerformanceTester Maven / Gradle / Ivy

package com.puresoltechnologies.purifinity.server.common.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class PerformanceTester {

	public static  PerformanceTestResult runPerformanceTest(
			int numberOfThreads, final int actionsPerThread,
			final PerformanceTest test) throws InterruptedException {
		ExecutorService executor = Executors
				.newFixedThreadPool(numberOfThreads);

		long start = System.currentTimeMillis();
		List>> futures = new ArrayList<>();
		for (int threadId = 0; threadId < numberOfThreads; threadId++) {
			final int finalThreadId = threadId;
			Future> future = executor
					.submit(new Callable>() {
						@Override
						public Map call() throws Exception {
							Map threadResults = new HashMap<>();
							for (int eventId = 0; eventId < actionsPerThread; eventId++) {
								threadResults.put(eventId,
										test.start(finalThreadId, eventId));
							}
							return threadResults;
						}
					});
			futures.add(future);
		}
		executor.shutdown();
		executor.awaitTermination(60, TimeUnit.SECONDS);
		long stop = System.currentTimeMillis();

		Map> results = new HashMap<>();
		List throwables = new ArrayList<>();
		for (int threadId = 0; threadId < futures.size(); threadId++) {
			try {
				Future> future = futures.get(threadId);
				Map threadResults = future.get();
				results.put(threadId, threadResults);
			} catch (ExecutionException e) {
				throwables.add(e.getCause());
			}
		}
		return new PerformanceTestResult(new Date(start), new Date(stop),
				numberOfThreads, actionsPerThread, results, throwables);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy