0

Context: I am trying to send my performance result to azure cosmos db using post call ,through the end point exposed and can be access by using the master key.

The JSON format of the payload is similarly like,

{
    "id": "Project_Name",
    "test_details": [
        { "test_Name": "APIM_Load_Test" },
        { "test_id": "Load_Test_01" }
    ],
    "response_Details": [
        {
            "requestName": "sampleResult.getSampleLabel()",
            "status": "status",
            "responseTime": "sampleResult.getTime()",
            "responsecode": "${(sampleResult.getResponseCode())}",
            "sentBytes": "sampleResult.getSentBytes()",
            "receivedBytes": "sampleResult.getBytesAsLong()",
            "responseMessage": "${(sampleResult.getResponseMessage())}",
            "samplecount": "sampleResult.getSampleCount()",
            "errorCount": "sampleResult.getErrorCount()",
            "time": "sampleResult.getTimeStamp()"
            ]
        },
    ],
    "thread_Details": { "number_vusers": "tc.activeThreads" }
}

Similarly, as we can do it to influx db by configuring JSR223 listener using groovy script as follow,

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.threads.JMeterContextService;

try {
    def tc = JMeterContextService.getThreadCounts();
    def testStarted = props.get("testStarted");
    def testEnded = props.get("testEnded");
    int aThreads = tc.activeThreads;
    int sThreads = tc.startedThreads;
    int fThreads = tc.finishedThreads;
    if ( testStarted == null && testEnded == null && aThreads >= 1 ){
        Tests = new StringBuilder();
        Tests.append("LoadTest")
            .append(",testName=")
            .append(args[0])
            .append(",testId=")
            .append(args[1])
            .append(",testTriggeredBy=")
            .append(args[2])
            .append(",projectId=")
            .append(args[3])
            .append(" startTest=")
            .append(vars.get("TESTSTART.MS"))
            .append(",status=")
            .append('"started"')
            .append(" ")
            .append(System.currentTimeMillis())
            .append("000000");
        props.put("testStarted","1");
        PostMeasurement(Tests.toString());
    }
    if ( testStarted != null && testEnded == null && fThreads >= 1 ){
        Teste = new StringBuilder();
        Teste.append("LoadTest")
            .append(",testName=")
            .append(args[0])
            .append(",testId=")
            .append(args[1])
            .append(",testTriggeredBy=")
            .append(args[2])
            .append(",projectId=")
            .append(args[3])
            .append(" endTest=")
            .append(System.currentTimeMillis())
            .append(",status=")
            .append('"ended"')
            .append(" ")
            .append(System.currentTimeMillis())
            .append("000000");
        props.put("testEnded","1");
        PostMeasurement(Tests.toString());
    }
    
    Threads = new StringBuilder();
    Threads.append("Threads,")
        .append("testId=")
        .append(vars.get("TestId"))
        .append(" activeThreads=")
        .append(tc.activeThreads)
        .append(" ")
        .append(System.currentTimeMillis())
        .append("000000");
    PostMeasurement(Threads.toString());    
} catch (Exception e) {
    log.error("Error in Grafana Threads Listener : " +e );
}

void PostMeasurement(String Metric) {
    def httpclient = new DefaultHttpClient(new BasicHttpParams());
    def httpPost = new HttpPost();
    httpPost.setURI(new URI(vars.get("InfluxDBAPI")));
    httpPost.setEntity(new StringEntity(Metric));
    HttpResponse response = httpclient.execute(httpPost);
    EntityUtils.consumeQuietly(response.getEntity());
}

enter image description here

0

If you're looking for the way of generating the request payload it can be done using i.e. JSR223 PreProcessor like:

def payload = [:]
payload.put('id', 'Project_Name')
def testDetails = []
testDetails.add(['test_Name': 'APIM_Load_Test'])
testDetails.add(['test_id': 'Load_Test_01'])
payload.put('test_details', testDetails)
def responseDetails = []
responseDetails.add(['requestName'    : ctx.getPreviousResult().getSampleLabel(),
                     'status'         : 'status',
                     'responseTime'   : ctx.getPreviousResult().getTime(),
                     'responsecode'   : ctx.getPreviousResult().getResponseCode(),
                     'sentBytes'      : ctx.getPreviousResult().getSentBytes(),
                     'receivedBytes'  : ctx.getPreviousResult().getBytesAsLong(),
                     'responseMessage': ctx.getPreviousResult().getResponseMessage(),
                     'samplecount'    : ctx.getPreviousResult().getSampleCount(),
                     'errorCount'     : ctx.getPreviousResult().getErrorCount(),
                     'time'           : ctx.getPreviousResult().getTimeStamp()])
payload.put('response_Details', responseDetails)
payload.put('thread_Details', ['number_vusers': ctx.getThreadGroup().getNumberOfThreads()])
log.info(new groovy.json.JsonBuilder(payload).toPrettyString())

The payload can be saved into a JMeter Variable using vars shorthand like:

vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())

so you will be able to refer it as ${vars} in the HTTP Request sampler

enter image description here

More information:

By the way, the easier way of posting test results to InfluxbDB is using Backend Listener

| improve this answer | |
  • thank you so much. – JYOTI PRAKASH MALLICK Jul 30 at 15:53
  • If I have a multiple request under multiple transaction controller. How to use it. For example, after one transaction I want to post the entire payload in that can how I can form that JSON? Please check the screenshot I have attached – JYOTI PRAKASH MALLICK Jul 30 at 16:03
  • Your setup is violating JMeter Scoping Rules, you need to put the JSR223 PreProcessor as a child of the HTTP Request sampler which will be sending the results to the cosmos db and the HTTP Request should be after the request you want to monitor. – Dmitri T Jul 30 at 16:21
  • In case of long hour load simulation test, the JMeter will run out of resource. If I write after every request. How to overcome this situation if I will write once after one complete workflow ran successfully? – JYOTI PRAKASH MALLICK Jul 30 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.