1 18 package org.apache.jmeter.protocol.java.test; 19 20 import java.io.Serializable ; 21 import java.util.Iterator ; 22 23 import org.apache.jmeter.config.Arguments; 24 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 25 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 26 import org.apache.jmeter.samplers.SampleResult; 27 28 45 public class SleepTest 46 extends AbstractJavaSamplerClient 47 implements Serializable 48 { 49 52 public static final long DEFAULT_SLEEP_TIME = 1000; 53 54 57 public static final long DEFAULT_SLEEP_MASK = 0x3ff; 58 59 62 private long sleepTime; 63 64 68 private long sleepMask; 69 70 76 public SleepTest() 77 { 78 getLogger().debug(whoAmI() + "\tConstruct"); 79 } 80 81 92 public void setupTest(JavaSamplerContext context) 93 { 94 getLogger().debug(whoAmI() + "\tsetupTest()"); 95 listParameters(context); 96 97 sleepTime = context.getLongParameter("SleepTime", DEFAULT_SLEEP_TIME); 98 sleepMask = context.getLongParameter("SleepMask", DEFAULT_SLEEP_MASK); 99 } 100 101 124 public SampleResult runTest(JavaSamplerContext context) 125 { 126 SampleResult results = new SampleResult(); 127 128 try 129 { 130 results.sampleStart(); 132 133 long start = System.currentTimeMillis(); 135 long sleep = getSleepTime() + (start % getSleepMask()); 136 137 results.setSampleLabel( 138 "Sleep Test: time = " + sleep); 139 140 Thread.sleep(sleep); 143 144 results.setSuccessful(true); 145 } 146 catch (InterruptedException e) 147 { 148 getLogger().warn("SleepTest: interrupted."); 149 results.setSuccessful(true); 150 } 151 catch (Exception e) 152 { 153 getLogger().error("SleepTest: error during sample", e); 154 results.setSuccessful(false); 155 } 156 finally 157 { 158 results.sampleEnd(); 159 } 160 161 if (getLogger().isDebugEnabled()) 162 { 163 getLogger().debug( 164 whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime()); 165 listParameters(context); 166 } 167 168 return results; 169 } 170 171 179 public void teardownTest(JavaSamplerContext context) 180 { 181 getLogger().debug(whoAmI() + "\tteardownTest()"); 182 listParameters(context); 183 } 184 185 199 public Arguments getDefaultParameters() 200 { 201 Arguments params = new Arguments(); 202 params.addArgument("SleepTime", String.valueOf(DEFAULT_SLEEP_TIME)); 203 params.addArgument( 204 "SleepMask", 205 "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase()); 206 return params; 207 } 208 209 215 private void listParameters(JavaSamplerContext context) 216 { 217 if (getLogger().isDebugEnabled()) 218 { 219 Iterator argsIt = context.getParameterNamesIterator(); 220 while (argsIt.hasNext()) 221 { 222 String name = (String ) argsIt.next(); 223 getLogger().debug(name + "=" + context.getParameter(name)); 224 } 225 } 226 } 227 228 234 private String whoAmI() 235 { 236 StringBuffer sb = new StringBuffer (); 237 sb.append(Thread.currentThread().toString()); 238 sb.append("@"); 239 sb.append(Integer.toHexString(hashCode())); 240 return sb.toString(); 241 } 242 243 249 private long getSleepTime() 250 { 251 return sleepTime; 252 } 253 254 260 private long getSleepMask() 261 { 262 return sleepMask; 263 } 264 } 265 | Popular Tags |