View | Details | Raw Unified | Return to bug 59963
Collapse All | Expand All

(-)src/core/org/apache/jmeter/resources/messages.properties (+2 lines)
Lines 784-789 Link Here
784
ramp_up=Ramp-Up Period (in seconds)\:
784
ramp_up=Ramp-Up Period (in seconds)\:
785
random_control_title=Random Controller
785
random_control_title=Random Controller
786
random_order_control_title=Random Order Controller
786
random_order_control_title=Random Order Controller
787
random_multi_result_source_variable=Source Variable(s) (use | as separator)
788
random_multi_result_target_variable=Target Variable
787
random_string_chars_to_use=Chars to use for random string generation
789
random_string_chars_to_use=Chars to use for random string generation
788
random_string_length=Random string length
790
random_string_length=Random string length
789
realm=Realm
791
realm=Realm
(-)src/core/org/apache/jmeter/resources/messages_fr.properties (+2 lines)
Lines 769-774 Link Here
769
ramp_up=Dur\u00E9e de mont\u00E9e en charge (en secondes) \:
769
ramp_up=Dur\u00E9e de mont\u00E9e en charge (en secondes) \:
770
random_control_title=Contr\u00F4leur Al\u00E9atoire
770
random_control_title=Contr\u00F4leur Al\u00E9atoire
771
random_order_control_title=Contr\u00F4leur d'Ordre al\u00E9atoire
771
random_order_control_title=Contr\u00F4leur d'Ordre al\u00E9atoire
772
random_multi_result_source_variable=Variable(s) source (separateur |)
773
random_multi_result_target_variable=Variable cible
772
random_string_chars_to_use=Caract\u00E8res \u00E0 utiliser pour la g\u00E9n\u00E9ration de la cha\u00EEne al\u00E9atoire
774
random_string_chars_to_use=Caract\u00E8res \u00E0 utiliser pour la g\u00E9n\u00E9ration de la cha\u00EEne al\u00E9atoire
773
random_string_length=Longueur de cha\u00EEne al\u00E9atoire
775
random_string_length=Longueur de cha\u00EEne al\u00E9atoire
774
realm=Univers (realm)
776
realm=Univers (realm)
(-)src/functions/org/apache/jmeter/functions/RandomFromMultipleVars.java (+151 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
19
package org.apache.jmeter.functions;
20
21
import java.util.ArrayList;
22
import java.util.Collection;
23
import java.util.LinkedList;
24
import java.util.List;
25
import java.util.concurrent.ThreadLocalRandom;
26
27
import org.apache.commons.lang3.StringUtils;
28
import org.apache.jmeter.engine.util.CompoundVariable;
29
import org.apache.jmeter.samplers.SampleResult;
30
import org.apache.jmeter.samplers.Sampler;
31
import org.apache.jmeter.threads.JMeterVariables;
32
import org.apache.jmeter.util.JMeterUtils;
33
import org.apache.jorphan.logging.LoggingManager;
34
import org.apache.log.Logger;
35
36
/**
37
 * Provides a RandomFromMultiResult function which returns a random element from a multi valued extracted variable.
38
 * Those kind of variable are extracted by:
39
 * - Regular Expression extractor
40
 * - JSON PATH extractor
41
 * - CSS/JQuery extractor
42
 * - XPath Extractor
43
 * 
44
 * @since 3.1
45
 */
46
public class RandomFromMultipleVars extends AbstractFunction {
47
    private static final Logger log = LoggingManager.getLoggerForClass();
48
49
    private static final List<String> desc = new LinkedList<>();
50
    private static final String KEY = "__RandomFromMultipleVars"; //$NON-NLS-1$
51
    private static final String SEPARATOR = "\\|"; //$NON-NLS-1$
52
    static {
53
        desc.add(JMeterUtils.getResString("random_multi_result_source_variable")); //$NON-NLS-1$
54
        desc.add(JMeterUtils.getResString("random_multi_result_target_variable")); //$NON-NLS-1$
55
    }
56
57
    private CompoundVariable variablesNamesSplitBySeparator;
58
    private CompoundVariable varName;
59
60
    /**
61
     * No-arg constructor.
62
     */
63
    public RandomFromMultipleVars() {
64
    }
65
66
    /** {@inheritDoc} */
67
    @Override
68
    public String execute(SampleResult previousResult, Sampler currentSampler)
69
            throws InvalidVariableException {
70
71
        String variablesNamesSplitBySeparatorValue = variablesNamesSplitBySeparator.execute().trim();
72
        JMeterVariables vars = getVariables();
73
        String outputValue = "";
74
        String separator = "";
75
        if (vars != null) { // vars will be null on TestPlan
76
            List<String> results = new ArrayList<>();
77
            String[] variables = variablesNamesSplitBySeparatorValue.split(SEPARATOR);
78
            for (String varName : variables) {
79
                if(!StringUtils.isEmpty(varName)) {
80
                    extractVariableValuesToList(varName, vars, results);
81
                }
82
            }
83
84
            if(results.size() > 0) {
85
                int randomIndex = ThreadLocalRandom.current().nextInt(0, results.size());
86
                outputValue = results.get(randomIndex);                
87
            } else {
88
                if(log.isDebugEnabled()) {
89
                    log.debug("RandomFromMultiResult didn't find <var>_matchNr in variables :'"+variablesNamesSplitBySeparatorValue
90
                            +"' using separator:'"+separator+"', will return empty value");
91
                }
92
            }
93
94
            if (varName != null) {
95
                final String varTrim = varName.execute().trim();
96
                if (varTrim.length() > 0){ 
97
                    vars.put(varTrim, outputValue);
98
                }
99
            }
100
        }    
101
        return outputValue;
102
103
    }
104
105
    /**
106
     * @param variableName String
107
     * @param vars {@link JMeterVariables}
108
     * @param results {@link List} where results are stored
109
     * @throws NumberFormatException
110
     */
111
    private void extractVariableValuesToList(String variableName,
112
            JMeterVariables vars, List<String> results)
113
            throws NumberFormatException {
114
        String matchNumberAsStr = vars.get(variableName+"_matchNr");
115
        if(!StringUtils.isEmpty(matchNumberAsStr)) {
116
            int matchNumber = Integer.parseInt(matchNumberAsStr);
117
            for (int i = 1; i <= matchNumber; i++) {
118
                results.add(vars.get(variableName+"_"+i));
119
            }
120
        } else {
121
            String value = vars.get(variableName);
122
            if(!StringUtils.isEmpty(value)) {
123
                results.add(vars.get(variableName));
124
            }
125
        }
126
    }
127
128
    /** {@inheritDoc} */
129
    @Override
130
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
131
        checkParameterCount(parameters, 1, 2);
132
        Object[] values = parameters.toArray();
133
        variablesNamesSplitBySeparator = (CompoundVariable) values[0];
134
        if (values.length>1){
135
            varName = (CompoundVariable) values[1];
136
        }
137
    }
138
139
    /** {@inheritDoc} */
140
    @Override
141
    public String getReferenceKey() {
142
        return KEY;
143
    }
144
145
    /** {@inheritDoc} */
146
    @Override
147
    public List<String> getArgumentDesc() {
148
        return desc;
149
    }
150
151
}
(-)xdocs/usermanual/functions.xml (+29 lines)
Lines 122-127 Link Here
122
        <tr><td>Calculation</td><td> <a href="#__intSum">intSum</a></td><td>add int numbers</td><td>1.8.1</td></tr>
