Search code examples
jmeterbeanshell

JMeter create custom .jtl log for each sampler in non-GUI mode


I'm trying to run my testcase in nongui mode.

here is my testplan

testplan

and the beanshell Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.URLDecoder;

import org.apache.jmeter.services.FileServer;

String samplerNumber = "${__threadNum}";
String samplerName = "${__samplerName()}";

if(sampleEvent.getResult() instanceof org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) {
    String request = (sampleEvent.getResult().getSamplerData());
    String response = prev.getResponseDataAsString();
    String url = java.net.URLDecoder.decode(request, "UTF-8");


    f = new FileOutputStream("D:/testScripts/logs/" + samplerName + ".log", true);
    p = new PrintStream(f); 

    p.println("sample time: " + prev.getTime() 
    + " Thread: " + samplerNumber 
    + ", sampler name: " + samplerName 
    + ", system time: " + new Date() 
    + " \nREQUEST: " + url);

    p.close();
    f.close();
}

Basically what it does is to write a .log file for each HTTP-sampler I have and log the thread, the sampler name, the system time and the requested url.

Instead of this i'd love to build a .jtl file which i can later read into a listener (graph for example) and see the results for each sampler itself.

Is there a way to modify the script in a way to accomplish that?


Solution

  • You basically don't need this listener at all, JMeter stores all this information into its .jtl results file by default. If you need to save the URL just add the next line to user.properties file:

    jmeter.save.saveservice.url=true
    

    or pass it via -J command-line argument like:

    jmeter -Jjmeter.save.saveservice.url=true -n -t test.jmx -l result.csv
    

    If you still want to do this from code, it must generate valid CSV which matches saveservice configuration, to wit the following properties:

    # legitimate values: xml, csv, db.  Only xml and csv are currently supported.
    #jmeter.save.saveservice.output_format=csv
    
    
    # true when field should be saved; false otherwise
    
    # assertion_results_failure_message only affects CSV output
    #jmeter.save.saveservice.assertion_results_failure_message=true
    #
    # legitimate values: none, first, all
    #jmeter.save.saveservice.assertion_results=none
    #
    #jmeter.save.saveservice.data_type=true
    #jmeter.save.saveservice.label=true
    #jmeter.save.saveservice.response_code=true
    # response_data is not currently supported for CSV output
    #jmeter.save.saveservice.response_data=false
    # Save ResponseData for failed samples
    #jmeter.save.saveservice.response_data.on_error=false
    #jmeter.save.saveservice.response_message=true
    #jmeter.save.saveservice.successful=true
    #jmeter.save.saveservice.thread_name=true
    #jmeter.save.saveservice.time=true
    #jmeter.save.saveservice.subresults=true
    #jmeter.save.saveservice.assertions=true
    #jmeter.save.saveservice.latency=true
    # Only available with HttpClient4
    #jmeter.save.saveservice.connect_time=true
    #jmeter.save.saveservice.samplerData=false
    #jmeter.save.saveservice.responseHeaders=false
    #jmeter.save.saveservice.requestHeaders=false
    #jmeter.save.saveservice.encoding=false
    #jmeter.save.saveservice.bytes=true
    # Only available with HttpClient4
    #jmeter.save.saveservice.sent_bytes=true
    #jmeter.save.saveservice.url=false
    #jmeter.save.saveservice.filename=false
    #jmeter.save.saveservice.hostname=false
    #jmeter.save.saveservice.thread_counts=true
    #jmeter.save.saveservice.sample_count=false
    #jmeter.save.saveservice.idle_time=true
    
    # Timestamp format - this only affects CSV output files
    # legitimate values: none, ms, or a format suitable for SimpleDateFormat
    #jmeter.save.saveservice.timestamp_format=ms
    

    You might need to generate header line as well which needs to match the columns like

    timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
    1496132228865,336,HTTP Request,200,OK,Thread Group 1-1,text,true,,1576,115,1,1,http://example.com/,336,0,174
    

    Also it would be much better to use JSR223 Listener and Groovy language instead of Beanshell as in this case performance will be higher and resource consumption lower.

    See Apache JMeter Properties Customization Guide to learn more about JMeter properties and ways of setting and overriding them