Skip to main content

Load testing with Java: Pound on a resource with multiple threads

Load testing involves multiple threads or processes performing operations against a resource. For example, execute a query against a database server via 50 concurrent threads. The tricky part is waiting for the threads to start up before the commands are actually executed, in order to make sure that the resource is truly getting hammered. Here's an example of how to accomplish this in Java. It is possible to refactor the following code into a utility accepts two parameters: the number of threads and a lambda expression which can be passed as a Runnable object.

final int numThreads = 50;
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
CountDownLatch ready = new CountDownLatch(numThreads);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(numThreads);
for (int j = 0 ; j < numThreads ; j++) {
   executor.execute(() -> {
ready.countDown();
try {
   start.await();
// do something here ...
} catch (InterruptedException ex) {
   Thread.currentThread().interrupt();
} finally {
   done.countDown();
}
   });
}
// wait until everybody is ready
ready.await();

// start
start.countDown();
// wait until everybody is done
done.await();

// clean up the thread pool
executor.shutdownNow();
executor.awaitTermination();

Comments

Popular posts from this blog

Running 2560x1080 on LG 29UM57-P on an old MacBook Pro OSX El Capitan

Yes. A mid-2011 MacBook Pro running OSX El Capitan can drive a 2560x1080 monitor via HDMI. I was not able to get 60 Hz working. I am settling with 53 Hz. I tried many settings before finding something that works. The refresh rate seems fine to me. Maybe the text could be clearer but for a $200 monitor running this resolution, I'll take it. Apple MacBook 2015 retina resolution is 2560 x 1800. Consider buying a better monitor that may literally give you fewer headaches. Install  SwitchResX Follow the instructions for disabling SIP After rebooting, run SwitchResX via System Preferences Select your LG monitor Click Custom Resolutions Click the + icon  Enter these settings  ( source ) Exit SwitchResX and save Reboot Run SwitchResX via System Preferences Select your LG monitor Click Current Resolutions Select the 2560x1080, 53 Hz setting Enable SIP If you discover better settings, please leave a comment.

Terris' Spicy Vegan Lentil Casserole

Lentils with Indian flavors Most of the liquid will be absorbed by the lentils so this dish will be more of a cassole than a soup. This recipe yields 10-12 large bowls. Great for leftovers and lunch-at-work! Credits: I started with this recipe . Estimates: Prep time: 30 mins, depending on whether you have help chopping Cook time: 30 mins Ingredients 1 bay leaf 1 Tbsp salt 2 tsp cumin 2 tsp curry powder 2 tsp roasted paprika 1/2 tsp garam masala 1/2 tsp roasted chipotle powder  4 yellow potatoes 6 oz crimini mushrooms, sliced 1 cup dry lentils 1 fresh jalapeƱo finely chopped 1 large yellow onion 1 pound cherry tomatoes - or add two more tomatoes 1 large whole tomato 3 carrots, chopped 1 lb cauliflower, chopped 1 lb broccoli florets, chopped 6 oz tomato paste 2 Tbsp olive oil 1 lemon, juiced (about 2 tablespoons) 3 cups vegetable broth 1 cup water Instructions Sautee the oil, garlic, and onions for 5 minutes in medium-low heat in pressure cooke