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 工具抓包获得, 具体参数值都在包含包的报文里面.


另外以上的一些类也可以直接写在一个类里面如下类  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Ӧ�����");
// 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){

}



}

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值