Load Testing using Postman
Performance measurement using Postman and newman

Load Testing using Postman

Chapter 1

Use Case

As a functional tester, I want to capture the response time of an API (sequential load) so that the performance requirements are met.

Problem

There are free tools available in market (like JMeter) for accomplishing this but time to learn and implement will be a bottleneck. It will be easy if there is a way to leverage the existing test scripts and workflow created in tools as part of functional testing. Hence, we can go with postman where already the APIs are automated with functional validations. This below article is explained for hitting the same API over and over sequentially to assert the API response is not fluctuating.

Postman as Swiss Army Knife

Postman does support many type of testing, but it cannot directly allow developer to access file system as it runs in an isolated environment. Postman collections can be executed using Newman (command line collection runner). Newman gives HTML report which will have all the request, response data and meta data.

newman run Example.postman_collection.json -e TestEnv.postman_environment.json --reporters cli,htmlextra --reporter-htmlextra-title "Performace sample Tests" --reporter-htmlextra-browserTitle "Performace sample Tests" --reporter-htmlextra-titleSize 2

In the above syntax "Example.postman_collection.json" is the collection exported, "TestEnv.postman_environment.json" is the environment collection exported from postman. Other switches are for reporting and Html report Title/Headers and font size. For this use case we need to write the response time alone in the external reports.

For fetching the specific ResponseTime values alone from the Newman execution, we need to run Newman itself as npm module. Note Newman triggers bunch of events during the execution and this can be used to fetch response time alone. Refer this link Newman.run Events for more details.

We can leverage the existing postman collections used for functional testing. In the existing test script in each request add a line of code as below so that output file will have ResponseTime and its corresponding Request Name (in this case, the message in console).

console.log("Your Request Name");

One can add any number of Request in the postman with different data, as per the requirement. When running this, all the given Request will be hit sequentially. Refer the screenshot for postman collection.

No alt text provided for this image

For our problem, we are going to hook the events 'request' and 'console' triggered during the execution. Check the below snippet which will fetch the response time and write it in flat file.

const newman = require('newman'),

fs = require('fs');

 
newman.run({

    collection: require('./Example.postman_collection.json'),

    environment: require('./TestEnv.postman_environment.json'),

    iterationCount:3,

    reporters: 'cli',
 

}).on('request', function (err, args) {

   fs.appendFile('./ResponseTime.txt', args.response.responseTime.toString()+"\t", function (err) {

            if (err) throw err;
 
        });
}).on('console',function(err,args){

   fs.appendFile('./ResponseTime.txt', args.messages.toString()+"\n", function (err) {

            if (err) throw err;

        });

});

The above code need the exported postman collection file, environment file, number of Iterations as inputs. The output file will be as below, but the same can be exported to csv or excel based on the need.

No alt text provided for this image

For further reading you can check this link for detailed steps to write postman execution events in files. Hope this is helpful. DM me if there are any queries.

Pragmatic and insightful. Nice article 👍

To view or add a comment, sign in

More articles by udhaya karthick

  • Naming conventions in Postman scripts

    This weekend I was trying to help one of friend in automating REST APIs. The ask was to start API test automation…

  • Performance Testing in postman - new feature

    This article is about performance testing using postman tool's new feature. Recently postman released a new feature and…

  • Non-Functional tests using Postman

    In this article I am going to suggest solutions for two NFR related problems that in-sprint API testing team may face…

  • Monitors using Postman

    Use Case As a functional tester, in continuous deployment want to check continuously if there are any memory leak and…

    1 Comment

Others also viewed

Explore content categories