java 编写接口性能测试

如题描述有点简单,其实也是自己的一点小的记录.

首先创建一个工程.创建一个类如下:

PerformanceThread 类:  [线程类]

package com.performance.test;

public class PerformanceThread extends Thread {


public synchronized void run() {


while (true) {
Test.t1();
}


}

}

创建一个方法类:

TestScript 类:


package com.performance.test;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class TestScript {


/**
* 接口测试定义
* @param host
* @param postPath
* @param param
* @param Content_TypeValue
* @return String
*/
public static String sendPost(String host,String postPath, String param,String Content_TypeValue) {

String response = "";

String url =host+postPath;

try {
// 固定值.......
URL postUrl = new URL(url);
 
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();// 

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type",Content_TypeValue+"; charset=UTF-8");

DataOutputStream out = new DataOutputStream(connection.getOutputStream());

out.writeBytes(param);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

response = reader.readLine();

reader.close();

} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return response;

}
}


创建一个执行类:

Test类:

package com.performance.test;


public class Test {


//脚本参数定义及调用方法.
public static void t1(){

String host="http://192.168.21.173:8080";
String postPath ="/testTestManager/getAllFunctionPoint.action";
String param = "productId=1&parentId=-1";
String Content_TypeValue = "application/x-www-form-urlencoded";


TestScript.sendPost(host, postPath, param, Content_TypeValue);
}

// 启动接口压力
public static void main(String[] args) {
try{
for (int i = 0; i < 100; i++) {
new Thread(new PerformanceThread()).start();        
}
}catch (Exception e){

}
}

}


注意: 红色标记的是参数的值.这些参数值从那里可以得到.可以通过 fiddler 工具抓包获得,当然也可以用其它方式获得 报文里面的类容.





以上也可以不分开,直接写在一个类里面如下:

package com.performance.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class Login extends Thread {

String host="http://192.168.21.173:8080";
//String postPath = "/testTestManager/login";

String postPath ="/testTestManager/getAllFunctionPoint.action";




// String param = "dopost=login&name=admin&pass=''&sm1=%e7%99%bb%e9%99%86";
String param = "productId=1&parentId=-1";

public static String sendPost(String host,String postPath, String param,String Content_TypeValue) {

String response = "";

String url =host+postPath;

try {
// 固定值.......
URL postUrl = new URL(url);
 
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();// 

connection.setDoOutput(true);


connection.setDoInput(true);


connection.setRequestMethod("POST");


connection.setInstanceFollowRedirects(true);
//�固定.....end


//String Content_TypeValue = "application/x-www-form-urlencoded";

// String Content_TypeValue = "application/x-www-form-urlencoded";

connection.setRequestProperty("Content-Type",Content_TypeValue+"; charset=UTF-8");


DataOutputStream out = new DataOutputStream(connection.getOutputStream());

out.writeBytes(param);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

response = reader.readLine();

reader.close();

} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return response;

}



public void showResponse(){
String response =null;
//response = sendPost(url,param);

System.out.println("Ӧ�����"+response);
System.out.println("\nӦ�����");
// for (String i : str1) {
// System.out.println(i);
// }
}


public void t1(){

String host="http://192.168.21.173:8080";

String postPath ="/testTestManager/getAllFunctionPoint.action";


String param = "productId=1&parentId=-1";
String Content_TypeValue = "application/x-www-form-urlencoded";
Login.sendPost(host, postPath, param, Content_TypeValue);
}




public void run (){
synchronized(this){ 

while(true){
Login login = new Login();
login.t1();

}
}
}





public static void main(String[] args) {
try{
for (int i = 0; i < 100; i++) {
new Thread(new Login()).start();        
}
}catch (Exception e){

}



}

}

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值