Highlighted
Valued Contributor.
Valued Contributor.
320 views

How can I keep open a WebSocket connection?

Hi all,

I want to test how many WebSocket connections allows my application.

Recording the script LR creates one WebSocketCB.c file and the proper wss request:

web_websocket_connect("ID=0",
"URI=wss://xxxxx.net/staging-web-socket-delivery-agg/latest/notification/558/44u43lhk/websocket",
"Origin={p_url}",
"OnOpenCB=OnOpenCB0",
"OnMessageCB=OnMessageCB0",
"OnErrorCB=OnErrorCB0",
"OnCloseCB=OnCloseCB0",
LAST);

When I replay the script, connection opens but immediately it is closed:

web_websocket_connect was successful, 0 body bytes, 671 header bytes [MsgId: MMSG-26386]
Closing websocket connection of URI="wss://xxxx.net/staging-web-socket-delivery-agg/latest/notification/558/44u43lhk/websocket" 
Ending iteration 1.
Ending Vuser...

When I see the request to the WebSocket in Chrome dev tools, it has the following message of connect:

["CONNECT\ncountry:GB\nOTT:xxxx-xxxxx-xxxx-xxxx\naccept-version:1.1,1.0\nheart-beat:10000,10000\n\n\u0000"]

How can I simulate sending that message with LR?

Thanks in advance.

Regards,

0 Likes
17 Replies
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

@Juan FrancoIs the websocket immediately closed or due to end of iteration?

I expect the latter. You might try to change your run time settings and remove 'simulate new user at each iteration'. (or test with a lr_think_time() after your web_websocket_connect() call and monitor the logs).

Note that there is a web_websocket_close() call that you might like to out in your vuser_end.

Signature:
Reward community members who take time to respond and help.
Highlighted
Valued Contributor.
Valued Contributor.

Re: How can I keep open a WebSocket connection?

@JHF Remmelzwaal  Thanks for your reply.

I've  removed 'simulate new user at each iteration' option in runtime settings and commented the  //"OnCloseCB=OnCloseCB0" 

I'm not very sure of what I'm doing. The request seems to be working, the connection is opened but it is closes almost inmediatly.

I've attached the extended log:

 Start connection: uri={websocket_request}, connection handle=155018688 [MsgId: MMSG-26000]
