RPC接口性能压测
/**
* @author zhangyuexu
* @date 2020-07-20 21:00
*/
import com.xueqiu.snowflake.usercenter.thrift.java.UcBlocks;
import com.xueqiu.snowflake.usercenter.UsercenterClient;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.config.Arguments;
import java.io.*;
import java.util.*;
import com.twitter.util.Await;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UcTest extends AbstractJavaSamplerClient implements Serializable {
private static final Logger logger = LoggerFactory.getLogger(AbstractJavaSamplerClient.class);
private SampleResult result;
// private UsercenterClient userCenterClient;
private static final UsercenterClient.JavaClient userCenterClient = new UsercenterClient.JavaClient("10.10.54.41:7950");
private static final List<String> uidList = readFileData("block_uid.csv","/home/op/QA/jmeter/apache-jmeter-5.2.1/data","utf-8");
// private static final List<String> uidList = readFileData("block_uid.csv","/Users/xueqiu/Downloads","utf-8");
private static Random random = new Random();
private String uidStr;
private List<Long> randList;
/**
* 自定义java方法入参的, 设置可用参数及默认值
* @return
*/
@Override
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("name", "");
params.addArgument("tag", "");
return params;
}
/**
* 每个线程测试前执行一次,做一些初始化工作;
* @param context
*/
@Override
public void setupTest(JavaSamplerContext context) {
int n = random.nextInt(uidList.size());
// ("随机的n="+n);
uidStr = uidList.get(n);
List<List<Long>> lists = new ArrayList<List<Long>>();
List<Long> randList1 = getRanList(uidList,5);
List<Long> randList2 = getRanList(uidList,10);
List<Long> randList3 = getRanList(uidList,15);
List<Long> randList4 = getRanList(uidList,20);
lists.add(randList1);
lists.add(randList2);
lists.add(randList3);
lists.add(randList4);
//每次从lists里随机取一个list元素
randList = lists.get(random.nextInt(lists.size()));
}
/**
* 开始测试,从args参数可以获得参数值;
* @param args
* @return
*/
public SampleResult runTest(JavaSamplerContext args) {
// String name = ("name");
// String tag = ("tag");
try {
result = new SampleResult();
result.sampleStart();
//保证每次只请求一次接口
List<UcBlocks> ucBlocksList = Await.result(userCenterClient.ucBlocksService()
.mGetBlockingAndBlockedTime(Long.parseLong(uidStr), randList));
// ("ucBlocksList内容:"+());
if(ucBlocksList.size() > 0 ){
// ("请求成功");
//请求成功,设置测试结果为成功
result.setSuccessful(true);
}else {
//请求失败,设置测试结果为失败
// ("请求失败");
logger.info("请求失败");
result.setSuccessful(false);
}
} catch (Exception e) {
// ("请求异常");
logger.info("请求失败");
result.setSuccessful(false);
e.printStackTrace();
} finally {
result.sampleEnd();
}
return result;
}
@Override
public void teardownTest(JavaSamplerContext context) {
logger.info(getClass().getName() + ": teardownTest");
// (context);
}
public List<Long> getRanList(List<String> lst,int m){
List<Long> randomList = new ArrayList<Long>();
Collections.shuffle(lst);
List<String> randomSeries = lst.subList(0, m);
// ("随机的子序列为:"+());
for(String item : randomSeries){
randomList.add(Long.parseLong(item));
}
return randomList;
}
/**
* 读取参数文件文体内容
* @param destName 目标文件名称,不包含路径
* @param destDir 目标目录
* @param destEncode 目标字符集
*/
public static List<String> readFileData(String destName, String destDir, String destEncode) {
List<String> result = null;
FileInputStream inStream = null;
BufferedReader bufReader = null;
String inPathFileName = destDir + "/" +destName;
try {
inStream = getFileInStream(destName, destDir);
if(inStream ==null) {
return null;
}
bufReader = new BufferedReader(new InputStreamReader(inStream, destEncode));
String read = null;
result = new ArrayList<String>();
while ((read = bufReader.readLine()) != null) {
result.add(read);
}
} catch (Exception e) {
result = null;
} finally {
try {
if (bufReader != null) {
bufReader.close();
}
bufReader = null;
if (inStream != null) {
inStream.close();
}
inStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/** 获取读取文件流 */
public static FileInputStream getFileInStream(String srcName, String srcDir) {
FileInputStream in = null;
try {
String pathFileName = srcDir + "/" +srcName;
File inf = new File(pathFileName);
if ((inf == null) || (! inf.exists() )) {
return null;
}
in = new FileInputStream(pathFileName);
} catch (Exception e){
in = null;
}
return in;
}
public static void main(String[] args) {
JavaSamplerContext javaSamplerContext = new JavaSamplerContext(new Arguments());
UcTest ucTest =new UcTest();
ucTest.setupTest(javaSamplerContext);
ucTest.runTest(javaSamplerContext);
ucTest.teardownTest(javaSamplerContext);
}
}