122
        <tr><td>Calculation</td><td> <a href="#__intSum">intSum</a></td><td>add int numbers</td><td>1.8.1</td></tr>
123
        <tr><td>Calculation</td><td> <a href="#__longSum">longSum</a></td><td>add long numbers</td><td>2.3.2</td></tr>
123
        <tr><td>Calculation</td><td> <a href="#__longSum">longSum</a></td><td>add long numbers</td><td>2.3.2</td></tr>
124
        <tr><td>Calculation</td><td> <a href="#__Random">Random</a></td><td>generate a random number</td><td>1.9</td></tr>
124
        <tr><td>Calculation</td><td> <a href="#__Random">Random</a></td><td>generate a random number</td><td>1.9</td></tr>
125
        <tr><td>Calculation</td><td> <a href="#__RandomFromMultipleVars">RandomFromMultipleVars</a></td><td>extracts an element from the values of a set of variables separated by <code>|</code></td><td>3.1</td></tr>
125
        <tr><td>Calculation</td><td> <a href="#__RandomString">RandomString</a></td><td>generate a random string</td><td>2.6</td></tr>
126
        <tr><td>Calculation</td><td> <a href="#__RandomString">RandomString</a></td><td>generate a random string</td><td>2.6</td></tr>
126
        <tr><td>Calculation</td><td> <a href="#__UUID">UUID</a></td><td>generate a random type 4 UUID</td><td>2.9</td></tr>
