先看个典型的grinder脚本
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
test1 = Test(1, "Request resource")
request1 = test1.wrap(HTTPRequest())
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
test1 = Test(1, "Request resource")
request1 = test1.wrap(HTTPRequest())
class TestRunner:
def __call__(self):
result = request1.GET("http://www.google.com.hk)
def __call__(self):
result = request1.GET("http://www.google.com.hk)
脚本解析的入口:Test 类。
1. 解析脚本:test1 = Test(1, "Request resource")
// number=1,description="Request resource"
public class Test extends AbstractTestSemantics implements Serializable{
public Test( int number, String description) {
m_number = number;
m_description = description;
//Test类在初始化的时候,向registeredTest注册该测试对象Test
m_registeredTest = Grinder.grinder .getTestRegistry().register(this);
}
class TestRegistryImplementation
public RegisteredTest register(Test test) {
//先判断instrumenter是否为空
if ( m_instrumenter == null) {
throw new AssertionError("Instrumenter not set");
}
// 创建测试业务实现对象, TestData可以包装关联被测目标对象 /方法,同时实现计时功能
final TestData newTestData;
//同步信息,从testMap中查询是否存在这个test,如果存在就直接返回。保证test在testMap中的唯一性。
synchronized ( this) {
final TestData existing = m_testMap .get(test);
if (existing != null) {
return existing;
}
//如果不存在,new 一个TestData对象。
newTestData = new TestData(m_threadContextLocator ,
m_statisticsSetFactory,
m_testStatisticsHelper,
m_timeAuthority,
m_instrumenter,
test);
// 把test和newTestData放入testMap 中进行管理。
m_testMap.put(test, newTestData);
m_testStatisticsMap.put(test, newTestData.getTestStatistics());
if (m_newTests == null) {
m_newTests = new ArrayList<Test>();
}
// To avoid many minor console updates we store a collection of
// the new tests which is periodically read and sent to the
// console by the scheduled reporter task.
//Collection<Test> 中新增这个test
m_newTests.add(test);