1、长连接和短连接性能测试结果差异长连接和短连接性能测试结果差异这次测试针对长连接和短连接对性能测试的影响做一个简单的比较,详细情况见下面1. 什么是TCP长连接什么是短连接?一般人讲的长连接与短连接的,这是一个通俗的说法, 这个TCP连接是根据连接时间的长短定义的。何谓短连接:就是一次操作完后断开连接,常见于大客户情况 如WEB服务器,如果每个连接都使用长连接 那么每个客户都保留一个socket ,系统资源耗费比较大。 何谓长连接:就是一次操作完后不断开连接,连接在一段时间保持着,则是多用于操作频繁情况,每个TCP连接都需要三步握手 这需要时间 如果每个操作都是先连接再操作的话那么处理速度会降低
2、很多所以每个操作完后都不断开 下次处理时直接发送数据包就可以了, 不用重新建立TCP新连接。2. 在性能测试过程中,需要注意业务需求,应该是用长连接还是短连接?之间的性能差异大概是多少?如果有差异是消耗在哪里?2.1测试场景简介:下面以测试XXXX性能测试结果为例,做个简单的对比.由于XXXX后端协议用的是TCP/IP协议,后端AGENT 发送很多带不同参数类型到MONITOR。现要测试一个MONITOR处理极限是多少?理想状态希望一个monitor最高能支持1W条AGENTE的信息,并且这个1W条信息时希望只建立一个SOCKET连接里面发送的事务数。 2.2测试环境描述:机器名CPU内存OS
3、应用软件说明10.20.136.19(DB)816GLinuxtomcat10.20.136.23(APP)816GLinuxtomcat10.20.136.7388Gwindowsloadrunner2.2测试脚本简述:在LR中开发JAVA脚本,直接发送字符串并成功接受返回的字符串。2.4测试结果对比:并发线程连接类型TPS响应时间CPU内存20长连接67660.015SAPP:780%DB:23%APP:36%DB: 85%20短连接72920.011SAPP:740%DB:14%APP:57%DB:83%2.5测试结果分析:线程并发数一样,但是长连接的TPS低于短连接的TPS,相差大概在
4、6%左右,长连接的应用服务器的APP的资源利用稍微大点,但是短连接的内存消耗明显比长连接的高,高出了大概58%左右。之所以消耗怎么高是因为,短连接不停的忙着建立连接,不停的建立握手,这样频繁的操作,造成内存资源上的很大消耗。3.总结虽然短连接的测试结果TPS以及相应时间是好于长连接的测试结果,但是不符合线上环境。最重要的一点是,测试人员,做测试脚本以及设计测试场景的时候,一定谨记不要把测试数据发送到服务器端,压力上去后,就不去分析了写的测试脚本以及测试场景是否是满足线上需要的,这样得出的测试结果会给开发人员造成一定的误解。测试的场景单一,没有去分析线程并发在不同的情况下的,性能结果差异是多少,
5、如果谁有兴趣可以在分不同的线程并发,多尝试几次,看看性能数据的差异是多少?4.测试代码附上,4.1长连接代码:/* LoadRunner Java script. (Build: 3020)* * Script Description: * */import lrapi.lr;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .Socket;public class ActionsSocket socket = null;DataOutputStrea
6、m out;DataInputStream in;public int init() throws Throwable socket = new Socket(10.20.136.23, 13888); out = new DataOutputStream(socket.getOutputStream(); in = new DataInputStream(socket.getInputStream(); sendConnect(out, 10.16.200.119, performance_test); String text = readMessage(in); System.out.pr
7、intln(text); return 0;/end of initpublic int action() throws Throwable send_memory(out);send_jvm(out);send_gc(out);send_threading(out); return 0;/end of actionpublic int end() throws Throwable if (socket != null) socket.close(); return 0;/end of endpublic String readMessage(DataInputStream in) throw
8、s IOException short type = in.readShort(); if (type != 1) throw new IOException(not support type + type); int length = in.readInt(); byte bytes = new bytelength; in.readFully(bytes); return new String(bytes, UTF-8);public void sendConnect(DataOutputStream out, String ip, String hostname) throws IOEx
9、ception String message = /T/:/Connect/,/S/:1,/MAC_ADDR/:/00-26-c6-8f-d8-2c/,/00-50-56-c0-00-08/,/IP/:/+ ip+ /,/192.168.64.1/,/VERSON/:/2.5/,/SERVICE_TAG/:/wenshao-pc-service-tag/,/HOST_NAME/:/+ hostname + /,/CLIENT_SESSION/:;sendMessage(out, message);public void send_memory(DataOutputStream out) thr
10、ows Exception long date = System.currentTimeMillis();String message = /T/:/MonitorItemData/,/S/:39,/D/:/UsedPhysicalMemorySize/:1908174848,/MemoryNonHeapCommitted/:41943040,/MemoryHeapCommitted/:122224640,/MemoryHeapUsed/:101766328,/TotalPhysicalMemorySize/:2036363264,/UsedSwapSpaceSize/:2582024192,
11、/TotalSwapSpaceSize/:4072726528,/MemoryNonHeapUsed/:41367072,/S/:/APP_NUM/:/demo/,/INST_NUM/:null,/TS/: + date + ,/MID/:548691;sendMessage(out, message);public void send_jvm(DataOutputStream out) throws Exception long date = System.currentTimeMillis();String message = /T/:/MonitorItemData/,/S/:42,/D
12、/:/AvailableProcessors/:2,/JavaHome/:/C:/Program Files/Java/jdk1.6.0_19/jre/,/JavaVersion/:/1.6.0_19/,/PID/:/3112/,/OSVersion/:/6.1/,/UnloadedClassCount/:0,/TotalCompilationTime/:6089,/OSName/:/Windows 7/,/JavaSpecificationVersion/:/1.6/,/Arch/:/amd64/,/LoadedClassCount/:5006,/JVM/:/Java HotSpot(TM)
13、 64-Bit Server VM (16.2-b04, mixed mode)/,/StartTime/:1288273090499,/InputArguments/:/-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:55785/n-Dcatalina.base=D:/java/workspace-dragoon-25/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/n-Dcatalina.home=D:/java/apache-tomcat-6.0.26/n
14、-Dwtp.deploy=D:/java/workspace-dragoon-25/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/n-Djava.endorsed.dirs=D:/java/apache-tomcat-6.0.26/endorsed/n-Dfile.encoding=UTF-8/,/TotalLoadedClassCount/:5006,/JavaLibraryPath/:/C:/Program Files/Java/jdk1.6.0_19/bin;.;C:/Windows/Sun/Java/bin
15、;C:/Windows/system32;C:/Windows;C:/Program Files/Java/jdk1.6.0_19/jre/bin;C:/product/11.1.0/client_1;C:/Windows/system32;C:/Windows;C:/Windows/System32/Wbem;C:/Windows/System32/WindowsPowerShell/v1.0/;C:/Program Files (x86)/TortoiseSVN/bin;D:/java/apache-maven-2.2.1/bin;C:/Program Files (x86)/Subver
16、sion/bin;C:/Program Files (x86)/SSH Communications Security/SSH Secure Shell;C:/Program Files (x86)/Subversion/bin;C:/Python25;D:/java/diffutils-2.8.7-1-bin/bin/,/S/:/APP_NUM/:/demo/,/INST_NUM/:null,/TS/: + date + ,/MID/:554891;sendMessage(out, message);public void send_gc(DataOutputStream out) thro
17、ws Exception long date = System.currentTimeMillis();String message = /T/:/MonitorItemData/,/S/:41,/D/:/YoungGCCollectionTime/:107,/FullGCCollectionTime/:141,/FullGCCollectionCount/:11,/PermGenUsed/:39267864,/SurvivorSpaceUsed/:7201080,/EdenSpaceUsed/:85454816,/YoungGCCollectionCount/:10,/OldGenUsed/
18、:11791088,/S/:/APP_NUM/:/demo/,/INST_NUM/:null,/TS/: + date + ,/MID/:552091;sendMessage(out, message);public void send_threading(DataOutputStream out) throws Exception long date = System.currentTimeMillis();String message = /T/:/MonitorItemData/,/S/:43,/D/:/RunnableThreadCount/:15,/ProcessCpuTimeRat
19、e/:0.901,/NewThreadCount/:0,/TotalStartedThreadCount/:49,/BlockedThreadCount/:0,/DeadLockedThreadCount/:0,/FullGCCollectionTimeRate/:0,/DaemonThreadCount/:17,/WaitingThreadCount/:11,/TeminatedThreadCount/:0,/ThreadCount/:48,/TimedWaitingThreadCount/:25,/S/:/APP_NUM/:/demo/,/INST_NUM/:null,/TS/: + da
20、te + ,/MID/:550191;sendMessage(out, message);private void sendMessage(DataOutputStream out, String text) throws IOException byte bytes = text.getBytes(UTF-8);out.writeShort(1);out.writeInt(bytes.length);out.write(bytes);4.2短连接代码/* LoadRunner Java script. (Build: 3020)* * Script Description: * */impo
21、rt lrapi.lr;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import .Socket;public class Actionspublic int init() throws Throwable return 0;/end of initpublic int action() throws Throwable test_connect();return 0;/end of actionpublic int end() throws Throwabl
22、e return 0;/end of end public void test_connect() throws Exception / 10.249.168.152:18001Socket socket = null;try socket = new Socket(10.20.136.23, 13888);DataOutputStream out = new DataOutputStream(socket.getOutputStream();DataInputStream in = new DataInputStream(socket.getInputStream();sendConnect
23、(out, 10.16.200.119, performance_test);String text = readMessage(in);System.out.println(text);send_memory(out);send_jvm(out);send_gc(out);send_threading(out); finally if (socket != null) socket.close(); public String readMessage(DataInputStream in) throws IOException short type = in.readShort();if (
24、type != 1) throw new IOException(not support type + type);int length = in.readInt();byte bytes = new bytelength;in.readFully(bytes);return new String(bytes, UTF-8);public void sendConnect(DataOutputStream out, String ip, String hostname) throws IOException String message = /T/:/Connect/,/S/:1,/MAC_A
25、DDR/:/00-26-c6-8f-d8-2c/,/00-50-56-c0-00-08/,/IP/:/+ ip+ /,/192.168.64.1/,/VERSON/:/2.5/,/SERVICE_TAG/:/wenshao-pc-service-tag/,/HOST_NAME/:/+ hostname + /,/CLIENT_SESSION/:;sendMessage(out, message);public void send_memory(DataOutputStream out) throws Exception long date = System.currentTimeMillis(
26、);String message = /T/:/MonitorItemData/,/S/:39,/D/:/UsedPhysicalMemorySize/:1908174848,/MemoryNonHeapCommitted/:41943040,/MemoryHeapCommitted/:122224640,/MemoryHeapUsed/:101766328,/TotalPhysicalMemorySize/:2036363264,/UsedSwapSpaceSize/:2582024192,/TotalSwapSpaceSize/:4072726528,/MemoryNonHeapUsed/
27、:41367072,/S/:/APP_NUM/:/demo/,/INST_NUM/:null,/TS/: + date + ,/MID/:548691;sendMessage(out, message);public void send_jvm(DataOutputStream out) throws Exception long date = System.currentTimeMillis();String message = /T/:/MonitorItemData/,/S/:42,/D/:/AvailableProcessors/:2,/JavaHome/:/C:/Program Files/Java/jdk1.6.0_19/jre/,/JavaVersion/:/1.6.0_19/,/PID/:/3112/,/OSVersion/:/6.1/,/UnloadedClassCount/:0,/TotalCompilationTime/:6089,/OSName/:/Windows 7/,/JavaSpecificationVersion/:/1.6/,/Arch/:/amd64/,/LoadedClassCount/:5006,/JVM/:/Java HotSpot(TM) 64-Bit Serv
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1