127
        <tr><td>Calculation</td><td> <a href="#__UUID">UUID</a></td><td>generate a random type 4 UUID</td><td>2.9</td></tr>
127
        <tr><td>Scripting</td><td> <a href="#__BeanShell">BeanShell</a></td><td>run a BeanShell script</td><td>1.X</td></tr>
128
        <tr><td>Scripting</td><td> <a href="#__BeanShell">BeanShell</a></td><td>run a BeanShell script</td><td>1.X</td></tr>
Lines 648-653 Link Here
648
</p>
649
</p>
649
</component>
650
</component>
650
651
652
<component index="&sect-num;.5.8" name="__RandomFromMultipleVars">
653
<description><p>The RandomFromMultipleVars function returns a random value based on the variable values provided by <code>Source Variables</code>.</p>
654
The variables can be simple or multi-valued as they can be generated by the following extractors:
655
<ul>
656
    <li><a href="component_reference.html#Regular_Expression_Extractor">Regular Expression Extractor</a></li>
657
    <li><a href="component_reference.html#CSS/JQuery_Extractor">CSS/JQuery Extractor</a></li>
658
    <li><a href="component_reference.html#JSON_Path_PostProcessor">JSON Path PostProcessor</a></li>
659
    <li><a href="component_reference.html#XPath_Assertion">XPath Assertion</a></li>
660
</ul>
661
662
Multi-value vars are the ones that are extracted when you set <code>-1</code> for <code>Match Numbers</code>. 
663
This leads to creation of match number variable called <code>varName_matchNr</code> and for each value to the creation of variable <code>varName_n</code> where n = 1, 2, 3 etc.
664
665
</description>
666
667
<properties>
668
        <property name="Source Variables" required="Yes">Variable names separated by <code>|</code> that contain the values that will be used as input for random computation</property>
669
        <property name="Variable Name" required="No">A reference name for reusing the value
670
                computed by this function.</property>
671
</properties>
672
<p>Examples:
673
<source>${__RandomFromMultipleVars(val)}</source> will return a random string based on content of variable val taking into account wether they are multi-value or not<br/>
674
<source>${__RandomFromMultipleVars(val1|val2)}</source> will return a random string based on content of variables val1 and val2 taking into account wether they are multi-value or not<br/>
675
<source>${__RandomFromMultipleVars(val1|val2, MYVAR)}</source> will return a random string based on content of variables val1 and val2 taking into account wether they are multi-value or not and store the result in <code>MYVAR</code><br/>
676
</p>
677
</component>
678
679
651
<component index="&sect-num;.5.8" name="__UUID">
680
<component index="&sect-num;.5.8" name="__UUID">
652
<description>
681
<description>
653
<p>The UUID function returns a pseudo random type 4 Universally Unique IDentifier (UUID).</p>
682
<p>The UUID function returns a pseudo random type 4 Universally Unique IDentifier (UUID).</p>

Return to bug 59963