长连接和短连接性能测试结果差异

这次测试针对长连接和短连接对性能测试的影响做一个简单的比较,详细情况见下面

1.       什么是TCP长连接什么是短连接

一般人讲的长连接与短连接的,这是一个通俗的说法, 这个TCP连接是根据连接时间的长短定义的。

何谓短连接:就是一次操作完后断开连接,常见于大客户情况 如WEB服务器,如果每个连接都使用长连接 那么每个客户都保留一个socket ,系统资源耗费比较大。
何谓长连接:就是一次操作完后不断开连接,连接在一段时间保持着,则是多用于操作频繁情况,每个TCP连接都需要三步握手 这需要时间 如果每个操作都是先连接 再操作的话那么处理速度会降低很多 所以每个操作完后都不断开 下次处理时直接发送数据包就可以了, 不用重新建立TCP新连接。。

2.       在性能测试过程中,需要注意业务需求,应该是用长连接还是短连接?之间的性能差异大概是多少?如果有差异是消耗在哪里?

2.1测试场景简介

下面以测试XXXX性能测试结果为例做个简单的对比.由于XXXX后端协议用的是TCP/IP协议,后端AGENT 发送很多带不同参数类型到MONITOR现要测试一个MONITOR处理极限是多少?理想状态希望一个monitor最高能支持1W条AGENTE的信息,并且这个1W条信息时希望只建立一个SOCKET连接里面发送的事务数。

2.2测试环境描述:

机器名

CPU

内存

OS

应用软件

说明

10.20.136.19 (DB)

8

16G

Linux

tomcat

 

10.20.136.23(APP)

8

16G

Linux

tomcat

 

10.20.136.73

8

8G

windows

loadrunner

 

 

2.2测试脚本简述:在LR中开发JAVA脚本,直接发送字符串并成功接受返回的字符串。

2.4测试结果对比

并发线程

连接类型

TPS

响应时间

CPU

内存

20

长连接

6766

0.015S

APP:780%

DB:23%

APP:36%

DB: 85%

20

短连接

7292

0.011S

APP:740%

DB:14%

APP57%

DB:83%

 

2.5测试结果分析

线程并发数一样,但是长连接的TPS低于短连接的TPS,相差大概在6%左右,长连接的应用服务器的APP的资源利用稍微大点,但是短连接的内存消耗明显比长连接的高,高出了大概58%左右。之所以消耗怎么高是因为,短连接不停的忙着建立连接,不停的建立握手,这样频繁的操作,造成内存资源上的很大消耗

 

3.总结

虽然短连接的测试结果TPS以及相应时间是好于长连接的测试结果,但是不符合线上环境最重要的一点是测试人员,做测试脚本以及设计测试场景的时候,一定谨记不要把测试数据发送到服务器端,压力上去后,就不去分析了写的测试脚本以及测试场景是否是满足线上需要的,这样得出的测试结果会给开发人员造成一定的误解。

测试的场景单一,没有去分析线程并发在不同的情况下的,性能结果差异是多少,如果谁有兴趣可以在分不同的线程并发,多尝试几次,看看性能数据的差异是多少?

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 java.net.Socket;

public class Actions
{
        Socket socket = null;
 DataOutputStream 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.println(text);

