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){
}
}
}
首先创建一个工程.编写一个线程类 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){
}
}
}
相关文章推荐
- java学习之System类
- 讲解Java中如何构造内部类对象以及访问对象
- [置顶] JAVA目录:
- java 编写接口性能测试
- import javax.servlet.annotation.*;出错
- 99乘法java
- java学习之Random类
- 基本常识
- java 开发, jdk 1.6 官方下载地址
- java虚拟机回收机制原理
- JSP学习之JavaBean用法分析
- java break语句详解 break label;语句的使用
- 1006--eclipse+webservice开发实例
- 深入学习微框架:Spring Boot
- 深入学习微框架:Spring Boot
- 深入学习微框架:Spring Boot
- 深入学习微框架:Spring Boot
- struts2如何获取数据
- 使用myeclipse“返回商品”步骤及代码(2015.09.15)
- java 内存泄露调试和解决