org.apache.jmeter.threads.JMeterVariables.get()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(69)

本文整理了Java中org.apache.jmeter.threads.JMeterVariables.get()方法的一些代码示例,展示了JMeterVariables.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JMeterVariables.get()方法的具体详情如下:
包路径:org.apache.jmeter.threads.JMeterVariables
类名称:JMeterVariables
方法名:get

JMeterVariables.get介绍

[英]Gets the value of a variable, converted to a String.
[中]获取转换为字符串的变量的值。

代码示例

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

private void saveVars(JMeterVariables vars){
  for(int i = 0; i < variableNames.length; i++){
    values[i] = vars.get(variableNames[i]);
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

@Override
public String get(String key) {
  return variables.get(key);
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_functions

/**
 * Get from vars the values of variableName (can be missing, contain 1 value or contain multiple values (_matchNr))
 * and stores them in results
 * @param variableName String
 * @param vars {@link JMeterVariables}
 * @param results {@link List} where results are stored
 */
private void extractVariableValuesToList(String variableName,
    JMeterVariables vars, List<String> results) {
  String matchNumberAsStr = vars.get(variableName+"_matchNr");
  int matchNumber = 0;
  if(!StringUtils.isEmpty(matchNumberAsStr)) {
    matchNumber = Integer.parseInt(matchNumberAsStr);
  }
  if(matchNumber > 0) {
    for (int i = 1; i <= matchNumber; i++) {
      results.add(vars.get(variableName+"_"+i));
    }
  } else {
    String value = vars.get(variableName);
    if(!StringUtils.isEmpty(value)) {
      results.add(value);
    }
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_http

private Map<String, String> buildParamsMap(){
  String regExRefName = getRegExRefName()+"_";
  String grNames = getRegParamNamesGrNr();
  String grValues = getRegExParamValuesGrNr();
  JMeterVariables jmvars = getThreadContext().getVariables();
  // verify if regex groups exists
  if(jmvars.get(regExRefName + MATCH_NR) == null
      || jmvars.get(regExRefName + 1 + REGEX_GROUP_SUFFIX + grNames) == null 
      || jmvars.get(regExRefName + 1 + REGEX_GROUP_SUFFIX + grValues) == null){
    return null;
  }
  int n = Integer.parseInt(jmvars.get(regExRefName + MATCH_NR));
  Map<String, String> map = new HashMap<>(n);
  for(int i=1; i<=n; i++){
    map.put(jmvars.get(regExRefName + i + REGEX_GROUP_SUFFIX + grNames), 
        jmvars.get(regExRefName + i + REGEX_GROUP_SUFFIX + grValues));
  }
  return map;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

/**
 * @see org.apache.jmeter.functions.Function#execute
 */
@Override
public String toString() {
  String ret = null;
  JMeterVariables vars = getVariables();
  if (vars != null) {
    ret = vars.get(name);
  }
  if (ret == null) {
    return "${" + name + "}";
  }
  return ret;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_functions

@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
    throws InvalidVariableException {
  String variableName = values[0].execute();
  JMeterVariables jMeterVariables = getVariables();
  if(jMeterVariables != null) {
    String variableValue = jMeterVariables.get(variableName);
    return Boolean.toString(variableValue != null);
  } else {
    return Boolean.FALSE.toString();
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

private int removePrevCount(JMeterVariables vars, String refName) {
  int prevCount = 0;
  String prevString = vars.get(refName + REF_MATCH_NR);
  if (prevString != null) {
    // ensure old value is not left defined
    vars.remove(refName + REF_MATCH_NR);
    try {
      prevCount = Integer.parseInt(prevString);
    } catch (NumberFormatException nfe) {
      if (log.isWarnEnabled()) {
        log.warn("{}: Could not parse number: '{}'.", getName(), prevString);
      }
    }
  }
  return prevCount;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

private void clearOldRefVars(JMeterVariables vars, String refName) {
  vars.remove(refName + REF_MATCH_NR);
  for (int i=1; vars.get(refName + "_" + i) != null; i++) {
    vars.remove(refName + "_" + i);
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

/**
 * Removes the variables:<br/>
 * basename_gn, where n=0...# of groups<br/>
 * basename_g = number of groups (apart from g0)
 */
private void removeGroups(JMeterVariables vars, String basename) {
  StringBuilder buf = new StringBuilder();
  buf.append(basename);
  buf.append("_g"); // $NON-NLS-1$
  int pfxlen=buf.length();
  // How many groups are there?
  int groups;
  try {
    groups=Integer.parseInt(vars.get(buf.toString()));
  } catch (NumberFormatException e) {
    groups=0;
  }
  vars.remove(buf.toString());// Remove the group count
  for (int i = 0; i <= groups; i++) {
    buf.append(i);
    vars.remove(buf.toString());// remove the g0,g1...gn vars
    buf.setLength(pfxlen);
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

/**
 * Get the next or only alias.
 * 
 * @return the next or only alias.
 * @throws IllegalArgumentException
 *             if {@link JmeterKeyStore#clientCertAliasVarName
 *             clientCertAliasVarName} is not empty and no key for this
 *             alias could be found
 */
public String getAlias() {
  if(!StringUtils.isEmpty(clientCertAliasVarName)) {
    // We return even if result is null
    String aliasName = JMeterContextService.getContext().getVariables().get(clientCertAliasVarName);
    if(StringUtils.isEmpty(aliasName)) {
      log.error("No var called '{}' found", clientCertAliasVarName);
      throw new IllegalArgumentException("No var called '"+clientCertAliasVarName+"' found");
    }
    return aliasName;
  }
  int length = this.names.length;
  if (length == 0) { // i.e. is == null
    return null;
  }
  return this.names[getIndexAndIncrement(length)];
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

/**
 * Evaluate the condition, which can be:
 * blank or LAST = was the last sampler OK?
 * otherwise, evaluate the condition to see if it is not "false"
 * If blank, only evaluate at the end of the loop
 *
 * Must only be called at start and end of loop
 *
 * @param loopEnd - are we at loop end?
 * @return true means end of loop has been reached
 */
private boolean endOfLoop(boolean loopEnd) {
  if(breakLoop) {
    return true;
  }
  String cnd = getCondition().trim();
  log.debug("Condition string: '{}'", cnd);
  boolean res;
  // If blank, only check previous sample when at end of loop
  if ((loopEnd && cnd.isEmpty()) || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$
    JMeterVariables threadVars = JMeterContextService.getContext().getVariables();
    res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// $NON-NLS-1$
  } else {
    // cnd may be null if next() called us
    res = "false".equalsIgnoreCase(cnd);// $NON-NLS-1$
  }
  log.debug("Condition value: '{}'", res);
  return res;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

buf.append("_g"); // $NON-NLS-1$
int pfxlen=buf.length();
String prevString=vars.get(buf.toString());
int previous=0;
if (prevString!=null){

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_functions

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
  String variableName = ((CompoundVariable) values[0]).execute();
  String variableDefault = variableName;
  if (values.length > 1) {
    variableDefault = ((CompoundVariable) values[1]).execute();
  }
  String variableValue = getVariables().get(variableName);
  return variableValue == null ? variableDefault : variableValue;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_functions

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
    throws InvalidVariableException {
  String variableName = ((CompoundVariable) values[0]).execute();
  final JMeterVariables vars = getVariables();
  if (vars == null){
    log.error("Variables have not yet been defined");
    return "**ERROR - see log file**";
  }
  String variableValue = vars.get(variableName);
  CompoundVariable cv = new CompoundVariable(variableValue);
  return cv.execute();
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

private List<String> extractMatches(SampleResult previousResult, JMeterVariables vars, int matchNumber) {
  if (isScopeVariable()) {
    String inputString = vars.get(getVariableName());
    if (inputString == null && log.isWarnEnabled()) {
      log.warn("No variable '{}' found to process by Boundary Extractor '{}', skipping processing",
          getVariableName(), getName());
    }
    return extract(getLeftBoundary(), getRightBoundary(), matchNumber, inputString);
  } else {
    Stream<String> inputs = getSampleList(previousResult).stream().map(this::getInputString);
    return extract(getLeftBoundary(), getRightBoundary(), matchNumber, inputs);
  }
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) {
  log.debug("Regex = '{}'", regex);
  Perl5Matcher matcher = JMeterUtils.getMatcher();
  List<MatchResult> matches = new ArrayList<>();
  int found = 0;
  if (isScopeVariable()){
    String inputString=vars.get(getVariableName());
    if(inputString == null) {
      if (log.isWarnEnabled()) {
        log.warn("No variable '{}' found to process by RegexExtractor '{}', skipping processing",
            getVariableName(), getName());
      }
      return Collections.emptyList();
    }
    matchStrings(matchNumber, matcher, pattern, matches, found,
        inputString);
  } else {
    List<SampleResult> sampleList = getSampleList(result);
    for (SampleResult sr : sampleList) {
      String inputString = getInputString(sr);
      found = matchStrings(matchNumber, matcher, pattern, matches, found,
          inputString);
      if (matchNumber > 0 && found == matchNumber){// no need to process further
        break;
      }
    }
  }
  return matches;
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

List<String> result = new ArrayList<>();
if (isScopeVariable()){
  String inputString=vars.get(getVariableName());
  if (!StringUtils.isEmpty(inputString)) {
    getExtractorImpl().extract(expression, attribute, matchNumber, inputString, result, found, "-1");

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

res.setIdleTime(pauseTime + res.getIdleTime());
res.sampleEnd();
res.setSuccessful(TRUE.equals(JMeterContextService.getContext().getVariables().get(JMeterThread.LAST_SAMPLE_OK)));
res.setResponseMessage(
    TransactionController.NUMBER_OF_SAMPLES_IN_TRANSACTION_PREFIX

代码示例来源:origin: undera/jmeter-plugins

String responseData;
if (getSubject().equals(SUBJECT_VARIABLE)) {
  responseData = vars.get(getSrcVariableName());
} else {
  responseData = previousResult.getResponseDataAsString();
    while (vars.get(this.getVar() + "_" + k) != null) {
      vars.remove(this.getVar() + "_" + k);
      k++;
  vars.put(this.getVar() + "_matchNr", "0");
  int k = 1;
  while (vars.get(this.getVar() + "_" + k) != null) {
    vars.remove(this.getVar() + "_" + k);
    k++;

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_components

private String getStringToCheck(SampleResult response) {
  String toCheck; // The string to check (Url or data)
  // What are we testing against?
  if (isScopeVariable()){
    toCheck = getThreadContext().getVariables().get(getVariableName());
  } else if (isTestFieldResponseData()) {
    toCheck = response.getResponseDataAsString(); // (bug25052)
  } else if (isTestFieldResponseDataAsDocument()) {
    toCheck = Document.getTextFromDocument(response.getResponseData());
  } else if (isTestFieldResponseCode()) {
    toCheck = response.getResponseCode();
  } else if (isTestFieldResponseMessage()) {
    toCheck = response.getResponseMessage();
  } else if (isTestFieldRequestHeaders()) {
    toCheck = response.getRequestHeaders();
  } else if (isTestFieldRequestData()) {
    toCheck = response.getSamplerData();
  } else if (isTestFieldResponseHeaders()) {
    toCheck = response.getResponseHeaders();
  } else { // Assume it is the URL
    toCheck = "";
    final URL url = response.getURL();
    if (url != null){
      toCheck = url.toString();
    }
  }
  return toCheck;
}

相关文章