In one of my last posts, i created a Spring based rest api project and hosted it on Heroku. If you would like to read about it, please see below posts -
I was working on a similar project at work where I was using Siebel java databean to create a rest api interface into our Siebel application. I wanted to test the performance of this application. This was more of a quick unit test and I decided to do this by running a Java application with multiple threads calling the API at the same time.
I am posting below the sample program to show how I did it. It is a very basic tutorial giving an example of using threads in Java. It also shows how to make HTTP calls from a java application.
First I will setup a class that will make my HTTP call to the Heroku api I setup -
As you can see in the above class, I am implementing the Runnable interface with it. This is the interface that will help me run it as a thread later. All the code that I want to execute in the thread is within the Run method that I am overriding.
Here I am using the HttpsURLConnection class to make a get call to my heroku web app url in the Run method. Another thing I am doing is capturing the time before the call and after it and finding the difference. This will give me an idea of how much time each call took.
Now let me setup the Main class which will run my threads. I want to run around 10 threads making the call to this api end point. So lets do that below in a very dumb looking main class -
I called it dumb as it really doesnt do much. All it does was create 10 threads with instances of my other class. Then i started all of them one after the other.
Below you can see the output of each of the calls. Looks like my api on heroku was responding with 500ms each of the 10 times. Not bad performance at all. Of course it is a very simple API with very little data. However I hope you still found this post useful to see how threads can be created and used in Java. Enjoy!
Comments
Post a Comment