t=16079ms: Connecting [6] to host 10.35.93.17:443 [MsgId: MMSG-26000]
t=16117ms: Connected socket [6] from 10.34.142.237:14666 to 10.35.93.17:443 in 38 ms [MsgId: MMSG-26000]
t=16117ms: Trying to set SNI with servername WS_url [MsgId: MMSG-26000]
t=16117ms: Setting SNI was succesfull [MsgId: MMSG-26000]
t=16205ms: 616-byte request headers for "{websocket_request}" (RelFrameId=1, Internal ID=26)
GET /staging-web-socket-delivery-agg/latest/notification/558/44u43lhk/websocket HTTP/1.1\r
\n
Connection: Upgrade\r\n
Sec-WebSocket-Key: cljzkudwXzjteU3OzF0gXw==\r\n
Sec-WebSocket-Version: 13\r\n
Upgrade: websocket\r\n
Origin: https://www-URL\r\n
Referer: https://www-URL/sso/login?service=https%3A%2F%2Fwww-URL.n
et%2fbreclient\r\n
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Geck
o) Chrome/81.0.4044.129 Safari/537.36\r\n
Accept-Encoding: gzip, deflate\r\n
Accept-Language: en-US,en;q=0.9\r\n
Accept: */*\r\n
Connection: Keep-Alive\r\n
Host: WS_url\r\n
\r\n
t=16309ms: 671-byte response headers for "{websocket_request}" (RelFrameId=1, Internal ID=26)
HTTP/1.1 101 \r\n
Connection: upgrade\r\n
Vary: Origin\r\n
Vary: Access-Control-Request-Method\r\n
Vary: Access-Control-Request-Headers\r\n
Access-Control-Allow-Origin: https://www-URL\r\n
Access-Control-Allow-Credentials: true\r\n
Upgrade: websocket\r\n
Sec-WebSocket-Accept: 1jQlgdClCOmeiZfLzz+ia8Fc8yE=\r\n
X-Content-Type-Options: nosniff\r\n
X-XSS-Protection: 1; mode=block\r\n
Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\n
Pragma: no-cache\r\n
Expires: 0\r\n
Strict-Transport-Security: max-age=31536000 ; includeSubDomains\r\n
X-Frame-Options: DENY\r\n
Date: Mon, 18 May 2020 12:33:31 GMT\r\n
X-Kong-Upstream-Latency: 63\r\n
X-Kong-Proxy-Latency: 0\r\n
Via: kong/1.3-enterprise-edition\r\n
\r\n
Connection Opened: uri={websocket_request}, connection handle=155018688 [MsgId: MMSG-26000]
t=16401ms: 3-byte response body for "{websocket_request}" (RelFrameId=1, Internal ID=26)
\x81\x01o
Get Message from server: uri={websocket_request}, connection handle=155018688, message=o, length=1 [MsgId: MMSG-26000]
Retaining websocket connection of URI="{websocket_request}" (RelFrameId=1, Internal ID=26) [MsgId: MMSG-27632]
Registered web_reg_find successful for "Text=101" (count=1) [MsgId: MMSG-26364]
web_websocket_connect was successful, 3 body bytes, 671 header bytes [MsgId: MMSG-26386]
Notify: Parameter Substitution: parameter "p_enableTransactionName" = "1"
Notify: Transaction "transaction_WebSocket" ended with a "Pass" status (Duration: 0.7392 Wasted Time: 0.3244).
Ending action PushNotifications.
Warning -27266: Pending Web requests detected at the end of iteration and will be aborted [MsgId: MWAR-27266]
Closing websocket connection of URI="{websocket_request}" (RelFrameId=1, Internal ID=26) [MsgId: MMSG-27631]
t=16866ms: Closed connection [6] to WS_url:443 after completing 0 request(s) [MsgId: MMSG-26000]
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Calling Vuser script function "OnOpenCB0", specified via the "OnOpenCB" argument of "web_websocket_connect" in file[MsgId: MMSG-26419]
Returned (rc=0) from Vuser script function "OnOpenCB0", specified via the "OnOpenCB" argument of web_websocket_connect in file[MsgId: MMSG-26418]
Calling Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of "web_websocket_connect" in file[MsgId: MMSG-26419]
Returned (rc=0) from Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of web_websocket_connect in file[MsgId: MMSG-26418]
Notify: Parameter Substitution: parameter "p_transactionType" = "GUI_QapterClaimsNewUI_"
Notify: Parameter Substitution: parameter "p_enableTransactionName" = "1"
Notify: Transaction "GUI_QapterClaimsNewUI_Logout" started.
Notify: Saving Parameter "p_time_now = 1589805212612".
web_url("logout") started [MsgId: MMSG-26355]

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

@Juan Franco,

 

I noticed that you jump after one iteration directly to vuser_end script. So you might do:

Runtime Settings -> Run Logic -> Number of iterations =  100

Runtime Settings -> Think Time -> Replay think time as recorded

 

Also add to the end your Action script/function a series of  lr_think_time(5); calls (e.g. 5 times.

Now observe your logging again when the Close happens.

Signature:
Reward community members who take time to respond and help.
Highlighted
Valued Contributor.
Valued Contributor.

Re: How can I keep open a WebSocket connection?

Thanks @JHF Remmelzwaal 

I've made these changes

web_websocket_connect("ID=0",
"URI=wss://xxxxxxxxxxx",
"Origin={p_url}",
"OnOpenCB=OnOpenCB0",
"OnMessageCB=OnMessageCB0",
"OnErrorCB=OnErrorCB0",
"OnCloseCB=OnCloseCB0",
LAST);
lr_think_time(500);

and connection seems to keep open more time but suddenly are being closed due to websocket inactivity:

Closed connection [5] to xxxxxx.net:443 after completing 1 request(s) [MsgId: MMSG-26000]
 Inactive socket [4] was closed by xxxxxxxxxx.net:443, probably due to Keep-Alive timeout [MsgId: MMSG-26000]

Also I'm getting this error in every request:

Error -35164: The WebSocket ID = "0" already exists [MsgId: MERR-35164] 

do you know what should I do to avoid this error?

thanks in advance

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

@Juan Franco, when you do not use a socket, it will like be closed by the underlying subsystem. Check out the Run Time Settings.

In the first iteration you should open it, and then in each iteration you should do something with it.

An lr_think_time(500) is quite long and far beyond I suggested.

You should do something like with multiple iterations (might contain errors, did not validate the code):

int init = 0;

action()
{
  int waits;
  if(!init) {
    init++;
    // only do it once
    web_websocket_connect( ....
  }
  lr_think_time(5);
  // Use the socket and send something
  web_websocket_...(....);
  // When you expect that only something is send wait for it, but not in 
  // huge lr_think_time(500) (however I might be wrong about this)
  waits = 10;
  while(waits--) {
    lr_think_time(10);
  }
}

 

Signature:
Reward community members who take time to respond and help.
Highlighted
Valued Contributor.
Valued Contributor.

Re: How can I keep open a WebSocket connection?

Hi @JHF Remmelzwaal 

After doing some operations with websocket connection, I have another issue... connection is closed from the server after sending a connect request. Here I've attached my request and the output response:

web_websocket_connect("ID=0",
"URI=wss://xxxxxxxxx",
"Origin={p_url}",
"OnOpenCB=OnOpenCB0",
"OnMessageCB=OnMessageCB0",
"OnErrorCB=OnErrorCB0",
"OnCloseCB=OnCloseCB0",
LAST);

lr_think_time(5);

web_websocket_send("ID=0",
"Buffer=CONNECT\ncountry:US\nOTT:{p_ott}\naccept-version:1.1,1.0\nheart-beat:10000,10000\n\n\u0000",
"IsBinary=0",
LAST);

lr_think_time(5);

web_websocket_send("ID=0",
"Buffer=SUBSCRIBE\nid:sub-0\ndestination:/user/queue/notification\n\n\u0000",
"IsBinary=0",
LAST);

lr_think_time(5);

Output response:

Connection Opened: uri=, connection handle=436957472 [MsgId: MMSG-26000]
Retaining websocket connection of URI="" (RelFrameId=1, Internal ID=26) [MsgId: MMSG-27632]
web_websocket_connect was successful, 0 body bytes, 671 header bytes [MsgId: MMSG-26386]
lr_think_time: 5.00 seconds.
Calling Vuser script function "OnOpenCB0", specified via the "OnOpenCB" argument of "web_websocket_connect" in .c(26) [MsgId: MMSG-26419]
Returned (rc=0) from Vuser script function "OnOpenCB0", specified via the "OnOpenCB" argument of web_websocket_connect in .c(26) [MsgId: MMSG-26418]
t=8075ms: 3-byte response body for "" (RelFrameId=1, Internal ID=26) [issued at .c(35)]
\x81\x01o
Get Message from server: uri=, connection handle=436957472, message=o, length=1 [MsgId: MMSG-26000]
Calling Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of "web_websocket_connect" in .c(26) [MsgId: MMSG-26419]
Returned (rc=0) from Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of web_websocket_connect in .c(26) [MsgId: MMSG-26418]
web_websocket_send started [MsgId: MMSG-26355]
Retaining websocket connection of URI="" (RelFrameId=1, Internal ID=26) [MsgId: MMSG-27632]
Notify: Parameter Substitution: parameter "p_ott" = "xxxxxx-xxxxxx-xxxxxxx"
SendMessage: message=CONNECT
country:GB
OTTxxxxxx-xxxxxxx-xxxxxxx
accept-version:1.1,1.0
heart-beat:10000,10000

u0000, len = 112 isBinary = 0 [MsgId: MMSG-26000]
t=12630ms: 118-byte request body for "" (RelFrameId=1, Internal ID=26) [issued at .c(37)]
\x81\xF0N3\xFF\xFF\r|\xB1\xB1\x0Bp\xAB\xF5-\\\x8A\x91:A\x86\xC5\tq\xF5\xB0\x1Ag\xC5\xCF}w\xBE
\xBD\x7Fp\xBE\xD2}\n
\xC6\xBAcw\xCB\xBE\x7F\x1E\xCA\xC9|\x00\xD2\xCA|\x06\xC7\xCCy\n
\xBE\xBB}\x04\xBA\xF5/P\x9C\x9A>G\xD2\x89+A\x8C\x96!]\xC5\xCE`\x02\xD3\xCE`\x03\xF5\x97+R\x8D
\x8BcQ\x9A\x9E:\t\xCE\xCF~\x03\xCF\xD3\x7F\x03\xCF\xCF~9\xF5\x8A~\x03\xCF\xCF
Retaining websocket connection of URI="" (RelFrameId=1, Internal ID=26) [MsgId: MMSG-27632]
web_websocket_send was successful [MsgId: MMSG-26392]
lr_think_time: 5.00 seconds.
t=13077ms: 12-byte response body for "" (RelFrameId=1, Internal ID=26) [issued at .c(42)]
\x81\n
c[1007,""]
Get Message from server: uri=, connection handle=436957472, message=c[1007,""], length=10 [MsgId: MMSG-26000]
t=13077ms: 4-byte response body for "" (RelFrameId=1, Internal ID=26) [issued at .c(42)]
\x88\x02\x03\xEF
Calling Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of "web_websocket_connect" in .c(26) [MsgId: MMSG-26419]
Returned (rc=0) from Vuser script function "OnMessageCB0", specified via the "OnMessageCB" argument of web_websocket_connect in .c(26) [MsgId: MMSG-26418]
t=13078ms: 8-byte request body for "" (RelFrameId=1, Internal ID=26) [issued at .c(42)]
\x88\x82\xB1i2\x8B\xB2\x86
t=13122ms: Server xxxxxxxxxxxxxx has shut down the connection [6] [MsgId: MMSG-26000]
t=13122ms: Closed connection [6] to kong-dev.chdc20.axadmin.net:443 after completing 0 request(s) [MsgId: MMSG-26000]
Connection Closed: uri=, connection handle=436957472 [MsgId: MMSG-26000]
Calling Vuser script function "OnCloseCB0", specified via the "OnCloseCB" argument of "web_websocket_connect" in .c(26) [MsgId: MMSG-26419]
WebSocket ID = 0 closed. CloseCode= 1007, CloseReason= [Time:2020-05-20 16:52:43]
Returned (rc=0) from Vuser script function "OnCloseCB0", specified via the "OnCloseCB" argument of web_websocket_connect in .c(26) [MsgId: MMSG-26418]
t=13577ms: Request done "" [MsgId: MMSG-26000]
web_websocket_send started [MsgId: MMSG-26355]
Warning -35046: The WebSocket whose ID is "0" is not connected [MsgId: MWAR-35046]
web_websocket_send highest severity level was "warning" [MsgId: MMSG-26391]
lr_think_time: 5.00 seconds.
Ending action .
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.

 

In my app logs in can see some errors like this:

 [OUT] {"message":"Broken data received. Terminating WebSocket connection abruptly","exceptionInfo":{"className":"com.fasterxml.jackson.core.JsonParseException","message":"Unrecognized token 'CONNECT': was expecting ('true', 'false' or 'null')


Any idea about this error?

 

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

@Juan Franco,

The error you represent is a native protocol error. You should talk to your stakeholders about protocol.

The error you show:

[OUT] {"message":"Broken data received. Terminating WebSocket connection abruptly","exceptionInfo":{"className":"com.fasterxml.jackson.core.JsonParseException","message":"Unrecognized token 'CONNECT': was expecting ('true', 'false' or 'null')


has the name JSON Parse error. Your message that you send is not JSON, it looks like it is expecting json.

This is beyond the scope of LR as stated above.

Signature:
Reward community members who take time to respond and help.
Highlighted
Valued Contributor.
Valued Contributor.

Re: How can I keep open a WebSocket connection?

Hi @JHF Remmelzwaal 

Thanks for your help. You were right. data inside Buffer needed to be escaped and adapted.

Now mi script is working until it ends an iteration. At that moment I get a warning message and immediately connection is closed:

Warning -27266: Pending Web requests detected at the end of iteration and will be aborted [MsgId: MWAR-27266]
Closing websocket connection of URI="wss:xxxxxxxx/websocket" (RelFrameId=1, Internal ID=50) [MsgId: MMSG-27631]
t=211203ms: Closed connection [1] to xxxxxxx.net:443 after completing 0 request(s) [MsgId: MMSG-26000]
Connection Closed: uri=ws:xxxxxxxxx/websocket, connection handle=428880536 [MsgId: MMSG-26000]
Calling Vuser script function "OnCloseCB0", specified via the "OnCloseCB" argument of "web_websocket_connect" in xxxxxxx.c(26) [MsgId: MMSG-26419]
WebSocket ID = 12 closed. CloseCode= 1006, CloseReason= [Time:2020-05-21 17:57:25]
Returned (rc=0) from Vuser script function "OnCloseCB0", specified via the "OnCloseCB" argument of web_websocket_connect in xxxxxxx.c(26) [MsgId: MMSG-26418]
Ending iteration 13.
Starting iteration 14.

In runtime settings I have this config:

2020-05-21_18h06_01.png

2020-05-21_18h05_48.png

2020-05-21_18h05_40.png2020-05-21_18h04_56.png

 

Do you know what should I change?

Thanks in advance

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

@Juan Franco,

You should make a study about all those settings. You might start disable 'Simulate a new user on each iteration'. A new user should never inherit a socket connection of an other user right?

Success,

Erik 

Edit: Added the word 'disable' to clarify.

Signature:
Reward community members who take time to respond and help.
0 Likes
Highlighted
Valued Contributor.
Valued Contributor.

Re: How can I keep open a WebSocket connection?

Thanks @JHF Remmelzwaal 

I want to use only one user opening different websocket connections and keep it open while iterating in action to open new sessions with the same user.

When I repeat "manually" these operations inside Actions, it works and connections keeps opened, for example:

  web_websocket_connect("ID={p_id}", 
 "URI=wss://xxxxxxxxxxxxx
"Buffer=[\"CONNECT\"]",
 "Buffer=\"[\"SUBSCRIBE"]\"",
....

  web_websocket_connect("ID={p_id}", 
 "URI=wss://xxxxxxxxxxxxx
"Buffer=[\"CONNECT\"]",
 "Buffer=\"[\"SUBSCRIBE"]\"",
....

Note that I'm repeating the same operations "copying and pasting".

My question is how to simulate that behavior using any LoadRunner option without need to copy and paste or create a for loop.

If there is not any option for that I'll consider to create a for loop to repeat those operations...

Thanks in advance

0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How can I keep open a WebSocket connection?

You state "I want to use only one user opening different websocket connections and keep it open while iterating in action to open new sessions with the same user". Can you be more precise? What is the relation between websocket-connections and sessions?

You might try to explain in more details and at a higher level what and how you want to test. Your question is not clear enough to me.

Signature:
Reward community members who take time to respond and help.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.