标签:contex 完全 ber api 英文 ADG stp lin uri
<?xml version="1.0" encoding="UTF-8"?> <jmeterTestPlan version="1.2" properties="2.7" jmeter="2.12 r1636949"> <hashTree> <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true"> <stringProp name="TestPlan.comments"></stringProp> <boolProp name="TestPlan.functional_mode">false</boolProp> <boolProp name="TestPlan.serialize_threadgroups">false</boolProp> <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true"> <collectionProp name="Arguments.arguments"/> </elementProp> <stringProp name="TestPlan.user_define_classpath"></stringProp> </TestPlan> <hashTree> </hashTree> </hashTree> </jmeterTestPlan>
XML Schema,使得我们如果我们真的要完全进行配置文件的操作的话会很有难度,不过我猜也没有人会这么做,因为这也太无聊了。
<hashTree> <JavaSampler guiclass="JavaTestSamplerGui" testclass="JavaSampler" testname="Java请求" enabled="true"> <elementProp name="arguments" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" enabled="true"> <collectionProp name="Arguments.arguments"> <elementProp name="Sleep_Time" elementType="Argument"> <stringProp name="Argument.name">Sleep_Time</stringProp> <stringProp name="Argument.value">100</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="Sleep_Mask" elementType="Argument"> <stringProp name="Argument.name">Sleep_Mask</stringProp> <stringProp name="Argument.value">0xFF</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="Label" elementType="Argument"> <stringProp name="Argument.name">Label</stringProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="ResponseCode" elementType="Argument"> <stringProp name="Argument.name">ResponseCode</stringProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="ResponseMessage" elementType="Argument"> <stringProp name="Argument.name">ResponseMessage</stringProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="Status" elementType="Argument"> <stringProp name="Argument.name">Status</stringProp> <stringProp name="Argument.value">OK</stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="SamplerData" elementType="Argument"> <stringProp name="Argument.name">SamplerData</stringProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> <elementProp name="ResultData" elementType="Argument"> <stringProp name="Argument.name">ResultData</stringProp> <stringProp name="Argument.value"></stringProp> <stringProp name="Argument.metadata">=</stringProp> </elementProp> </collectionProp> </elementProp> <stringProp name="classname">org.apache.jmeter.protocol.java.test.JavaTest</stringProp> </JavaSampler> <hashTree/>
1
2
3
4
5
|
< elementProp name="Sleep_Time" elementType="Argument"> < stringProp name="Argument.name">Sleep_Time</ stringProp > < stringProp name="Argument.value">100</ stringProp > < stringProp name="Argument.metadata">=</ stringProp > </ elementProp > |
/**这个包名,也是这个文件所在的路径,java自带的一个演示文件, 英文好的同学可自行阅读,英文不好的可以和我一起阅读, 互相学习,共同进步,如有错误欢迎指正。 */ package org.apache.jmeter.protocol.java.test; import java.io.Serializable; import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; public class JavaTest extends AbstractJavaSamplerClient implements Serializable { private static final Logger LOG = LoggingManager.getLoggerForClass(); private static final long serialVersionUID = 240L; /**这个变量是用来记录JavaSamplerContext对象context调用getLongParameter方法传递DEFAULT_SLEEP_TIME和SLEEP_NAME运算后的返回值*/ private long sleepTime; /** 下边这个变量的值100就是Sleep_Time的默认值,也是我们在页面中看到的那个100,注意这个值是一个毫秒值. */ public static final long DEFAULT_SLEEP_TIME = 100; /** 这个变量就是用来存储GUI中的那个Sleep_Time的变量。 */ private static final String SLEEP_NAME = "Sleep_Time"; /** 以下的变量名称和GUI中的变量名称都是相同的,不难看懂。 */ private long sleepMask; public static final long DEFAULT_SLEEP_MASK = 0xff; private static final String DEFAULT_MASK_STRING = "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase(java.util.Locale.ENGLISH); private static final String MASK_NAME = "Sleep_Mask"; private String label; private static final String LABEL_NAME = "Label"; private String responseMessage; private static final String RESPONSE_MESSAGE_DEFAULT = ""; private static final String RESPONSE_MESSAGE_NAME = "ResponseMessage"; private String responseCode; private static final String RESPONSE_CODE_DEFAULT = ""; private static final String RESPONSE_CODE_NAME = "ResponseCode"; private String samplerData; private static final String SAMPLER_DATA_DEFAULT = ""; private static final String SAMPLER_DATA_NAME = "SamplerData"; private String resultData; private static final String RESULT_DATA_DEFAULT = ""; private static final String RESULT_DATA_NAME = "ResultData"; private boolean success; private static final String SUCCESS_DEFAULT = "OK"; private static final String SUCCESS_NAME = "Status"; /** * 默认的构造函数,实例化了一个客户端的类 */ public JavaTest() { LOG.debug(whoAmI() + "\tConstruct"); } /* * 这个方法就是设置了所有的值,我们从GUI中输入的值就是由这个方法进行读取并且赋值给上边定义的变量。 */ private void setupValues(JavaSamplerContext context) { sleepTime = context.getLongParameter(SLEEP_NAME, DEFAULT_SLEEP_TIME); sleepMask = context.getLongParameter(MASK_NAME, DEFAULT_SLEEP_MASK); responseMessage = context.getParameter(RESPONSE_MESSAGE_NAME, RESPONSE_MESSAGE_DEFAULT); responseCode = context.getParameter(RESPONSE_CODE_NAME, RESPONSE_CODE_DEFAULT); success = context.getParameter(SUCCESS_NAME, SUCCESS_DEFAULT).equalsIgnoreCase("OK"); label = context.getParameter(LABEL_NAME, ""); if (label.length() == 0) { label = context.getParameter(TestElement.NAME); } samplerData = context.getParameter(SAMPLER_DATA_NAME, SAMPLER_DATA_DEFAULT); resultData = context.getParameter(RESULT_DATA_NAME, RESULT_DATA_DEFAULT); } /** 这是一个初始化方法,可以理解为loadrunner中的init */ @Override public void setupTest(JavaSamplerContext context) { if (LOG.isDebugEnabled()) { LOG.debug(whoAmI() + "\tsetupTest()"); listParameters(context); } } /** 这个方法是一个与生命周期相关的方法 决定了哪些变量参数是否显示到GUI页面上。这个方法类一加载就会运行,运行的顺序排在所有复写的方法中第一。 */ @Override public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument(SLEEP_NAME, String.valueOf(DEFAULT_SLEEP_TIME)); params.addArgument(MASK_NAME, DEFAULT_MASK_STRING); params.addArgument(LABEL_NAME, ""); params.addArgument(RESPONSE_CODE_NAME, RESPONSE_CODE_DEFAULT); params.addArgument(RESPONSE_MESSAGE_NAME, RESPONSE_MESSAGE_DEFAULT); params.addArgument(SUCCESS_NAME, SUCCESS_DEFAULT); params.addArgument(SAMPLER_DATA_NAME, SAMPLER_DATA_DEFAULT); params.addArgument(RESULT_DATA_NAME, SAMPLER_DATA_DEFAULT); return params; } /** 主要运行的代码,相当于Loadrunner中的action */ @Override public SampleResult runTest(JavaSamplerContext context) { setupValues(context); SampleResult results = new SampleResult(); results.setResponseCode(responseCode); results.setResponseMessage(responseMessage); results.setSampleLabel(label); if (samplerData != null && samplerData.length() > 0) { results.setSamplerData(samplerData); } if (resultData != null && resultData.length() > 0) { results.setResponseData(resultData, null); results.setDataType(SampleResult.TEXT); } //这个方法很重要,是标记一个事务的开始,可以理解为loadrunner中的lr_start_transaction results.sampleStart(); long sleep = sleepTime; if (sleepTime > 0 && sleepMask > 0) { long start = System.currentTimeMillis(); sleep = sleepTime + (start % sleepMask); } try { if (sleep > 0) { TimeUnit.MILLISECONDS.sleep(sleep); } results.setSuccessful(success); } catch (InterruptedException e) { LOG.warn("JavaTest: interrupted."); results.setSuccessful(true); } catch (Exception e) { LOG.error("JavaTest: error during sample", e); results.setSuccessful(false); } finally { // 这里就是标记事务的结束,相当于loadrunner中的lr_end_transaction results.sampleEnd(); } if (LOG.isDebugEnabled()) { LOG.debug(whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime()); listParameters(context); } return results; } /** 包含初始化参数的一个调试方法。 */ private void listParameters(JavaSamplerContext context) { Iterator<String> argsIt = context.getParameterNamesIterator(); while (argsIt.hasNext()) { String name = argsIt.next(); LOG.debug(name + "=" + context.getParameter(name)); } } /** 这个就是一个用户自定义的调试方法。 */ private String whoAmI() { StringBuilder sb = new StringBuilder(); sb.append(Thread.currentThread().toString()); sb.append("@"); sb.append(Integer.toHexString(hashCode())); return sb.toString(); } }
更深层次的东西还需进一步阅读源代码,了解原理。
标签:contex 完全 ber api 英文 ADG stp lin uri
原文地址:https://www.cnblogs.com/a00ium/p/10468478.html