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

java 编写接口性能脚本

2015-09-15 09:03 429 查看
入门

首先创建一个工程.编写一个线程类  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 工具抓包获得, 具体参数值都在包含包的报文里面.

另外以上的一些类也可以直接写在一个类里面如下类  Login :

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Ӧ�����")
9eb4
;

// 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){

}

}

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