JMeter graph showing Java code

I have a class that writes działająco my results to a file like this:

<httpSample t="266" lt="235" ts="1430124707554" s="true" lb="" rc="200" rm="OK" tn=" 1-1" dt="text" by="56440"/>

      

How can I create a graph like here http://www.metaltoad.com/blog/plotting-your-load-test-jmeter using JMeter results?

This is my working function:

public void jMeterTest(){
            // JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();

        //JMeter initialization (properties, log levels, locale, etc)
        JMeterUtils.setJMeterHome("/usr/share/jmeter");
        JMeterUtils.loadJMeterProperties("/usr/share/jmeter/bin/jmeter.properties");

        //JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
        JMeterUtils.initLocale();

        // JMeter Test Plan, basic all u JOrphan HashTree
        HashTree testPlanTree = new HashTree();

        // HTTP Sampler
        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setDomain("192.168.1.5");
        httpSampler.setPort(9206);
        httpSampler.setPath("/site/showing");
        httpSampler.setMethod("GET");

        // Loop Controller
        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.addTestElement(httpSampler);
        loopController.setFirst(true);
        loopController.initialize();

        // Thread Group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(7);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);

        // Test Plan
        TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");

        // Construct Test Plan from previously initialized elements
        testPlanTree.add("testPlan", testPlan);
        testPlanTree.add("loopController", loopController);
        testPlanTree.add("threadGroup", threadGroup);
        testPlanTree.add("httpSampler", httpSampler);

        Summariser summer = null;
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
        if (summariserName.length() > 0) {
            summer = new Summariser(summariserName);
        }


        String home = System.getProperty("user.home");
        String logFile = home + "/.platform/Results.jtf";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);

        testPlanTree.add(testPlanTree.getArray()[0], logger);


        // Run Test Plan
        jmeter.configure(testPlanTree);
        jmeter.run();

    }

      

Thanks for reading!

+3


source to share


1 answer


You can use the Chart library to draw it, for example: HighChart, Chartjs ... to draw a chart from an existing JTL file. (Note that in your code above, you are creating a JTF file, it must be a JTL file instead). In my case, I used CanvasJS to draw the chart Create an ajax call to upload the file:

$.ajax({
    type: "GET",
    url: "/resources/data/report.jtl",
    dataType: "text",
    success: function (data) { processData(data); }
});

      

Function to read data from JTL file:

function processData(allText) {
    var allLinesArray = allText.split('\n');
    if (allLinesArray.length > 0) {
        var dataPoints = [];
        for (var i = 0; i <= allLinesArray.length - 1; i++) {
            var rowData = allLinesArray[i].split(',');
            if(rowData && rowData.length > 1)
                dataPoints.push({ label: rowData[0], y: parseInt(rowData[11]) });
        }
        chart.options.data[0].dataPoints = dataPoints;
        chart.render();
    }
}

      



After that, create a new diagram and render it for viewing:

var chart = new CanvasJS.Chart("chartContainer4", {
    theme: "theme4",
    zoomEnabled:true,
    exportEnabled: true,
    zoomType: "xy",
    animationEnabled: true,
    animationDuration: 100,
    axisX:{
        minimum: 0,
        title: "Execution Time",
    },
    axisY:{
        minimun: 0,
        title: "Number of VUsers"
    },
    title: {
        text: "Threads Over Time"
    },
    data: [
    {
        type: "spline",
        lineThickness: 2,
        dataPoints: []
    }
    ]
});

      

You can refer to Canvasjs for more details . Hope for this help.

+1


source







All Articles