并发性能测试程序编写

发表于:2014-6-09 11:13

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:visionsky    来源:51Testing软件测试网采编

  一般要测试软件或者库的性能,需要在多线程条件下进行。本文提供一种编写多线程性能测试的模板,方便大家参考和使用。
  本文以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();
}
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号