Skip to content

Commit

Permalink
Issue #5122 - add specialized Connection.Listener for WebSocket Conne…
Browse files Browse the repository at this point in the history
…ctionStatistics

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Aug 6, 2020
1 parent f416d87 commit be429f0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 35 deletions.
6 changes: 6 additions & 0 deletions jetty-websocket/jetty-websocket-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,11 @@
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.eclipse.jetty.websocket.tests;

import java.lang.management.ManagementFactory;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
Expand All @@ -28,7 +29,7 @@
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
Expand All @@ -41,8 +42,9 @@
import org.eclipse.jetty.websocket.common.WebSocketFrame;
import org.eclipse.jetty.websocket.common.frames.TextFrame;
import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.eclipse.jetty.websocket.common.util.WebSocketConnectionStatistics;
import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -53,36 +55,23 @@

public class WebSocketStatsTest
{
public static class MyWebSocketServlet extends WebSocketServlet
{
@Override
public void configure(WebSocketServletFactory factory)
{
factory.setCreator((req, resp) -> new EchoSocket());
}
}

private final CountDownLatch wsConnectionClosed = new CountDownLatch(1);
private Server server;
private ServerConnector connector;
private WebSocketClient client;
private ConnectionStatistics statistics;
private CountDownLatch wsUpgradeComplete = new CountDownLatch(1);
private CountDownLatch wsConnectionClosed = new CountDownLatch(1);

@BeforeEach
public void start() throws Exception
{
statistics = new ConnectionStatistics()
statistics = new WebSocketConnectionStatistics()
{
@Override
public void onClosed(Connection connection)
{
super.onClosed(connection);

if (connection instanceof AbstractWebSocketConnection)
wsConnectionClosed.countDown();
else if (connection instanceof HttpConnection)
wsUpgradeComplete.countDown();
}
};

Expand All @@ -93,20 +82,26 @@ else if (connection instanceof HttpConnection)

ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/");
contextHandler.addServlet(MyWebSocketServlet.class, "/testPath");
NativeWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
container.addMapping("/", EchoSocket.class));
WebSocketUpgradeFilter.configure(contextHandler);
server.setHandler(contextHandler);

client = new WebSocketClient();

// Setup JMX.
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbeanContainer);

server.start();
client.start();
}

@AfterEach
public void stop() throws Exception
{
client.stop();
server.stop();
client.stop();
}

long getFrameByteSize(WebSocketFrame frame)
Expand All @@ -122,22 +117,15 @@ long getFrameByteSize(WebSocketFrame frame)
@Test
public void echoStatsTest() throws Exception
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/testPath");
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/");
EventSocket socket = new EventSocket();
Future<Session> connect = client.connect(socket, uri);

final long numMessages = 10000;
final long numMessages = 1;
final String msgText = "hello world";

long upgradeSentBytes;
long upgradeReceivedBytes;

try (Session session = connect.get(5, TimeUnit.SECONDS))
{
wsUpgradeComplete.await(5, TimeUnit.SECONDS);
upgradeSentBytes = statistics.getSentBytes();
upgradeReceivedBytes = statistics.getReceivedBytes();

assertThat(statistics.getConnections(), is(1L));
for (int i = 0; i < numMessages; i++)
{
session.getRemote().sendString(msgText);
Expand All @@ -150,18 +138,18 @@ public void echoStatsTest() throws Exception
assertThat(statistics.getConnectionsMax(), is(1L));
assertThat(statistics.getConnections(), is(0L));

assertThat(statistics.getSentMessages(), is(numMessages + 2L));
assertThat(statistics.getReceivedMessages(), is(numMessages + 2L));
// Sent and received all of the echo messages + 1 for the close frame.
assertThat(statistics.getSentMessages(), is(numMessages + 1L));
assertThat(statistics.getReceivedMessages(), is(numMessages + 1L));

WebSocketFrame textFrame = new TextFrame().setPayload(msgText);
WebSocketFrame closeFrame = new CloseInfo(socket.closeCode, socket.closeReason).asFrame();

final long textFrameSize = getFrameByteSize(textFrame);
final long closeFrameSize = getFrameByteSize(closeFrame);
final int maskSize = 4; // We use 4 byte mask for client frames

final long expectedSent = upgradeSentBytes + numMessages * textFrameSize + closeFrameSize;
final long expectedReceived = upgradeReceivedBytes + numMessages * (textFrameSize + maskSize) + closeFrameSize + maskSize;
final long expectedSent = numMessages * textFrameSize + closeFrameSize;
final long expectedReceived = numMessages * (textFrameSize + maskSize) + (closeFrameSize + maskSize);

assertThat("stats.sendBytes", statistics.getSentBytes(), is(expectedSent));
assertThat("stats.receivedBytes", statistics.getReceivedBytes(), is(expectedReceived));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.websocket.common.util;

import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection;

public class WebSocketConnectionStatistics extends ConnectionStatistics
{
@Override
public void onOpened(Connection connection)
{
if (connection instanceof AbstractWebSocketConnection)
super.onOpened(connection);
}

@Override
public void onClosed(Connection connection)
{
if (connection instanceof AbstractWebSocketConnection)
super.onClosed(connection);
}
}

0 comments on commit be429f0

Please sign in to comment.