Skip to content

Instantly share code, notes, and snippets.

@jfuerth
Created January 22, 2013 16:23
Show Gist options
  • Select an option

  • Save jfuerth/4596003 to your computer and use it in GitHub Desktop.

Select an option

Save jfuerth/4596003 to your computer and use it in GitHub Desktop.
Example PerfRunner test that compares file writing performance under a variable number of concurrent threads.
@Test
public void perThread(@Varying(name = "thread", axis = Axis.X, from = 1, to = 10, step = 1) final int threadCount) throws Exception {
// First define the task that needs to be done.
// This task writes a million 'a' characters to a file:
Runnable task = new Runnable() {
@Override
public void run() {
// create temporary file with extension suffix
File file1 = File.createTempFile("PerThreadTest", ".javatemp");
BufferedWriter out = new BufferedWriter(new FileWriter(file1));
int megabyte = 1000000;
for (int i = 1; i < megabyte; i++) {
out.write('a');
}
out.close();
}
};
// Now define the executor service that will execute the above task
ExecutorService exec = Executors.newFixedThreadPool(threadCount);
// Submit the task 10 times (total data written will be 10MB)
for (int i = 0; i < 10; i++) {
exec.submit(task);
}
// Finally, wait for all the submitted tasks to complete (1 hour should be way more than enough!)
exec.awaitTermination(1, TimeUnit.HOURS);
// Free the threads that were created by Executors.newFixedThreadPool(threadCount) above
exec.shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment