not connecting to web socket properly in gatling - gatling

I want to connect to a web socket through gatling. But it is not working. The socket listener does not get any messsage. The code is given below.Can anyone suggest the problem? Is there any recording option for websocket in gatling. the recorder only record http requests.
`val scn = scenario("RecordedSimulation")
.exec(http("login")
.post("/user/login")
.formParam("email", "username")
.formParam("password", "*******")
.check(headerRegex("Set-Cookie", "localhost:1337.sid=(.*); Path=/").saveAs("cookie")))
.pause(5)
.exec(http("get sid")
.get("/socket.io/?EIO=3&transport=polling&t=1468496890883-0")
.headers(headers_3)
.check(regex("\"sid\":\"(.*)\",").saveAs("sid"))
)
.pause(4)
.exec(ws("Connect WebSocket").open("/socket.io/?EIO=3&transport=websocket&sid=${sid}")
.headers(Map(
"Accept-Encoding" -> "gzip, deflate, sdch",
"Accept-Language" -> "en-US,en;q=0.8",
"Pragma" -> "no-cache",
"Host" -> "localhost:1337",
"Cache-Control" -> "no-cache",
"Connection" -> "Upgrade",
"Origin" -> "http://localhost:1337",
"Sec-WebSocket-Extensions" -> "permessage-deflate; client_max_window_bits",
"Sec-WebSocket-Key" -> "sBWXugNrGCMSXmO3BEm4yw==",
"Sec-WebSocket-Version" -> "13",
"Upgrade" ->"websocket",
"Cookie" -> "io=${sid}; __cfduid=d1cf62d5cf275e2c709080ad7610da8b61465800778; cf_clearance=42068d23995e3243b3ee748ac616389d5cc27d92-1468865525-1800; _gat=1; _ga=GA1.2.1134427855.1467369017; localhost:1337.sid=${cookie}"
)))
.pause(1)
.exec(http("Run")
.post("/posturl")
.headers(headers_13)
.body(RawFileBody("RecordedSimulation_0013_request.txt")))
.exec(ws("Set Check for instance ID")
.check(wsAwait.within(30).until(1).regex("\"intInstanceID\":\"(.*-.*-.*-.*-.*)\",").saveAs("instanceID")))
.pause(1)
.exec(ws("Say Hello WS")
.sendText("""{"text": "Hello, I'm ${sid}"}"""))
.exec( session =>{
println(session("cookie").as[String])
session
})
setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}`

You don't say how your expected incoming messages are related to your other actions, but beware that we don't currently buffer unmatched incoming messages.
So if a messages arrives before you actually set a check, it's lost.
If you have some feedback about your WebSocket load test usage, please share and discuss on our mailing list. There's an ongoing effort to redesign WebSocket support for Gatling 3.

Related

Unble to open multiple simultaneous websockets to the same endpoint in case where the consumer is busy for the first websocket

I am using Django channels with React websockets. I am unable to open multiple simulataneous websockets if the consumer of the first websocket channel is busy with some activity. I want to open multiple simultaneous websockets where consumers is doing something for each individual websocket.
The module versions are:
asgiref 3.6.0
daphne 3.0.2
django-cors-headers 3.13.0
djangorestframework 3.14.0
djangorestframework-simplejwt 5.2.2
From the snippet below, once one websocket completes the task the second websocket can be connected (in a separate tab) but while 1st websocket is busy (sleeping), the other websockets in the new browser tabs fails after handshake with the following error:
**
WebSocket HANDSHAKING /ws/socket-server/monitor-rack-kpis/ushoeuhrti/ [127.0.0.1:49228]
django.channels.server INFO WebSocket HANDSHAKING /ws/socket-server/monitor-rack-kpis/ushoeuhrti/ [127.0.0.1:49228]
daphne.http_protocol DEBUG Upgraded connection ['127.0.0.1', 49228] to WebSocket
daphne.ws_protocol DEBUG WebSocket closed for ['127.0.0.1', 49228]
WebSocket DISCONNECT /ws/socket-server/monitor-rack-kpis/ushoeuhrti/ [127.0.0.1:49228]
django.channels.server INFO WebSocket DISCONNECT /ws/socket-server/monitor-rack-kpis/ushoeuhrti/ [127.0.0.1:49228]
daphne.server WARNING Application instance <Task pending name='Task-56' coro=<StaticFilesWrapper.__call__() running at /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/futures.py:387, Task.task_wakeup()]>> for connection <WebSocketProtocol client=['127.0.0.1', 49216] path=b'/ws/socket-server/monitor-rack-kpis/vhofoefwmr/'> took too long to shut down and was killed.
**
I tried using url in routing instead of re_path but that didn't help either
settings.py
`
ASGI_APPLICATION = 'backend.asgi.application'
CHANNEL_LAYERS={'default':{'BACKEND':'channels.layers.InMemoryChannelLayer'}}
`
asgi.py
application = ProtocolTypeRouter({
'http':get_asgi_application(),
'websocket':AuthMiddlewareStack(
URLRouter(logstatus.routing.websocket_urlpatterns)
)
})
routing.py
websocket_urlpatterns=[
re_path(r'^ws/socket-server/monitor-rack-kpis/(?P<username>[A-Za-z]+)/', consumers.InfluxWritePromesthusConsumer.as_asgi())
]
consumer.py
class InfluxWritePromesthusConsumer(WebsocketConsumer):
def ws_connect(message):
message.reply_channel.send({"accept": True})
def receive(self, text_data):
print(f"\nReceiving from: {self.channel_name}\n")
t = 0
while t<=100:
self.send(text_data=json.dumps({
'type':"LearnSocket",
'message': "Received Messages"
}))
t += 10
time.sleep(10)
Frontend- React.JS
const randN = generate()
console.log(randN)
const socket = new WebSocket('ws://127.0.0.1:8000/ws/socket-server/monitor-rack-kpis/'+ randN+ '/');
console.log(socket)
socket.onopen=function(e){
console.log(socket.readyState)
if (user && user.access) {
socket.send("Hi");
//Receiving response from WebSocket
socket.onmessage=function(e){
//let data1=JSON.parse(e.data);
console.log(e.data, new Date());
//setOutputResponse(data1['message']);
}
}
}

