You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMeter:如何消除TCP采样器请求之间的间隔

问题描述

发送TCP请求"login-bam"后,该请求与"heatbeat"请求之间存在约2秒的间隔。已勾选NoDelay选项,但并未解决问题,恳请告知如何消除该间隔。

以下是JMeter测试计划脚本(.jmx):

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">true</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <intProp name="LoopController.loops">-1</intProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration">0</stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
      </ThreadGroup>
      <hashTree>
        <TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="login-bam" enabled="true">
          <stringProp name="TCPSampler.classname">BinaryTCPClientImpl</stringProp>
          <stringProp name="TCPSampler.server">10.3.0.132</stringProp>
          <boolProp name="TCPSampler.reUseConnection">true</boolProp>
          <stringProp name="TCPSampler.port">60101</stringProp>
          <boolProp name="TCPSampler.nodelay">true</boolProp>
          <stringProp name="TCPSampler.timeout">2000</stringProp>
          <stringProp name="TCPSampler.ctimeout">2000</stringProp>
          <stringProp name="TCPSampler.request">630000000100003133366132396537316335373435653538313366666465666138633264633739023401002220807b227479706522203a202242414d222c22696422203a202231222c2276657222203a2022312e302e30222c22687274223a2234227d</stringProp>
          <boolProp name="TCPSampler.closeConnection">false</boolProp>
          <stringProp name="TCPSampler.soLinger">0</stringProp>
          <stringProp name="ConfigTestElement.username"></stringProp>
          <stringProp name="ConfigTestElement.password"></stringProp>
        </TCPSampler>
        <hashTree/>
        <TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="heatbeat" enabled="true">
          <stringProp name="TCPSampler.classname">BinaryTCPClientImpl</stringProp>
          <stringProp name="TCPSampler.server">10.3.0.132</stringProp>
          <boolProp name="TCPSampler.reUseConnection">true</boolProp>
          <stringProp name="TCPSampler.port">60101</stringProp>
          <boolProp name="TCPSampler.nodelay">true</boolProp>
          <stringProp name="TCPSampler.timeout">10000</stringProp>
          <stringProp name="TCPSampler.ctimeout">10000</stringProp>
          <stringProp name="TCPSampler.request">2f0000000100006332373265613362646163373435626239343039333030313537313362393165023300002220477d</stringProp>
          <boolProp name="TCPSampler.closeConnection">false</boolProp>
          <stringProp name="TCPSampler.soLinger">0</stringProp>
          <stringProp name="ConfigTestElement.username"></stringProp>
          <stringProp name="ConfigTestElement.password"></stringProp>
        </TCPSampler>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>
解决方法
  • 排查响应等待逻辑:默认TCP采样器会等待服务器返回响应后才执行下一个请求,若login-bam的响应耗时2秒,就会导致间隔。可以降低login-bam采样器的Response Timeout值,或者抓包确认服务器是否确实延迟返回响应。
  • 自定义TCP客户端实现:默认的BinaryTCPClientImpl可能存在隐性等待逻辑,可编写自定义TCP客户端类,发送login-bam请求后不等待响应直接发送heatbeat请求(需确认业务允许这种无等待的顺序发送)。
  • 调整JMeter线程属性:在jmeter.properties中设置jmeter.sampler.thread.stopwait=0,减少线程切换的等待时间;同时确认tcp.nodelay=true已在全局属性中生效。
  • 合并请求发送:如果业务逻辑允许,将login-bam和heatbeat的请求数据合并为一个TCP包发送,彻底消除两个请求之间的间隔。

内容的提问来源于stack exchange,提问作者zhoutian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 12:07:09