jmeter函数开发实现返回随机字符串
2017-01-22 11:04
405 查看
实现功能:对给定的用逗号分隔的字符串,随机返回其中一个。
代码如下:
Java代码
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.functions;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
import java.util.*;
import java.util.Random;
/**
* Function to return a random string you given
* Pass parameters split by comma
*
* @since 2.10
*/
public class GetRandomStr extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
private static final String KEY = "__getrandomstr"; //$NON-NLS-1$
static {
desc.add(JMeterUtils.getResString("getrandomstr_string")); //$NON-NLS-1$
}
private Object[] values;
Random ra;
int rannum;
public GetRandomStr() {
}
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
String randomString = ""; //$NON-NLS-1$
ra =new Random();
rannum = ra.nextInt(values.length);
try {
randomString = ((CompoundVariable) values[rannum]).execute();
} catch (Exception uee) {
return null;
}
return randomString;
}
/** {@inheritDoc} */
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
//checkParameterCount(parameters, 1);
values = parameters.toArray();
}
/** {@inheritDoc} */
@Override
public String getReferenceKey() {
return KEY;
}
/** {@inheritDoc} */
@Override
public List<String> getArgumentDesc() {
return desc;
}
}
在messages.properties中增加对应的属性:
Java代码
urlencode_string=String to encode in URL encoded chars
<span style="color: #ff0000;">getrandomstr_string=return a random you given,split by comma</span>
use_custom_dns_resolver=Use custom DNS resolver
使用截图:
查看图片附件
代码如下:
Java代码
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.functions;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
import java.util.*;
import java.util.Random;
/**
* Function to return a random string you given
* Pass parameters split by comma
*
* @since 2.10
*/
public class GetRandomStr extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
private static final String KEY = "__getrandomstr"; //$NON-NLS-1$
static {
desc.add(JMeterUtils.getResString("getrandomstr_string")); //$NON-NLS-1$
}
private Object[] values;
Random ra;
int rannum;
public GetRandomStr() {
}
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
String randomString = ""; //$NON-NLS-1$
ra =new Random();
rannum = ra.nextInt(values.length);
try {
randomString = ((CompoundVariable) values[rannum]).execute();
} catch (Exception uee) {
return null;
}
return randomString;
}
/** {@inheritDoc} */
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
//checkParameterCount(parameters, 1);
values = parameters.toArray();
}
/** {@inheritDoc} */
@Override
public String getReferenceKey() {
return KEY;
}
/** {@inheritDoc} */
@Override
public List<String> getArgumentDesc() {
return desc;
}
}
在messages.properties中增加对应的属性:
Java代码
urlencode_string=String to encode in URL encoded chars
<span style="color: #ff0000;">getrandomstr_string=return a random you given,split by comma</span>
use_custom_dns_resolver=Use custom DNS resolver
使用截图:
查看图片附件
相关文章推荐
- php开发笔记(七)用数组函数实现生成随机字符串
- 有一个数组,每次从中间随机取一个,然后放回去,当所有的元素都被取过,返回总共的取的次数。写一个函数实现。复杂度是什么。
- 02微信公众平台 - 实现【快递查询】功能函数,返回一个文本字符串。
- C语言中实现函数返回字符串
- C语言:向函数传递和返回字符串:Strlen函数、Strcpy、Strcat函数函数的实现
- PHP开发小技巧④—两种方法实现获取随机字符串
- 利用java反射调用实现对字符串的解析,成功调入函数,并且返回值
- 实现char * my_strcat(char * dest,char *src)函数. 返回: dest字符串的地址。 功能:将src指向的字符串追加到dest指向字符串的后面
- PHP封装函数实现生成随机的字符串验证码
- //1、有一个字符串开头或结尾含有n个空格(” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。 //要求1:请自己定义一个接口(函数),并实现功能;70分 //要求2:编写
- 写一个函数,实现两个字符串的比较。即实现strcmp函数,s1=s2时返回0,s1!=s2时返回二者第一个不同字符的ASCII值。
- 【C语言】实现char * my_strcat(char * dest,char *src)函数. 返回: dest字符串的地址。 功能:将src指向的字符串追加到dest指向字符串的后面。
- 04微信公众平台 - 实现【翻译】功能函数,返回一个文本字符串。
- 实现 fizzBuzz 函数,参数 num 与返回值的关系如下: 1、如果 num 能同时被 3 和 5 整除,返回字符串 fizzbuzz 2、如果 num 能被 3 整除,返回字符串 fizz 3
- C 语言 实现 字符串 分割 函数(返回"二维字符数组",及分割后的字符数组的长度)
- 01微信公众平台 - 实现【笑话】功能函数,返回一个文本字符串。
- Android腾讯微博开发之随机字符串与签名实现
- 【c语言】实现char * my_strcat(char * dest,char *src)函数. 返回: dest字符串的地址。 功能:将src指向的字符串追加到dest指向字符串的后面
- 返回指针的函数实现:字符串长度大小判断
- 编一个程序,将两个字符串s1和s2比较,返回差值,即实现strcmp函数功能