并发测试示例代码__Java SDK_SDK使用手册_消息服务-阿里云
本文档介绍了基于Java SDK提供的队列消息发送以及消费的并发测试Case。
1. 用户可指定并发度、运行时间;2. 使用发送总请求数除以运行时间得到QPS;
1. 初始化
在运行目录创建perf_test_config.properties文本文件,按照如下格式指定参数(其中队列请先行创建好):
Endpoint=AccessId=AccessKey=QueueName=JavaSDKPerfTestQueueThreadNum=200TotalSeconds=180
注:
ThreadNum是发送/消费的线程数,MNS具备强大的高并发扩展性能;TotalSeconds是测试Case运行的时间;
2. 代码
package com.aliyun.mns;import com.aliyun.mns.client.CloudAccount;import com.aliyun.mns.client.CloudQueue;import com.aliyun.mns.client.MNSClient;import com.aliyun.mns.common.http.ClientConfiguration;import com.aliyun.mns.model.Message;import com.aliyun.mns.model.QueueMeta;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Properties;import java.util.concurrent.atomic.AtomicLong;public class JavaSDKPerfTest {private static MNSClient client = null;private static AtomicLong totalCount = new AtomicLong(0);private static String endpoint = null;private static String accessId = null;private static String accessKey = null;private static String queueName = "JavaSDKPerfTestQueue";private static int threadNum = 100;private static int totalSeconds = 180;protected static boolean parseConf() {String confFilePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "perf_test_config.properties";BufferedInputStream bis = null;try {bis = new BufferedInputStream(new FileInputStream(confFilePath));if (bis == null) {System.out.println("ConfFile not opened: " + confFilePath);return false;}} catch (FileNotFoundException e) {System.out.println("ConfFile not found: " + confFilePath);return false;}// load fileProperties properties = new Properties();try {properties.load(bis);} catch(IOException e) {System.out.println("Load ConfFile Failed: " + e.getMessage());return false;} finally {try {bis.close();} catch (Exception e) {// do nothing}}// init the member parametersendpoint = properties.getProperty("Endpoint");System.out.println("Endpoint: " + endpoint);accessId = properties.getProperty("AccessId");System.out.println("AccessId: " + accessId);accessKey = properties.getProperty("AccessKey");queueName = properties.getProperty("QueueName", queueName);System.out.println("QueueName: " + queueName);threadNum = Integer.parseInt(properties.getProperty("ThreadNum", String.valueOf(threadNum)));System.out.println("ThreadNum: " + threadNum);totalSeconds = Integer.parseInt(properties.getProperty("TotalSeconds", String.valueOf(totalSeconds)));System.out.println("TotalSeconds: " + totalSeconds);return true;}public static void main(String[] args) {if (!parseConf()) {return;}ClientConfiguration clientConfiguration = new ClientConfiguration();clientConfiguration.setMaxConnections(threadNum);clientConfiguration.setMaxConnectionsPerRoute(threadNum);CloudAccount cloudAccount = new CloudAccount(accessId, accessKey, endpoint, clientConfiguration);client = cloudAccount.getMNSClient();CloudQueue queue = client.getQueueRef(queueName);queue.delete();QueueMeta meta = new QueueMeta();meta.setQueueName(queueName);client.createQueue(meta);// 1. Now check the SendMessageArrayList<Thread> threads = new ArrayList<Thread>();for (int i = 0; i < threadNum; ++i){Thread thread = new Thread(new Runnable() {public void run() {try {CloudQueue queue = client.getQueueRef(queueName);Message message = new Message();message.setMessageBody("Test");long count = 0;long startTime = System.currentTimeMillis();System.out.println(startTime);long endTime = startTime + totalSeconds * 1000;while (true) {for (int i = 0; i < 50; ++i) {queue.putMessage(message);}count += 50;if (System.currentTimeMillis() >= endTime) {break;}}System.out.println(System.currentTimeMillis());System.out.println("Thread" + Thread.currentThread().getName() + ": " + String.valueOf(count));totalCount.addAndGet(count);} catch (Exception e) {e.printStackTrace();}}}, String.valueOf(i));thread.start();threads.add(thread);}for (int i = 0; i < threadNum; ++i) {try {threads.get(i).join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("SendMessage QPS: ");System.out.println(totalCount.get() / totalSeconds);// 2. Now is the ReceiveMessagethreads.clear();totalCount.set(0);totalSeconds = totalSeconds / 3; // To ensure that messages in queue are enough for receivingfor (int i = 0; i < threadNum; ++i){Thread thread = new Thread(new Runnable() {public void run() {try {CloudQueue queue = client.getQueueRef(queueName);long count = 0;long endTime = System.currentTimeMillis() + totalSeconds * 1000;while (true) {for (int i = 0; i < 50; ++i) {queue.popMessage();}count += 50;if (System.currentTimeMillis() >= endTime) {break;}}System.out.println("Thread" + Thread.currentThread().getName() + ": " + String.valueOf(count));totalCount.addAndGet(count);} catch (Exception e) {e.printStackTrace();}}}, String.valueOf(i));thread.start();threads.add(thread);}for (int i = 0; i < threadNum; ++i) {try {threads.get(i).join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("ReceiveMessage QPS: ");System.out.println(totalCount.get() / totalSeconds);return;}}
最后更新:2016-11-23 17:16:08
上一篇:
消费消息示例代码__Java SDK_SDK使用手册_消息服务-阿里云
下一篇:
主题+HttpEndpoint使用手册__Python SDK_SDK使用手册_消息服务-阿里云
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云