nGrinder 每次跑完脚本都可以看到日志文件生成。 然而一个代理里面只能看到一个进程的日志文件,而且这个文件的大小最大只能支持到1MB,如果超过1MB将会覆盖现有日志,这样当你跑的场景时间足够长,生成足够多的日志时这时往往一些出错信息就很容易被刷掉再或者你想从N多个日志信息中立马筛选出错误信息也是相对复杂的工作,这时我们可能需要考虑日志只打印级别为ERROR的,这时你可以添加如下代码:
Groovy:
LoggerFactory.getLogger(“worker”).setLevel(Level.ERROR)
需要import
import ch.qos.logback.classic.Level
import org.slf4j.LoggerFactory
具体例子:
import HTTPClient.HTTPResponse
import ch.qos.logback.classic.Level
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "172.23.81.1")
request = new HTTPRequest()
test.record(request);
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
// 输出错误的日志
LoggerFactory.getLogger("worker").setLevel(Level.ERROR)
grinder.statistics.delayReports = true;
grinder.logger.info("before thread.");
}
@Test
public void test() {
HTTPResponse result = request.GET("http://172.23.81.1:8081/")
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));
}
}
}
Jython:
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from org.slf4j import LoggerFactory
from ch.qos.logback.classic import Level
from ch.qos.logback.classic import Logger
test1 = Test(1, "Test1")
request1 = test1.wrap(HTTPRequest())
class TestRunner:
def __init__(self) :
logger = LoggerFactory.getLogger("worker");
logger.setLevel(Level.ERROR);
def __call__(self):
grinder.statistics.delayReports=True
result = request1.GET(“http://www.baidu.com")
if result.getStatusCode() == 200 :
grinder.statistics.forLastTest.success = 1
else :
grinder.logger.error(result.getStatusCode())
grinder.statistics.forLastTest.success = 0