Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

User defined statistic in nGrinder

Maybe you know that "The Grinder" supports user defined statistic. If you define and put some value in the script, then this filed can be saved in data log or summary data. You can check the example script for this from:

But in ngrinder, in previous 3.1.2 version, you can not see that data in test report. But from version 3.1.2, you can see that data in report chart.

The sample script is below:


# -*- coding:utf-8 -*-

# A simple example using the HTTP plugin that shows the retrieval of a
# single page via HTTP. 
# This script is auto generated by ngrinder.
# @author admin
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from net.grinder.plugin.http import HTTPPluginControl
from java.util import Random

control = HTTPPluginControl.getConnectionDefaults()
# if you don't want that HTTPRequest follows the redirection, please modify the following option 0.
# control.followRedirects = 1
# if you want to increase the timeout, please modify the following option.
control.timeout = 6000

test1 = Test(1, "Test1")
request1 = HTTPRequest()

#grinder.statistics.registerDataLogExpression("Delivery time", "userLong0")
grinder.statistics.registerSummaryExpression("User_defined", "(/ userLong0(+ (count timedTests)))")
# Make any method call on request1 increase TPS
random = Random()

class TestRunner:
	# initlialize a thread 
	def __init__(self):

	# test method		
	def __call__(self):
		result = request1.GET("")
		# do something
		deliveryTime = random.nextInt(1000)
		grinder.sleep(deliveryTime)"deliveryTime: %d" % deliveryTime)
		grinder.statistics.forLastTest.setLong("userLong0", deliveryTime)
		if result.getStatusCode() == 200 :
			grinder.statistics.forLastTest.success = 1
		elif result.getStatusCode() in (301, 302) :
			grinder.logger.warn("Warning. The response may not be correct. The response code was %d." %  result.getStatusCode()) 
			grinder.statistics.forLastTest.success = 1
		else :
			grinder.statistics.forLastTest.success = 0


class Test1 {

	public static GTest test;
	public static HTTPRequest request;

	public static void beforeClass() {
		test = new GTest(1, "aa000000");
		// Register a custom statistics
		grinder.statistics.registerSummaryExpression("User_defined", "(/ userLong0(+ (count timedTests)))")
		request = new HTTPRequest();
		test.record(request);"before process.");
	Random random = new Random();
	public void beforeThread() {
		grinder.statistics.delayReports=true;"before thread.");

	public void test(){
		HTTPResponse result = request.GET("");
		def deliveryTime = random.nextInt(1000)
		grinder.sleep(deliveryTime)"deliveryTime: ${deliveryTime}")
		// Update statistics
		grinder.statistics.forLastTest.setLong("userLong0", deliveryTime)
		if (result.statusCode == 301 || result.statusCode == 302) {
			grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
		} else {
			assertThat(result.statusCode, is(200));

Currently, only one chart can be displayed for the user defined statistic, and the name of this statistic should be “User_defined”. For more detailed description of usage, you can refer the document of grinder: registerSummaryExpression. After running a test with this script, you can see the chart for user defined statistics in the detailed report.


comments powered by Disqus
Page info
viewed 1315 times
translations en
posted last year by
updated 11 months ago by
View revisions
Share this article