Tuesday 30 October 2018

Testing .NET Core with JMeter and Curl


          There are different ways of testing your APIs, I've recently started using 2 new to me and quite useful. Given I come from the Windows world I never had the pleasure to use Curl. Recently I've started using it specifically to test the API with big json files. All we need to do is to install it, it will run within the gitbash. Nowadays we can use HTTP-REPL to discover and test APIs from command line, however Curl can still be useful. An example of a call:

curl -X POST "http://localhost:9000/api/resource" -H "accept: text/plain" -H "header1: headerValue1" -H "header2: headerValue2" -H "Content-Type: application/json" --data-binary @file.json

I think most of the above is pretty self-explanatory.
--data-binary allows to load the file at the current folder, while maintaining the line breaks in json
--data is an option we can use instead --data-binary, it will however strip all the line breaks
We can get the file from the folder one level above our level by using @../file.json instead

Another tool is JMeter. JMeter allows you to run a plan of API tests list response times, put them on a chart, calculate averages etc. The plan can be multi request, we can add authentication against a token end point, and then do an authenticated call to our API and check the summarized timings. Perhaps it looks a bit dated, but it's a pretty powerful tool. It's also free.

To do a quick start you need to:
  • Create a Test Plan
  • Add a Thread Group to the Test Plan (TestPlan -> Add -> Threads (Users) -> Thread Group
    • In Thread Group you can change Thread properties to influence how many threads are sending requests at a time and how long the pause is between the requests. Also set Loop Count to Forver
  • Add HTTP Request to the Thread Group (Thread Group -> Add -> Sampler -> HTTP Request
    • In HTTP Request set the HTTP Method, Path should contain the API endpoint URL, Server Name should contain the ip or the host, in Parameters add the headers
  • Add Listeners to the Thread Group (Thread Group -> Add -> Listener -> Summary Report or Response Time Graph
     
Now you can click play and check the Summary Report and the Time Graph after multiple requests have been processed.


JMeter: https://jmeter.apache.org/index.html
CURL for Windows: https://curl.haxx.se/download.html

No comments:

Post a Comment