            return 0;
 }//end of init

 public int action() throws Throwable {

   
 
  send_memory(out);
  send_jvm(out);
  send_gc(out);
  send_threading(out);
 
            return 0;
 }//end of action

 public int end() throws Throwable {

     if (socket != null) {
                socket.close();
            }
    
            return 0;
 }//end of end

 public String readMessage(DataInputStream in) throws IOException {
            short type = in.readShort();
            if (type != 1) {
                throw new IOException("not support type " + type);
            }

            int length = in.readInt();
            byte[] bytes = new byte[length];
            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_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) throws Exception {
  long date = System.currentTimeMillis();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":39},{/"D/":{/"UsedPhysicalMemorySize/":1908174848,/"MemoryNonHeapCommitted/":41943040,/"MemoryHeapCommitted/":122224640,/"MemoryHeapUsed/":101766328,/"TotalPhysicalMemorySize/":2036363264,/"UsedSwapSpaceSize/":2582024192,/"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();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":42},{/"D/":{/"AvailableProcessors/":2,/"JavaHome/":/"C://ProgramFiles//Java//jdk1.6.0_19//jre/",/"JavaVersion/":/"1.6.0_19/",/"PID/":/"3112/",/"OSVersion/":/"6.1/",/"UnloadedClassCount/":0,/"TotalCompilationTime/":6089,/"OSName/":/"Windows7/",/"JavaSpecificationVersion/":/"1.6/",/"Arch/":/"amd64/",/"LoadedClassCount/":5006,/"JVM/":/"JavaHotSpot(TM) 64-Bit Server VM (16.2-b04, mixedmode)/",/"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-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://ProgramFiles//Java//jdk1.6.0_19//bin;.;C://Windows//Sun//Java//bin;C://Windows//system32;C://Windows;C://ProgramFiles//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://ProgramFiles(x86)//TortoiseSVN//bin;D://java//apache-maven-2.2.1//bin;C://ProgramFiles (x86)//Subversion//bin;C://Program Files (x86)//SSH CommunicationsSecurity//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) throws Exception {
  long date = System.currentTimeMillis();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":41},{/"D/":{/"YoungGCCollectionTime/":107,/"FullGCCollectionTime/":141,/"FullGCCollectionCount/":11,/"PermGenUsed/":39267864,/"SurvivorSpaceUsed/":7201080,/"EdenSpaceUsed/":85454816,/"YoungGCCollectionCount/":10,/"OldGenUsed/":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();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":43},{/"D/":{/"RunnableThreadCount/":15,/"ProcessCpuTimeRate/":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/":"+ date + ",/"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:
 *                    
 */

import lrapi.lr;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Actions
{

 public int init() throws Throwable {
  return 0;
 }//end of init


 public int action() throws Throwable {
            test_connect();
  return 0;
 }//end of action


 public int end() throws Throwable {
  return 0;
 }//end of end

        public void test_connect() throws Exception {
   // 10.249.168.152:18001
  Socket socket = null;
  try {
   socket = new Socket("10.20.136.23", 13888);
   DataOutputStream out = new DataOutputStream(socket.getOutputStream());
   DataInputStream in = new DataInputStream(socket.getInputStream());
   sendConnect(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 (type != 1) {
   throw new IOException("not support type " + type);
  }

  int length = in.readInt();
  byte[] bytes = new byte[length];
  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_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) throws Exception {
  long date = System.currentTimeMillis();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":39},{/"D/":{/"UsedPhysicalMemorySize/":1908174848,/"MemoryNonHeapCommitted/":41943040,/"MemoryHeapCommitted/":122224640,/"MemoryHeapUsed/":101766328,/"TotalPhysicalMemorySize/":2036363264,/"UsedSwapSpaceSize/":2582024192,/"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();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":42},{/"D/":{/"AvailableProcessors/":2,/"JavaHome/":/"C://ProgramFiles//Java//jdk1.6.0_19//jre/",/"JavaVersion/":/"1.6.0_19/",/"PID/":/"3112/",/"OSVersion/":/"6.1/",/"UnloadedClassCount/":0,/"TotalCompilationTime/":6089,/"OSName/":/"Windows7/",/"JavaSpecificationVersion/":/"1.6/",/"Arch/":/"amd64/",/"LoadedClassCount/":5006,/"JVM/":/"JavaHotSpot(TM) 64-Bit Server VM (16.2-b04, mixedmode)/",/"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-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://ProgramFiles//Java//jdk1.6.0_19//bin;.;C://Windows//Sun//Java//bin;C://Windows//system32;C://Windows;C://ProgramFiles//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://ProgramFiles(x86)//TortoiseSVN//bin;D://java//apache-maven-2.2.1//bin;C://ProgramFiles (x86)//Subversion//bin;C://Program Files (x86)//SSH CommunicationsSecurity//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) throws Exception {
  long date = System.currentTimeMillis();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":41},{/"D/":{/"YoungGCCollectionTime/":107,/"FullGCCollectionTime/":141,/"FullGCCollectionCount/":11,/"PermGenUsed/":39267864,/"SurvivorSpaceUsed/":7201080,/"EdenSpaceUsed/":85454816,/"YoungGCCollectionCount/":10,/"OldGenUsed/":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();
  Stringmessage ="[{/"T/":/"MonitorItemData/",/"S/":43},{/"D/":{/"RunnableThreadCount/":15,/"ProcessCpuTimeRate/":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/":"+ date + ",/"MID/":831891}]";
  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);
 }
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MySQL的内连接和左连接的效率问题不是绝对的,它们的效率取决于具体的查询语句和表结构。在某些情况下,内连接可能更快,而在其他情况下,左连接可能更快。一般情况下,内连接的效率比左连接更快,因为内连接只返回两个表之间匹配的行,而左连接需要返回左表的所有行和匹配的右表的行,如果右表中没有匹配的行,则返回NULL值。但是在某些情况下,使用左连接可以避免查询中的重复数据,提查询效率。因此,选择使用内连接还是左连接应该根据具体的需求和查询条件来决定。 ### 回答2: MySQL的内连接和左连接的效率并没有绝对的答案,因为它们的效率取决于具体的数据库设计、索引使用和查询需求等多个因素。 内连接是通过匹配两张表之间的共同数据来返回结果。它仅返回匹配的行,如果表之间没有匹配的数据,那么将不会返回任何结果。由于内连接只返回匹配的行,所以它通常是比较效的。如果在连接的列上存在索引,那么内连接的效率将更,因为索引能够加速数据的查找和匹配过程。 左连接是将左表中的所有数据都返回,同时匹配右表中的数据。如果右表中没有匹配的数据,那么结果中对应的字段就会显示为NULL。左连接会返回较多的数据,所以在一些情况下可能会比较耗时。但是如果查询需要获取左表的全部数据,无论是否存在匹配的右表数据,那么左连接是更合适的选择。 总的来说,内连接在某些具体的场景下可能比左连接效,因为它只返回匹配的数据。但是在其他情况下,左连接可能更适合获取完整的数据集。不同的查询需求和数据库设计都会对效率产生影响,因此在实际应用中,我们需要根据具体情况选择合适的连接方式。 ### 回答3: MySQL的内连接和左连接的效率取决于具体的查询和数据情况。一般情况下,内连接的效率更。 内连接是通过比较两个表之间的关联字段,并返回满足条件的记录。它只返回两个表中关联字段匹配的数据,因此它们通常需要比较少的数据量,从而提查询效率。 左连接是根据左表的记录,联接右表的记录,并返回所有左表的记录以及满足关联条件的右表记录。这意味着左连接可能返回大量的数据,尤其是右表中含有大量匹配记录时。因此左连接在某些情况下可能会降低查询效率。 然而,具体的情况可能因查询条件、数据量和表结构等因素而异。如果左表和右表的数据量相似,内连接和左连接的效率可能没有明显差异。而如果右表中的匹配记录非常少,左连接可能会在一定程度上更快,因为它不需要进行大量的数据匹配。 总的来说,内连接和左连接的效率取决于实际情况,通常情况下内连接更快,但在特定情况下左连接也可能具有更的效率。对于具体的查询,可以通过分析执行计划和性能测试来确定最佳的连接方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值