akka http with connecting http server to outgoing tcp server throught nginx

Problem when try to using akka http like proxy to tcp.
Start http server, which responsibility only to get json request from the client and send message on Tcp server through nginx, take response from it and send response back to the client. So, only like proxy.
The problem is that server try to response before tcp server response. But when I remove nginx proxy between http and tcp servers everything all right. In tcp level akka http server send RST packet and right after that get RST from nginx and connection close. Tcp respond to nginx but connection already closed. With simple tcp server the same situation.
Akka version 2.4.17, akka http 10.0.10
The code below
val connections: Flow[ByteString, ByteString,
Future[Tcp.OutgoingConnection]] =
Tcp().outgoingConnection(new InetSocketAddress(settings.tcpServerHost,
settings.tcpServerPort))
def start(): Unit ={
val requestHandler: HttpRequest => Future[HttpResponse] = {
case HttpRequest(POST, uri#Uri.Path("/v1/doc"), _, entry, _) =>
proxyTcp(100, uri, entry)
case HttpRequest(POST, uri#Uri.Path("/v5/doc"), _, entry, _) =>
proxyTcp(105, uri, entry)
case _ => Future(HttpResponse(status = StatusCodes.NotFound))
}
Http().bind(settings.host, settings.port).runForeach(connection => {
connection.handleWithAsyncHandler(requestHandler)
}) }
private def proxyTcp(version: Short, uri: Uri, entry: RequestEntity):
Future[HttpResponse] = {
val transform = Transformations(connections, version)
entry.withoutSizeLimit().dataBytes
.via(transform.json2tlv)
.via(transform.connections)
.runWith(Sink.head)
.map(result => HttpResponse(status = StatusCodes.OK, entity =
HttpEntity(ContentTypes.`application/json`, result.get)))
}

angular-sails websocket url undefined ( ws://www.undefined)

I am using sails 0.10.5 and in angular frontend with angular-sails .
It gives an error like below on server side Whenever an api call by $sails.get
In My FrontEnd side controller code :
io.sails.url = 'http://localhost:1337';
io.socket.get('http://localhost:1337/admin/v1/user/subscribe', function (body, response) {
console.log('Sails responded with: ', body);
console.log('with headers: ', response.headers);
console.log('and with status code: ', response.statusCode);
});
and console is
Sails responded with: Object {}
app.js:167 with headers: Object {}
app.js:168 and with status code: 200
But in backend side sails console gives error like this
debug: --------------------------------------------------------
debug: :: Fri Dec 19 2014 10:44:25 GMT+0530 (India Standard Time)
debug: Environment : development
debug: Port : 1337
debug: --------------------------------------------------------
error: Error:
res.redirect() :: [socket.io] ::
Cannot redirect socket to invalid location :: 'ws://www.undefinedhttp://localhost:1337/admin/v1/user/subscribe'
at Object.Err.invalidRedirect (C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\interpret.js:33:14)
at ResStream.doRedirect (C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\interpret.js:310:31)
at C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\sockets\lib\interpreter\saveSessionAndThen.js:23:7
at C:\wamp\www\i-phone\i-phone\node_modules\sails\lib\hooks\session\index.js:59:17
My route file is like this :
'get /admin/v1/user/subscribe': 'UserController.subscribe',
And in UserController.js :
subscribe: function (req, res) {
console.log('aaaaaaaaaaaaaaaaaaaa')
User.find().exec(function(err,users){
// subscribing this socket to User model classroom
User.subscribe(req.socket);
// subscribing this socket to User model instance room
User.subscribe(req.socket,users);
console.log('subscribe')
})
}
For setting up Socket connection with server:
Include sails.io.js in index.html.
Create a global variable for io.sails.url and you'll need to tell the Sails socket client what URL to connect to:
io.sails.url = 'http://localhost:1337';
Put this anywhere before you start making socket calls; the library is smart enough to wait until its connected before trying to make the calls.
To Check if Socket is connected,
io.socket.get('http://localhost:1337/data', function (body, response) {
console.log('Sails responded with: ', body);
console.log('with headers: ', sailsResponseObject.headers);
console.log('and with status code: ', sailsResponseObject.statusCode);
});
You should be able to see in the console whether or not the socket connected by looking for the "io.socket connected successfully." message.
The above code was about setting up socket connection as you requested.
Regarding your problem, Can you please provide some Code
The controller action you posted is clearly not being run (you would see the aaaaaaaaaaaaaaaaaaaa log in your console). Some other code is running and is calling res.redirect with invalid arguments. My first guess would be that you have a policy configured in config/policies.js that is attached to the UserController.subscribe action.
It was little silly mistake.
In my route.js , I am redirecting non WWW request to WWW request .
And socket request start with ws:// tag , thats why it is being also redirect to www.undefined
It is solved by a small condition
if(!req.isSocket){
// redirect logic
}else{
next();
}

How to get instance of current message bein processed in cxf?

I want to get the ID of the Inbound message in my implemented service end point which has following parameters available:
Custom JAXB Request
#Context HttpServletRequest
e.g. From below inbound message i want to retrieve ID: 1 in my service endpoint.
INFO: Inbound Message
ID: 1
Address:
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/xml
Headers:
Payload:
Can anyone please tell me if there is a way to get that ID ?
You can get the current CXF Message using PhaseInterceptorChain.getCurrentMessage(). The logging ID used by the logging interceptors is stored in the Message Map, and can be retrieved with its key, e.g.
String loggingId = (String) PhaseInterceptorChain.getCurrentMessage().get(LoggingMessage.ID_KEY);

C HTTP Proxy, the browser shows "waiting for" indefinetely

I have a hard time implementing a proxy server in C. It works for a few first webpages but then I get blocked while waiting for a new request.
Design:
Firefox -> Proxy -> Webserver --.
Firefox <- Proxy <- Webserver <-'
So each request is a round-trip from the browser to the proxy and the server and then back. Until the response from the request comes back, nothing should happen. I use no pipelining, threads or anything like that but rather only recv() and send() in a linear serial manner (for simplicity and intuition). I also don't close any sockets as I want to have a persistent connection.
I expect to be able and fetch one whole webpage including subrequested resources like css, img, js, etc..
In my implementation I manage to fetch a few first requests for a webpage. Then it hangs at step 1.
Implementation:
puts("Waiting for user to connect..");
int sock_user = accept(sock, (struct sockaddr*)NULL, NULL);
int sock_host = -1;
printf("User connected.\n");
// Accept requests
while(1){
http_request req;
http_response resp;
// 1. Client ==> Proxy Server
http_parse_request(sock_user, &req); // uses recv()
// 2. Client Proxy ==> Server
if (sock_host < 0)
sock_host = proxy_connect_host(req.header->host);
write(sock_host, req.header->raw_data, req.header->raw_size);
// 3. Client Proxy <== Server
http_parse_response(sock_host, &resp); // uses recv()
// 4. Client <== Proxy Server
write(sock_user, resp.header->raw_data, resp.header->raw_size);
write(sock_user, resp.body ->first_block->data, resp.body ->first_block->size);
}
Log:
---- ......................................... ----
---- after succesfully responded to 4 requests ----
Client ==> Proxy Server
Received 389
Client Proxy ==> Server
Sending.. 389
Sent 389
Client Proxy <== Server
Got header 312
Got body 1437
Response total 1749
Client <== Proxy Server
Sending header.. 312
Sent 312
Sending body.. 1437
Sent 1437
Client ==> Proxy Server
---- Hangs/blocks here ----
Firebug:
Wireshark:
I have no intuition as to what the reason for that block is and I have spent a whole week trying to resolve this problem without a breakthrough.
Among things tried to resolve problem:
Sending some extra CRLF for each response body
Checked the return value from each recv() and send(). (in the log above,
the values getting printed are the return values from recv and
send)
I hope someone could give at least some direction as to how to troubleshoot this or my brain will soon explode :)
You have to be very careful not to read too much data. E.g. make sure that:
header is read only up to the double CRLF; store extra data and sent them with the body
sending of body starts not before the server has sent the complete header (does not apply to this GET case but is important for POST or CONNECT)
there are received and sent only Content-Length bytes from the body
This applies to both the client -> proxy request and the server -> proxy response.
Your sample code stays in an endless loop (while (1) ...). How do you abort this? Do you honor the "Proxy-Connection" header?

Resources