並發測試示例代碼__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-阿裏雲