Jmeter?GUI???????
???????????? ???????[ 2015/3/12 14:21:55 ] ?????????????????? ?????????? Jmeter ????
????????GUI??jmx??????
???????????java??????jmeter??????????????????GUI?????????
??????????????jmx????У??????????????????????jmx?б????????????磺
????Sleep_Time????????????jmx????е????????
????<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>
???????У?stringProp????У????? name="Argument.name" ?????Sleep_Time
????stringProp????У????? name="Argument.value" ????????Sleep_Time???
????<stringProp name="Argument.metadata">=</stringProp> ??????????????Sleep_Time??100??????????
????????????????????????????н?????
????????????
/**???????????????????????·????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() + " Construct");
}
/*
* ??????????????????е?????????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() + " setupTest()");
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() + " runTest()" + " Time: " + 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();
}
}
//????????????????????????д?????????? teardownTest????????????????loadrunner?е?end
?????????????????????????????????????????
?????????ε???????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11