如何在Grinder中添加自定义统计信息

时间:2014-05-04 05:33:05

标签: jython grinder

在Grinder中,我们想添加自定义统计信息

grinder.statistics.registerSummaryExpression("connTimeout", "userLong0")

grinder.statistics.forCurrentTest.addLong("userLong0", 1)

它似乎很成功,因为我们可以在Grinder out文件中获得自定义字段 问题是该统计数据的值始终为0

这是Jython

实现的完整脚本
# -*- coding: utf-8 -*-

from net.grinder.script.Grinder import grinder
from net.grinder.script import Test

from com.netease.cloud.ndir.performance import Query
from com.netease.cloud.ndir.performance import QueryReturnCode

def writeToFile(text):
    filename = "response.log"
    file = open(filename, "a")
    file.write(str(text) + "\n")
    file.close() 

ndir_client = grinder.getProperties().getProperty("ndirClient")
query_file = grinder.getProperties().getProperty("queryFile")

request = Query("grinder.properties", query_file)

grinder.statistics.registerSummaryExpression("connTimeout", "userLong0")
grinder.statistics.registerSummaryExpression("readTimeout", "userLong1")
grinder.statistics.registerSummaryExpression("code!=200", "userLong2")
grinder.statistics.registerSummaryExpression("docs=[]", "userLong3")
grinder.statistics.registerSummaryExpression("unknown", "userLong4")


class TestRunner:

    def __init__(self):
        grinder.statistics.delayReports=True

    def initialSleep(self):
        sleepTime = grinder.threadNumber * 20  # per thread
        grinder.sleep(sleepTime, 0)

    def query(self):
        if ndir_client == "true":
            query = request.randomQueryByNdirClient
        else:
            query = request.randomQueryByHttpGet
        try:
            result = query()
        except:
            writeToFile("exception")
            grinder.statistics.forCurrentTest.addLong("userLong4", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            return
        if result == 0:
            grinder.getStatistics().getForCurrentTest().setSuccess(True)
            return
        elif result == 1:
            grinder.statistics.forCurrentTest.addLong("userLong0", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            return
        elif result == 2:
            grinder.statistics.forCurrentTest.addLong("userLong1", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            return
        elif result == 3:
            grinder.statistics.forCurrentTest.addLong("userLong2", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            return          
        elif result == 4:
            grinder.statistics.forCurrentTest.addLong("userLong3", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(True)    
            return
        else:
            grinder.statistics.forCurrentTest.addLong("userLong4", 1)
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            return

    request = Test(120, 'query').wrap(query)

    def __call__(self):
        if grinder.runNumber == 0: 
            self.initialSleep()
        self.request(self)

1 个答案:

答案 0 :(得分:4)

我怀疑问题是您将测试标记为失败,但期望统计信息显示在摘要中。只有成功的测试才会累积到摘要统计中。

尝试注册数据日志表达式

grinder.statistics.registerDataLogExpression("connTimeout", "userLong0")
grinder.statistics.registerDataLogExpression("readTimeout", "userLong1")
grinder.statistics.registerDataLogExpression("code!=200", "userLong2")
grinder.statistics.registerDataLogExpression("docs=[]", "userLong3")
grinder.statistics.registerDataLogExpression("unknown", "userLong4")

然后,您至少会看到工作进程的数据日志文件中的值。