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

来源:互联网 发布:支付宝海外 知乎 编辑:程序博客网 时间:2024/05/16 18:50

Dears:

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

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

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

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

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

2.1测试场景简介

下面以测试XXXX性能测试结果为例做个简单的对比.由于XXXX后端协议用的是TCP/IP协议,后端AGENT 发送很多带不同参数类型到MONITOR现要测试一个MONITOR处理极限是多少?理想状态希望一个monitor最高能支持1WAGENTE的信息,并且这个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

APP780%

DB23%

APP36%

DB: 85%

20

短连接

7292

0.011S

APP740%

DB14%

APP57%

DB83%

 

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();
  String message = "[{/"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();
  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 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-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;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)//Subversion//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) throws 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/":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,/"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();
  String message = "[{/"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();
  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 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-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;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)//Subversion//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) throws 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/":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,/"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);
 }
}

 

原创粉丝点击