mnaufaldillah

Random Jmeter

Oct 23rd, 2021
581
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. package org.apache.jmeter.functions;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.concurrent.ThreadLocalRandom;
  24.  
  25. import org.apache.jmeter.engine.util.CompoundVariable;
  26. import org.apache.jmeter.samplers.SampleResult;
  27. import org.apache.jmeter.samplers.Sampler;
  28. import org.apache.jmeter.threads.JMeterVariables;
  29. import org.apache.jmeter.util.JMeterUtils;
  30.  
  31. /**
  32.  * Provides a Random function which returns a random long integer between a min
  33.  * (first argument) and a max (second argument).
  34.  * @since 1.9
  35.  */
  36. public class Random extends AbstractFunction {
  37.  
  38.     private static final List<String> desc = new ArrayList<>();
  39.     private static final String KEY = "__Random"; //$NON-NLS-1$
  40.  
  41.     static {
  42.         desc.add(JMeterUtils.getResString("minimum_param")); //$NON-NLS-1$
  43.         desc.add(JMeterUtils.getResString("maximum_param")); //$NON-NLS-1$
  44.         desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
  45.     }
  46.  
  47.     private CompoundVariable varName;
  48.     private CompoundVariable minimum;
  49.     private CompoundVariable maximum;
  50.  
  51.     /**
  52.      * No-arg constructor.
  53.      */
  54.     public Random() {
  55.     }
  56.  
  57.     /** {@inheritDoc} */
  58.     @Override
  59.     public String execute(SampleResult previousResult, Sampler currentSampler)
  60.             throws InvalidVariableException {
  61.  
  62.  
  63.         long min = Long.parseLong(minimum.execute().trim());
  64.         long max = Long.parseLong(maximum.execute().trim());
  65.  
  66.         long rand = ThreadLocalRandom.current().nextLong(min, max+1);
  67.  
  68.         String randString = Long.toString(rand);
  69.  
  70.         if (varName != null) {
  71.             JMeterVariables vars = getVariables();
  72.             final String varTrim = varName.execute().trim();
  73.             if (vars != null && varTrim.length() > 0){// vars will be null on TestPlan
  74.                 vars.put(varTrim, randString);
  75.             }
  76.         }
  77.  
  78.         return randString;
  79.  
  80.     }
  81.  
  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
  85.         checkParameterCount(parameters, 2, 3);
  86.         Object[] values = parameters.toArray();
  87.  
  88.         minimum = (CompoundVariable) values[0];
  89.         maximum = (CompoundVariable) values[1];
  90.         if (values.length>2){
  91.             varName = (CompoundVariable) values[2];
  92.         } else {
  93.             varName = null;
  94.         }
  95.  
  96.     }
  97.  
  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public String getReferenceKey() {
  101.         return KEY;
  102.     }
  103.  
  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public List<String> getArgumentDesc() {
  107.         return desc;
  108.     }
  109.  
  110. }
RAW Paste Data