全部产品
云市场

为什么采样日志中报java.net.SocketException: Socket closed错误?

更新时间:2019-03-29 15:22:28

引起 java.net.SocketException: Socket closed 错误的原因通常是未设置连接的超时时间。

问题现象

如果压测的是 HTTPS 接口,同时在压测进行过程中出现 RT(响应时间)逐渐变高,TPS 和成功率都有跌零或者相应的趋势。jmeter-socket-closed异常的监控图

这时,打开采样日志看到的报错如下(选择 HTTP 模板更清晰)。

jmeter-socket-closed异常的日志1

详细的文本参考如下:

  1. java.net.SocketException: Socket closed
  2. at java.net.PlainSocketImpl.socketConnect(Native Method)
  3. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  4. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  5. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  6. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  7. at java.net.Socket.connect(Socket.java:589)
  8. at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
  9. at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:542)
  10. at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:414)
  11. at org.apache.jmeter.protocol.http.sampler.LazySchemeSocketFactory.connectSocket(LazySchemeSocketFactory.java:97)
  12. at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
  13. at org.apache.jmeter.protocol.http.sampler.hc.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:318)
  14. at org.apache.jmeter.protocol.http.sampler.MeasuringConnectionManager$MeasuredConnection.open(MeasuringConnectionManager.java:114)
  15. at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
  16. at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
  17. at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
  18. at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
  19. at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:695)
  20. at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:454)
  21. at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
  22. at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1189)
  23. at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1178)
  24. at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:498)
  25. at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:424)
  26. at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:255)
  27. at java.lang.Thread.run(Thread.java:766)

查看采样日志的 Timing 页签,可以看到时间都耗费在连接建立阶段。

jmeter-socket-closed异常的瀑布模型

解决办法

该问题可以尝试通过以下方法解决。

如果在 HTTP Request Sampler 的 Basic 里勾选了 Use KeepAlive,那么建议在 Advanced 页签下:

  • Implementation 选为 HttpClient4
  • Timeouts 中的 Connect 设置一个10~60秒的值,表示连接的空闲超时时间,避免由于没收到被压测端的响应回来的 Keep-Alive 的 Header 导致的连接断开。

jmeter-socket-closed异常的解决

参考资料:https://wiki.apache.org/jmeter/JMeterSocketClosed