| 1 | |
package kg.apc.jmeter.functions; |
| 2 | |
|
| 3 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 4 | |
import org.apache.jmeter.functions.AbstractFunction; |
| 5 | |
import org.apache.jmeter.functions.InvalidVariableException; |
| 6 | |
import org.apache.jmeter.samplers.SampleResult; |
| 7 | |
import org.apache.jmeter.samplers.Sampler; |
| 8 | |
import org.apache.jmeter.threads.JMeterVariables; |
| 9 | |
|
| 10 | |
import java.util.Collection; |
| 11 | |
import java.util.LinkedList; |
| 12 | |
import java.util.List; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class DoubleSum extends AbstractFunction { |
| 19 | |
|
| 20 | 1 | private static final List<String> desc = new LinkedList<String>(); |
| 21 | |
private static final String KEY = "__doubleSum"; |
| 22 | |
|
| 23 | |
static { |
| 24 | 1 | desc.add("First double to add"); |
| 25 | 1 | desc.add("Second long to add - further doubles can be summed by adding further arguments"); |
| 26 | 1 | desc.add("Name of variable in which to store the result (optional)"); |
| 27 | 1 | } |
| 28 | |
|
| 29 | |
private Object[] values; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 4 | public DoubleSum() { |
| 35 | 4 | } |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
@Override |
| 41 | |
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) |
| 42 | |
throws InvalidVariableException { |
| 43 | |
|
| 44 | 1 | JMeterVariables vars = getVariables(); |
| 45 | |
|
| 46 | 1 | Double sum = 0D; |
| 47 | 1 | String varName = ((CompoundVariable) values[values.length - 1]).execute().trim(); |
| 48 | |
|
| 49 | 2 | for (int i = 0; i < values.length - 1; i++) { |
| 50 | 1 | sum += Double.parseDouble(((CompoundVariable) values[i]).execute()); |
| 51 | |
} |
| 52 | |
|
| 53 | |
try { |
| 54 | 1 | sum += Double.parseDouble(varName); |
| 55 | 1 | varName = null; |
| 56 | 0 | } catch (NumberFormatException ignored) { |
| 57 | 1 | } |
| 58 | |
|
| 59 | 1 | String totalString = Double.toString(sum); |
| 60 | 1 | if (vars != null && varName != null && varName.length() > 0) { |
| 61 | 0 | vars.put(varName, totalString); |
| 62 | |
} |
| 63 | |
|
| 64 | 1 | return totalString; |
| 65 | |
|
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
| 73 | 2 | checkMinParameterCount(parameters, 2); |
| 74 | 2 | values = parameters.toArray(); |
| 75 | 2 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
@Override |
| 81 | |
public String getReferenceKey() { |
| 82 | 1 | return KEY; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
@Override |
| 89 | |
public List<String> getArgumentDesc() { |
| 90 | 1 | return desc; |
| 91 | |
} |
| 92 | |
} |