你好,我有一个用于文字游戏的 websocket 服务器。服务器工作正常,我将它与 JS 客户端一起使用,也与 Postman 一起使用。目前我正在尝试使用 Gattle Websocket API 执行负载测试,但没有成功。
连接到服务器后,它会发送一条消息,提示进行选择(1 或 2)。我想做的是接收该消息并立即发送“2”并保持连接挂起。
模拟代码如下所示:
package computerdatabase;
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
public class WebsocketSimulation extends Simulation {
HttpProtocolBuilder httpProtocol = http
.wsBaseUrl("ws://localhost:5000");
ScenarioBuilder scene = scenario("Websocket")
.exec(ws("connect")
.connect("")
.await(30).on(ws.checkTextMessage("checkMessage")
.check(regex(".*?").saveAs("message")))
)
.exec(session -> {
System.out.println("response: " + session.get("message"));
return session;
})
.exec(ws("sendMessage").sendText("2"))
.pause(30)
.exec(ws("Close WS").close());
{
setUp(scene.injectOpen(
atOnceUsers(1))
.protocols(httpProtocol));
}
}
在 logback.xml 中设置
<root level="DEBUG">
,表明消息是在 Gattle 中接收的,但是 session.get("response")
不返回任何内容。虽然日志显示包含 2 的消息已发送,但服务器从未收到它。
加特林的日志:
03:51:01.522 [DEBUG] i.g.h.a.w.WsListener - Received response to WebSocket CONNECT: 101 Switching Protocols DefaultHttpHeaders[Upgrade: websocket, Connection: Upgrade, Sec-WebSocket-Accept: Bhe1yEfgK701lLdh9BK4+ZVDr4k=, Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover; server_max_window_bits=12; client_max_window_bits=12, Date: Tue, 29 Oct 2024 01:51:01 GMT, Server: Python/3.11 websockets/13.1, content-length: 0]
03:51:01.527 [DEBUG] i.g.h.a.w.f.WsConnectingState - Connected, performing checks before proceeding (no onConnected action)
03:51:01.528 [DEBUG] i.g.h.a.w.f.WsFsm - Timeout 1437022170 scheduled
03:51:03.529 [DEBUG] i.g.h.c.i.WebSocketHandler - Read msg=TextWebSocketFrame(data: CompositeByteBuf(ridx: 0, widx: 895, cap: 895, components=1))
03:51:03.533 [DEBUG] i.g.h.a.w.f.WsPerformingCheckState - Received matching message <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">
.ansi2html-content { display: inline; white-space: pre-wrap; word-wrap: break-word; }
.body_foreground { color: #AAAAAA; }
.body_background { background-color: #000000; }
.inv_foreground { color: #000000; }
.inv_background { background-color: #AAAAAA; }
.ansi1 { font-weight: bold; }
.ansi2 { font-weight: lighter; }
.ansi37 { color: #F5F1DE; }
</style>
</head>
<body class="body_foreground body_background" style="font-size: normal;" >
<pre class="ansi2html-content">
<span class="ansi2"></span><span class="ansi1"></span><span class="ansi1 ansi37">Welcome to the Script!
Which mode would you like to play?
1.Single Player
2.Multi Player</span>
</pre>
</body>
</html>
03:51:03.544 [DEBUG] i.g.h.a.w.f.WsPerformingCheckState - Current check success
03:51:03.544 [DEBUG] i.g.h.a.w.f.WsPerformingCheckState - Current check sequence complete
03:51:03.544 [DEBUG] i.g.h.a.w.f.WsFsm - Timeout 1437022170 cancelled
03:51:03.544 [DEBUG] i.g.h.a.w.f.WsPerformingCheckState - Check sequences completed successfully
response:
03:51:03.548 [DEBUG] i.g.h.a.w.WsSendTextFrame - Sending text frame 2 with WebSocket '<function1>': Scenario 'Websocket', UserId #1
03:51:03.550 [DEBUG] i.g.h.a.w.f.WsIdleState - Send text frame sendMessage 2
03:51:03.550 [DEBUG] i.g.h.c.i.WebSocketHandler - ctx.write msg=TextWebSocketFrame(data: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 1, cap: 64))
03:51:03.553 [DEBUG] i.g.c.a.Pause - Pausing for 30000ms
================================================================================
2024-10-29 01:51:21 GMT 20s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=3 KO=0 )
> connect (OK=1 KO=0 )
> checkMessage (OK=1 KO=0 )
> sendMessage (OK=1 KO=0 )
---- Websocket -----------------------------------------------------------------
[--------------------------------------------------------------------------] 0%
waiting: 0 / active: 1 / done: 0
================================================================================
03:51:21.517 [DEBUG] i.g.h.c.i.WebSocketHandler - Read msg=PingWebSocketFrame(data: PooledUnsafeDirectByteBuf(ridx: 0, widx: 4, cap: 4))
================================================================================
2024-10-29 01:51:31 GMT 30s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=3 KO=0 )
> connect (OK=1 KO=0 )
> checkMessage (OK=1 KO=0 )
> sendMessage (OK=1 KO=0 )
---- Websocket -----------------------------------------------------------------
[--------------------------------------------------------------------------] 0%
waiting: 0 / active: 1 / done: 0
================================================================================
03:51:33.556 [DEBUG] i.g.h.a.w.WsClose - Closing websocket '<function1>': Scenario 'Websocket', UserId #1
03:51:33.558 [DEBUG] i.g.h.a.w.f.WsIdleState - Client requested WebSocket close
03:51:33.559 [DEBUG] i.g.h.a.w.f.WsFsm - Timeout 2061203621 scheduled
03:51:33.560 [DEBUG] i.g.h.c.i.WebSocketHandler - ctx.write msg=CloseWebSocketFrame(data: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 5, cap: 64))
03:51:33.565 [DEBUG] i.g.h.c.i.WebSocketHandler - Read msg=CloseWebSocketFrame(data: PooledUnsafeDirectByteBuf(ridx: 0, widx: 5, cap: 5))
03:51:33.570 [DEBUG] i.g.h.a.w.f.WsFsm - Timeout 2061203621 cancelled
03:51:33.571 [DEBUG] i.g.h.a.w.f.WsClosingState - Server has acked closing: 1000/Bye
03:51:33.573 [DEBUG] i.g.c.a.Exit - End user #1
03:51:33.578 [DEBUG] i.g.c.c.i.Injector - End user #Websocket
03:51:33.579 [DEBUG] i.g.h.c.i.WebSocketHandler - channelInactive
03:51:33.579 [INFO ] i.g.c.c.i.Injector - All users of scenario Websocket are stopped
03:51:33.582 [INFO ] i.g.c.c.i.Injector - Stopping
03:51:33.583 [INFO ] i.g.c.c.Controller - Injector has stopped, initiating graceful stop
================================================================================
2024-10-29 01:51:33 GMT 32s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=4 KO=0 )
> connect (OK=1 KO=0 )
> checkMessage (OK=1 KO=0 )
> sendMessage (OK=1 KO=0 )
> Close WS (OK=1 KO=0 )
---- Websocket -----------------------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done: 1
================================================================================
03:51:33.598 [DEBUG] i.g.c.c.Controller - StatsEngine was stopped
03:51:33.598 [INFO ] i.g.app.Runner - Simulation computerdatabase.WebsocketSimulation completed in 32 seconds
有什么问题吗?
我对你所经历的行为感到惊讶。我下周检查一下。 但是您的正则表达式检查很奇怪,因为它不包含捕获组。如果您的目标是捕获完整的入站消息,只需使用 bodyString 检查。否则,添加捕获组。