DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. A Novel Approach to Load Testing with ExecutorService

A Novel Approach to Load Testing with ExecutorService

Alex Collins user avatar by
Alex Collins
·
Aug. 19, 10 · Interview
Like (0)
Save
Tweet
Share
7.62K Views

Join the DZone community and get the full member experience.

Join For Free

Although 1.5's concurrency improvements have been around for yonks, they provide quite a handy means of parallel programming.

A scenario

Let's say you have some web service that's exposed via an interface called 'UserStorageDAO'. A typical implementation of this would call a web service with your given POJO instance and return true/false on successful storage. A quick and dirty way of seeing how resilient your setup is would be in a heavy usage scenario is to hit that service with as many concurrent calls as might be relevant - say 100.

Using our age-old friend JSPs, we can quickly hack something together using the Java 1.5 java.util.concurrent package that'll take x number of threads and run them in a managed manner; much easier than your normal route. 

Code

// inside a JSP declaration tag (for defining class methods), with java.util.concurrent.* imported...
public UserStorageThread implements Runnable {
private int max;

public UserStorageThread(int max) {
this.max = max;
}

public void run() {
UserStorage storer = new UserStorageImpl();
User user = new User();
// set some random props on user
for (int i = 0; i < this.max; i++) {
storer.storeUser(user);
}
}
}

 Above is just a simple Thread class that'll be executed by our ExecutorService instance. In here  we call our interface for storing with our web service.

Now, let's consider a simple form that has input for the maximum above. The use case is simple: the user enters a maximum number of threads they want executed and we create a limited number of threads, and then set them to run. It's important to note that we don't want to generate N threads where N = maximum as this could overload our server(s). Instead, we should generate a smaller amount, say 10, and hand them the work.

<%
ExecutorService ex = new ExecutorService();
int max = Integer.parseInt(request.getParameter("max"));// null & integer checks omitted for brevity
int countPerThread = (int) max / 10;
for (int i = 0; i < 10; i++) {
ex.submit(new UserStorageThread(countPerThread));// exec thread when it chooses
}

%>

So now we have some simple code that'll create our threads and execute them asynchronously for us. This saves us a lot of hassle with implementation detail. 

What's also great about the ExecutorService is its ability to force shutdown or the Future objects that it returns from the submit/invoke methods. These tell us whether the particular threads have finished or not. Some useful stuff.

You could also extend this JSP to force shutdown should you need to (using shutdownNow()) or even execute Runnables in a collection you already have. 

Alternatives 

If you want a quick and easy web service testing tool for features such as one-click submission to your WS or running a fake instance of a service locally you can use the excellent tool SoapUI which boasts features such as:

  • service simulation
  • functional testing of web services
  • load testing
  • multiple protocol support - not just SOAP
The feature list is extensive and there's an open source version available too.
Load testing Web Service

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Running Databases on Kubernetes
  • Comparing Map.of() and New HashMap() in Java
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: