Advertisement
mnaufaldillah

RandomString Jmeter

Oct 23rd, 2021
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  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.  
  24. import org.apache.commons.lang3.RandomStringUtils;
  25. import org.apache.commons.lang3.StringUtils;
  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. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33.  
  34. /**
  35.  * Provides a RandomString function which returns a random String of length (first argument)
  36.  * using characters (second argument)
  37.  * @since 2.6
  38.  */
  39. public class RandomString extends AbstractFunction {
  40.     private static final Logger log = LoggerFactory.getLogger(RandomString.class);
  41.  
  42.     private static final List<String> desc = new ArrayList<>();
  43.  
  44.     private static final String KEY = "__RandomString"; //$NON-NLS-1$
  45.  
  46.     static {
  47.         desc.add(JMeterUtils.getResString("random_string_length")); //$NON-NLS-1$
  48.         desc.add(JMeterUtils.getResString("random_string_chars_to_use")); //$NON-NLS-1$
  49.         desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
  50.     }
  51.  
  52.     private CompoundVariable[] values;
  53.  
  54.     private static final int MAX_PARAM_COUNT = 3;
  55.  
  56.     private static final int MIN_PARAM_COUNT = 1;
  57.  
  58.     private static final int CHARS = 2;
  59.  
  60.     private static final int PARAM_NAME = 3;
  61.  
  62.     /**
  63.      * No-arg constructor.
  64.      */
  65.     public RandomString() {
  66.         super();
  67.     }
  68.  
  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public String execute(SampleResult previousResult, Sampler currentSampler)
  72.             throws InvalidVariableException {
  73.  
  74.         int length = Integer.parseInt(values[0].execute());
  75.  
  76.         String charsToUse = null;//means no restriction
  77.         if (values.length >= CHARS) {
  78.             charsToUse = values[CHARS - 1].execute().trim();
  79.             if (charsToUse.length() <= 0) { // empty chars, return to null
  80.                 charsToUse = null;
  81.             }
  82.         }
  83.  
  84.         String myName = "";//$NON-NLS-1$
  85.         if (values.length >= PARAM_NAME) {
  86.             myName = values[PARAM_NAME - 1].execute().trim();
  87.         }
  88.  
  89.         String myValue = null;
  90.         if(StringUtils.isEmpty(charsToUse)) {
  91.             myValue = RandomStringUtils.random(length);
  92.         } else {
  93.             myValue = RandomStringUtils.random(length, charsToUse);
  94.         }
  95.  
  96.         if (myName.length() > 0) {
  97.             JMeterVariables vars = getVariables();
  98.             if (vars != null) {// Can be null if called from Config item testEnded() method
  99.                 vars.put(myName, myValue);
  100.             }
  101.         }
  102.  
  103.         if (log.isDebugEnabled()) {
  104.             log.debug("{} name:{} value:{}", Thread.currentThread().getName(), myName, myValue); //$NON-NLS-1$
  105.         }
  106.  
  107.         return myValue;
  108.     }
  109.  
  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
  113.         checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT);
  114.         values = parameters.toArray(new CompoundVariable[parameters.size()]);
  115.     }
  116.  
  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public String getReferenceKey() {
  120.         return KEY;
  121.     }
  122.  
  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public List<String> getArgumentDesc() {
  126.         return desc;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement