1 18 19 package org.apache.jmeter.protocol.java.test; 20 21 import java.io.Serializable ; 22 import java.util.Iterator ; 23 24 import org.apache.jmeter.config.Arguments; 25 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 26 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 27 import org.apache.jmeter.samplers.SampleResult; 28 29 62 63 public class JavaTest 64 extends AbstractJavaSamplerClient 65 implements Serializable 66 { 67 68 private long sleepTime; 69 70 71 public static final long DEFAULT_SLEEP_TIME = 100; 72 73 74 private static final String SLEEP_NAME="Sleep_Time"; 75 76 77 81 private long sleepMask; 82 83 84 public static final long DEFAULT_SLEEP_MASK = 0xff; 85 86 87 private static final String DEFAULT_MASK_STRING = 88 "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase(); 89 90 91 private static final String MASK_NAME="Sleep_Mask"; 92 93 94 95 private String label; 96 97 98 private static final String LABEL_DEFAULT = "JavaTest"; 99 100 101 private static final String LABEL_NAME = "Label"; 102 103 104 105 private String responseMessage; 106 107 108 private static final String RESPONSE_MESSAGE_DEFAULT = ""; 109 110 111 private static final String RESPONSE_MESSAGE_NAME = "ResponseMessage"; 112 113 114 115 private String responseCode; 116 117 118 private static final String RESPONSE_CODE_DEFAULT = ""; 119 120 121 private static final String RESPONSE_CODE_NAME = "ResponseCode"; 122 123 124 125 private String samplerData; 126 127 128 private static final String SAMPLER_DATA_DEFAULT = ""; 129 130 131 private static final String SAMPLER_DATA_NAME = "SamplerData"; 132 133 134 135 private String resultData; 136 137 138 private static final String RESULT_DATA_DEFAULT = ""; 139 140 141 private static final String RESULT_DATA_NAME = "ResultData"; 142 143 144 145 private boolean success; 146 147 148 private static final String SUCCESS_DEFAULT = "OK"; 149 150 151 private static final String SUCCESS_NAME = "Status"; 152 153 154 160 public JavaTest() 161 { 162 getLogger().debug(whoAmI() + "\tConstruct"); 163 } 164 165 166 169 private void setupValues(JavaSamplerContext context) 170 { 171 172 sleepTime = context.getLongParameter(SLEEP_NAME, DEFAULT_SLEEP_TIME); 173 sleepMask = context.getLongParameter(MASK_NAME, DEFAULT_SLEEP_MASK); 174 175 responseMessage = 176 context.getParameter( 177 RESPONSE_MESSAGE_NAME, 178 RESPONSE_MESSAGE_DEFAULT); 179 180 responseCode = 181 context.getParameter(RESPONSE_CODE_NAME, RESPONSE_CODE_DEFAULT); 182 183 success = 184 context.getParameter( 185 SUCCESS_NAME, 186 SUCCESS_DEFAULT).equalsIgnoreCase( 187 "OK"); 188 189 label = context.getParameter(LABEL_NAME, LABEL_DEFAULT); 190 191 samplerData = 192 context.getParameter(SAMPLER_DATA_NAME, SAMPLER_DATA_DEFAULT); 193 194 resultData = 195 context.getParameter(RESULT_DATA_NAME, RESULT_DATA_DEFAULT); 196 } 197 206 public void setupTest(JavaSamplerContext context) 207 { 208 getLogger().debug(whoAmI() + "\tsetupTest()"); 209 listParameters(context); 210 } 211 212 213 227 public Arguments getDefaultParameters() 228 { 229 Arguments params = new Arguments(); 230 params.addArgument(SLEEP_NAME, String.valueOf(DEFAULT_SLEEP_TIME)); 231 params.addArgument(MASK_NAME, DEFAULT_MASK_STRING); 232 params.addArgument(LABEL_NAME, LABEL_DEFAULT); 233 params.addArgument(RESPONSE_CODE_NAME, RESPONSE_CODE_DEFAULT); 234 params.addArgument(RESPONSE_MESSAGE_NAME, RESPONSE_MESSAGE_DEFAULT); 235 params.addArgument(SUCCESS_NAME, SUCCESS_DEFAULT); 236 params.addArgument(SAMPLER_DATA_NAME, SAMPLER_DATA_DEFAULT); 237 params.addArgument(RESULT_DATA_NAME, SAMPLER_DATA_DEFAULT); 238 return params; 239 } 240 241 275 public SampleResult runTest(JavaSamplerContext context) 276 { 277 setupValues(context); 278 279 SampleResult results = new SampleResult(); 280 281 results.setResponseCode(responseCode); 282 results.setResponseMessage(responseMessage); 283 results.setSampleLabel(label); 284 285 if (samplerData != null && samplerData.length() > 0) 286 { 287 results.setSamplerData(samplerData); 288 } 289 290 if (resultData != null && resultData.length() > 0) 291 { 292 results.setResponseData(resultData.getBytes()); 293 results.setDataType(SampleResult.TEXT); 294 } 295 296 results.sampleStart(); 298 299 long sleep = sleepTime; 300 if (sleepTime > 0 && sleepMask > 0) 301 { long start = System.currentTimeMillis(); 303 sleep = sleepTime + (start % sleepMask); 305 } 306 307 try 308 { 309 if (sleep > 0) 312 { 313 Thread.sleep(sleep); 314 } 315 results.setSuccessful(success); 316 } 317 catch (InterruptedException e) 318 { 319 getLogger().warn("JavaTest: interrupted."); 320 results.setSuccessful(true); 321 } 322 catch (Exception e) 323 { 324 getLogger().error("JavaTest: error during sample", e); 325 results.setSuccessful(false); 326 } finally{ 327 results.sampleEnd(); 329 } 330 331 if (getLogger().isDebugEnabled()) 332 { 333 getLogger().debug( 334 whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime()); 335 listParameters(context); 336 } 337 338 return results; 339 } 340 341 349 public void teardownTest(JavaSamplerContext context) 350 { 351 getLogger().debug(whoAmI() + "\tteardownTest()"); 352 listParameters(context); 353 } 354 360 private void listParameters(JavaSamplerContext context) 361 { 362 if (getLogger().isDebugEnabled()) 363 { 364 Iterator argsIt = context.getParameterNamesIterator(); 365 while (argsIt.hasNext()) 366 { 367 String name = (String ) argsIt.next(); 368 getLogger().debug(name + "=" + context.getParameter(name)); 369 } 370 } 371 } 372 373 379 private String whoAmI() 380 { 381 StringBuffer sb = new StringBuffer (); 382 sb.append(Thread.currentThread().toString()); 383 sb.append("@"); 384 sb.append(Integer.toHexString(hashCode())); 385 return sb.toString(); 386 } 387 388 } 389 | Popular Tags |