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 public class IterationCounter extends AbstractFunction implements Serializable 33 { 34 35 private static final List desc = new LinkedList (); 36 private static final String KEY = "__counter"; 37 38 static { 39 desc.add(JMeterUtils.getResString("iteration_counter_arg_1")); 40 desc.add(JMeterUtils.getResString("function_name_param")); 41 } 42 43 transient private Object [] variables; 44 transient private int[] counter; 45 transient private String key; 47 public IterationCounter() 48 { 49 counter = new int[1]; 50 key=KEY+System.identityHashCode(this); 52 } 53 54 public Object clone() 55 { 56 IterationCounter newCounter = new IterationCounter(); 57 newCounter.counter = counter; 58 return newCounter; 59 } 60 61 64 public synchronized String execute( 65 SampleResult previousResult, 66 Sampler currentSampler) 67 throws InvalidVariableException 68 { 69 counter[0]++; 70 71 JMeterVariables vars = getVariables(); 72 73 boolean perThread = 74 Boolean.valueOf(((CompoundVariable) variables[0]).execute()).booleanValue(); 75 76 String varName = 77 ((CompoundVariable) variables[variables.length - 1]).execute(); 78 String counterString = ""; 79 80 if (perThread) 81 { 82 counterString = vars.get(key); 83 if (null==counterString){ 84 counterString= "1"; 85 } 86 else 87 { 88 counterString = Integer.toString(Integer.parseInt(counterString)+1); 89 } 90 vars.put(key,counterString); 91 92 } 93 else 94 { 95 counterString = String.valueOf(counter[0]); 96 } 97 98 if (varName.length()>0) vars.put(varName, counterString); 99 return counterString; 100 } 101 102 105 public void setParameters(Collection parameters) 106 throws InvalidVariableException 107 { 108 109 variables = parameters.toArray(); 110 111 if (variables.length < 2) 112 { 113 throw new InvalidVariableException("Fewer than 2 parameters"); 114 } 115 } 116 117 120 public String getReferenceKey() 121 { 122 return KEY; 123 } 124 125 128 public List getArgumentDesc() 129 { 130 return desc; 131 } 132 } 133 | Popular Tags |