并发性能测试程序编写
作者:网络转载 发布时间:[ 2014/6/9 13:43:58 ] 推荐标签:性能测试 程序编写
一般要测试软件或者库的性能,需要在多线程条件下进行。本文提供一种编写多线程性能测试的模板,方便大家参考和使用。
本文以AES加密和解密为例,并指出Cipher的获取在程序中的不同位置会对程序性能造成的影响。
程序代码如下:
package com.lazycat.secure.aes;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AESCoder {
public static int count = 1000000;
public static CountDownLatch latch =new CountDownLatch(count);
public static void main(String[] args)throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(200);
final byte[] payload = "{"msg":{"content":{"text":"JJH","tplId":0},"from":{"name":"10000213","id":1,"type":0},"to":{"name":"10095812","id":10000213,"type":0},"time":0,"txid":0,"subtype":1},"type":"chat"}".getBytes(Charset.forName("utf-8"));
final String secureKey ="BBmFdTFVgAjgHNwRkWWRcOFFiBzAANFU9DmMAP1JpBmc.";
long start = System.currentTimeMillis();
for(int i = 0 ; i <count ; i++){
pool.execute(new Runnable() {
public void run() {
AESCoder coder = new AESCoder();
byte[] enret =null;
try {
enret = coder.encrypt(payload,secureKey);
byte[] deret = coder.decrypt(enret, secureKey);
System.out.println(new String(deret,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
latch.countDown();
}
});
}
latch.await();
System.out.println(System.currentTimeMillis() - start);
pool.shutdown();
}
private byte[] encrypt(byte[] payload,String securekey)throws Exception{
byte[] enCodeFormat = securekey.substring(0, 16).getBytes();
SecretKeySpec key = newSecretKeySpec(enCodeFormat,"AES");
Cipher cipher = CliperInstance.getInstance();//创建密码器
//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);//初始化
byte[] result = cipher.doFinal(payload);
return result;
}
public byte[] decrypt(byte[] buffer,StringsecureKey)throws Exception{
byte[] enCodeFormat = secureKey.substring(0,16).getBytes();
SecretKeySpec key = newSecretKeySpec(enCodeFormat,"AES");
//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//创建密码器
Cipher cipher = CliperInstance.getInstance();
cipher.init(Cipher.DECRYPT_MODE, key);//初始化
byte[] result = cipher.doFinal(buffer);
return result;
}
}
class CliperInstance {
private static ThreadLocal<Cipher> cipherTL =new ThreadLocal<Cipher>(){
@Override
protected Cipher initialValue() {
try {
return Cipher.getInstance("AES/ECB/PKCS5Padding");
}catch(Exception e){
return null;
}
}
};
public static CiphergetInstance() throws NoSuchAlgorithmException, NoSuchPaddingException{
returncipherTL.get();
}
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南