您的位置:首页 > 编程语言 > Java开发

java 编写接口性能测试

2015-09-15 08:51 363 查看
如题描述有点简单,其实也是自己的一点小的记录.

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

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 = "appli
4000
cation/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){

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: