1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.jmeter.engine.util.CompoundVariable; 27 import org.apache.jmeter.samplers.SampleResult; 28 import org.apache.jmeter.samplers.Sampler; 29 import org.apache.jmeter.threads.JMeterVariables; 30 import org.apache.jmeter.util.JMeterUtils; 31 32 39 public class Random extends AbstractFunction implements Serializable 40 { 41 42 private static final List desc = new LinkedList (); 43 private static final String KEY = "__Random"; 44 45 static { 46 desc.add(JMeterUtils.getResString("minimum_param")); 47 desc.add(JMeterUtils.getResString("maximum_param")); 48 desc.add(JMeterUtils.getResString("function_name_param")); 49 } 50 51 private transient CompoundVariable varName, minimum, maximum; 52 53 56 public Random() 57 { 58 } 59 60 65 public Object clone() 66 { 67 Random newRandom = new Random(); 68 return newRandom; 69 } 70 71 76 public synchronized String execute( 77 SampleResult previousResult, 78 Sampler currentSampler) 79 throws InvalidVariableException 80 { 81 82 JMeterVariables vars = getVariables(); 83 84 int min = Integer.parseInt(minimum.execute().trim()); 85 int max = Integer.parseInt(maximum.execute().trim()); 86 87 int rand = (int) Math.round(min + Math.random() * (max - min)); 88 89 String randString = Integer.toString(rand); 90 vars.put(varName.execute(), randString); 91 92 return randString; 93 94 } 95 96 101 public synchronized void setParameters(Collection parameters) 102 throws InvalidVariableException 103 { 104 Object [] values = parameters.toArray(); 105 106 if (values.length < 3) 107 { 108 throw new InvalidVariableException(); 109 } 110 else 111 { 112 varName = (CompoundVariable) values[2]; 113 minimum = (CompoundVariable) values[0]; 114 maximum = (CompoundVariable) values[1]; 115 } 116 117 } 118 119 124 public String getReferenceKey() 125 { 126 return KEY; 127 } 128 129 134 public List getArgumentDesc() 135 { 136 return desc; 137 } 138 139 } 140 | Popular Tags |