TA的每日心情 | 开心 2017-12-1 09:52 |
---|
签到天数: 2 天 连续签到: 2 天 [LV.1]测试小兵
|
6#
楼主 |
发表于 2017-11-24 18:56:53
|
只看该作者
3.把代码导出为JAR file
4.把jar包放在apache-jmeter-3.2\lib\ext中
5.在Jmeter3.2中运行jmeter.bat
新增java请求
6.下面上代码:
package com.perf.jmeter;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class JmeterSample extends AbstractJavaSamplerClient {
private String username;
private String password;
String requset_str ="";
String res ="";
@Override
public void setupTest(JavaSamplerContext context) {
super.setupTest(context);
}
@Override
public void teardownTest(JavaSamplerContext context) {
super.teardownTest(context);
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("username", "apollo");
params.addArgument("password", "abcd1234");
return params;
}
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult sr = new SampleResult();
sr.sampleStart();
username = arg0.getParameter("username");
password = arg0.getParameter("password");
try {
requset_str = "username:" + username + "password:" + password;
Http javaHttp = new Http();
res = javaHttp.Post(requset_str, 1);
String arr[] = res.split("|");
if(arr[0].equals("200")) {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(true);
sr.setResponseCode("200");
sr.setResponseCodeOK();
sr.sampleEnd();
}else {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(false);
sr.sampleEnd();
}
} catch (Exception e) {
sr.setSamplerData(requset_str);
sr.setResponseData(e.getMessage(), "utf-8");
sr.setSuccessful(false);
sr.sampleEnd();
e.printStackTrace();
}
return sr;
}
public static void main(String[] args) {
JmeterSample runTestSample = new JmeterSample();
Arguments params = new Arguments();
JavaSamplerContext arg0 = new JavaSamplerContext(params);
runTestSample.runTest(arg0);
}
}
package com.perf.jmeter;
/**
* Created by xun.gong on 2016/12/26.
*/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Http {
public String Post(String content, int seq) {
System.out.println(content);
String res="not response";
String code ="600";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url = new URL("http://scottishroots.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Content-Type", "text/html; charset=utf-8");
// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.setConnectTimeout(3*10000);
connection.setReadTimeout(3*10000);
connection.connect();
PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
printWriter.write(content);
printWriter.flush();
printWriter.close();
try {
byte[] buf = new byte[1024];
int len = 0;
InputStream is = connection.getInputStream();
String responseCode = (new Integer(connection.getResponseCode()).toString());
String Message = (connection.getResponseMessage());
if(!(responseCode.equals("200")) || !(Message.equals("OK"))){
code="200";
}
while ((len = is.read(buf)) != -1) {
baos.write(buf, 0, len);
res = baos.toString();
}
baos.flush();
System.out.println("res: "+res);
baos.close();
is.close();
} catch (SocketTimeoutException e0) {
res = e0.getMessage();
System.out.println("SocketTimeoutException From (ServerResponse): " + e0);
}catch (Exception e1) {
res = e1.getMessage();
System.out.println("e1 From (ServerResponse): " + e1);
}
connection.disconnect();
} catch (Exception e2) {
res = e2.getMessage();
System.out.println("e2 From (ServerResponse): " + e2);
}
return code+"|"+res;
}
}
|
|