需求: 向lcbserver髮長連接
原始方法:
java public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult results = new SampleResult();
int i = 0;
Long totaltime = null;
try {
results.setSampleLabel("TestLCB-HowManyConnect");
results.sampleStart();
Long start = System.currentTimeMillis();
for (i = 0; i <= 10000000; i++) {
Thread.sleep(1);
Client.init();
Client.connect();
}
results.sampleEnd();
Long end = System.currentTimeMillis();
totaltime = end - start;
} catch (Throwable t) {
t.printStackTrace();
System.out.println(t.getMessage());
System.out.println("Exceptoin occur------------------------------");
System.out.println("When exception happen, the connection number is:" + i + "TotalTime:" + totaltime);
results.setSuccessful(false);
}
return results;
}
這種調用客戶端的方式要調用mina,使用linux jmeter 拋 too many file的錯誤,linux機器上句柄打開太多,壓力還沒發到服務端,客戶端就先掛掉了。
新的解決方式: 採用原聲的socket 連接的方式
jmeter代碼:
package com.lcb.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
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 LcbConnetSocket extends AbstractJavaSamplerClient {
private static Integer sotimeout = 1 * 10 * 1000;
public static List<Byte> encodeRemainingLength(int value) {
List<Byte> l = new ArrayList<Byte>();
byte digit;
do {
digit = (byte) (value % 128);
value = value / 128;
if (value > 0) {
digit = (byte) (digit | 0x80);
}
l.add(digit);
} while (value > 0);
return l;
}
public static void writeWord(OutputStream out, int value) throws IOException {
out.write((byte) ((value & 0xFF00) >> 8)); // msb
out.write((byte) (value & 0x00FF)); // lsb
}
public void setupTest(JavaSamplerContext arg0) {
super.setupTest(arg0);
}
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult results = new SampleResult();
int i = 0;
try {
results.setSampleLabel("TestLCB-HowManyConnect");
results.sampleStart();
for (i = 1; i <= 10000000; i++) {
Socket socket = new Socket("10.20.162.21", 1883);
socket.setKeepAlive(true);// 開啓保持活動狀態的套接字
socket.setSoTimeout(sotimeout);
OutputStream out = socket.getOutputStream();
byte a = 0x10;
byte b[] = { 0x00, 0x06, 0x4d, 0x51, 0x49, 0x73, 0x64, 0x70, 0x03, (byte) 0x80, 0x00, 0x0A };
String username = "{'accessToken' : 'a0c79c116b8d474c56a8f9528b72ea16'}";
String clientId = "laiwang/" + System.currentTimeMillis();
byte[] uname = username.getBytes("utf-8");
byte[] client = clientId.getBytes("utf-8");
int size = 12 + client.length + 2 + uname.length + 2;
List<Byte> re = encodeRemainingLength(size);
out.write(a);
for (Byte bd : re) {
out.write(bd);
}
out.write(b);
writeWord(out, client.length);
out.write(client);
writeWord(out, uname.length);
out.write(uname);
out.flush();
while (true) {
InputStream input = socket.getInputStream();
byte[] bc = new byte[3];
input.read(bc);
if (bc.length == 3 & bc[0] == 32 & bc[1] == 2) {
int count = i;
//System.out.println("fff:" + bc.length + "tt=" + bc[0] + "mm" + bc[1]);
System.out.println("建立連接成功,已建立的連接數爲" + count);
break;
}
}
}
results.sampleEnd();
} catch (Throwable t) {
t.printStackTrace();
System.out.println(t.getMessage());
results.setSuccessful(false);
}
return results;
}
public void teardownTest(JavaSamplerContext arg0) {
System.exit(0);
}
public static void main(String[] args) {
LcbConnetSocket test = new LcbConnetSocket();
JavaSamplerContext arg0 = new JavaSamplerContext(new Arguments());
test.setupTest(arg0);
test.runTest(arg0);
test.teardownTest(arg0);
}
}
參考資料:
socket 服務器寫數據 http://wenku.baidu.com/view/5e5c356627d3240c8447ef28.html
socket 服務器讀數據 http://wenku.baidu.com/view/83732ca20029bd64783e2ca4.html
mqtt 3.1 http://www.doc88.com/p-337769072534.html 3.1 connection
java socket 實例 http://hi.baidu.com/bieluanchi/item/2ce379292e5bda83af48f519