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 SwitchResXFollow the instructions for disabling SIPAfter rebooting, run SwitchResX via System PreferencesSelect your LG monitorClick Custom ResolutionsClick the + icon Enter these settings (source)Exit SwitchResX and saveRebootRun SwitchResX via System PreferencesSelect your LG monitorClick Current ResolutionsSelect the 2560x1080, 53 Hz settingEnable SIPIf you discover better settings, please leave a comment.

Collecting and graphing metrics via Apache Camel, Statsd, and Graphite

If you're curious about Apache Camel's metrics reporting via Dropwizard, this guide will help you get Graphite running in a Docker container. Then you will run an instrumented Camel application and view its metrics via Graphite's browser UI.

(1) Install Docker

There are many ways to do this depending on your platform. Google it.

(2) Install and run the Docker container for Statsd and Graphite

Refer to https://github.com/hopsoft/docker-graphite-statsd

The instructions infer that you can run the container without specifying port mappings. This is plain wrong. So use the following command to start the container:

docker run -d --name graphite --restart=always -p 80:80 -p 8125:8125/udp hopsoft/graphite-statsd It is not necessary to enable Graphite's UDP listener. Statsd receives metrics via UDP and forwards them to Graphite over TCP. (3) On Windows and OSX, determine the IP address of the graphite Docker container

On Linux, you can use localhost.

On Windows and OSX, there are…

Logitech Harmony turns your phone into an IoT magic wand for your house

The Logitech Harmony far exceeded my expectations. It's marketed as a "Universal Remote" and, as such, it's one of the best. However, the Harmony is much more, as it works with popular Internet-connected devices.

Product setup is easy via smartphone/tablet. The first step involves get connecting the Harmony hub to wifi. Since the device database is downloaded from the Internet, Harmony works with almost anything that has a remote: cable boxes, TVs, dvd players, stereos, etc.. You may configure up to 8 devices.

Harmony's remote doesn't need line of sight to communicate with its hub. This means you can place the hub in a solid wood cabinet and control it with the remote or your smartphone/tablet. The Harmony hub has a built-in internal IR blaster for old-school devices. Harmony ships with an additional IR blaster with a long wire so it can be placed, for example, outside your cabinet. Additional IR blasters can be purchased separately.

The provided remote is …