use of com.tvd12.ezyfoxserver.statistics.EzyNetworkStats in project ezyfox-server by youngmonkeys.
the class EzySimpleStatisticsTest method test.
public void test(int max) throws Exception {
EzySimpleStatistics statistics = new EzySimpleStatistics();
EzySocketStatistics socketStats = statistics.getSocketStats();
EzyNetworkStats networkStats = (EzyNetworkStats) socketStats.getNetworkStats();
AtomicInteger count = new AtomicInteger();
Thread[] threads = new Thread[max];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(() -> {
networkStats.addReadBytes(50);
networkStats.addReadPackets(1);
networkStats.addWrittenBytes(2 * 50);
networkStats.addWrittenPackets(2);
count.incrementAndGet();
});
}
for (Thread thread : threads) {
thread.start();
}
while (count.get() < max) {
EzyThreads.sleep(5L);
}
Thread.sleep(100);
System.out.println("getReadBytes: " + networkStats.getReadBytes());
System.out.println("getReadPackets: " + networkStats.getReadPackets());
System.out.println("getWrittenBytes: " + networkStats.getWrittenBytes());
System.out.println("getWrittenPackets: " + networkStats.getWrittenPackets());
System.out.println(networkStats.getReadBytes());
System.out.println(networkStats.getReadPackets());
System.out.println(networkStats.getWrittenBytes());
System.out.println(networkStats.getWrittenPackets());
assert statistics.getStartTime() > 0;
assert statistics.getWebSocketStats() != null;
}
use of com.tvd12.ezyfoxserver.statistics.EzyNetworkStats in project ezyfox-server by youngmonkeys.
the class EzyAbstractHandlerGroupTest method test.
@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
ExHandlerGroup group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzyChannel channelX = mock(EzyChannel.class);
MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, null).invoke();
MethodInvoker.create().object(group).method("canWriteBytes").param(EzyChannel.class, channelX).invoke();
sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
when(sessionTicketsRequestQueues.addRequest(any())).thenReturn(false);
group = (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
MethodInvoker.create().object(group).method("handleReceivedData").param(Object.class, EzyEntityFactory.newArrayBuilder().append(EzyCommand.PING.getId()).append(EzyEntityFactory.EMPTY_OBJECT).build()).param(int.class, 100).invoke();
((EzyDatagramChannelPoolAware) session).setDatagramChannelPool(new EzyDatagramChannelPool(1));
((EzyUdpClientAddressAware) session).setUdpClientAddress(new InetSocketAddress("127.0.0.1", 12348));
EzyPacket packet = mock(EzyPacket.class);
when(packet.getData()).thenReturn(new byte[] { 1, 2, 3 });
ByteBuffer buffer = ByteBuffer.allocate(100);
MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, buffer).invoke();
MethodInvoker.create().object(group).method("executeSendingPacket").param(EzyPacket.class, packet).param(Object.class, new Object()).invoke();
Asserts.assertNotNull(group.getSession());
}
use of com.tvd12.ezyfoxserver.statistics.EzyNetworkStats in project ezyfox-server by youngmonkeys.
the class EzyAbstractHandlerGroupTest method newHandlerGroup.
@SuppressWarnings("rawtypes")
private ExHandlerGroup newHandlerGroup() throws Exception {
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzySimpleSession session = mock(EzySimpleSession.class);
EzySessionManager sessionManager = mock(EzySessionManager.class);
server.setSessionManager(sessionManager);
EzyResponseApi responseApi = mock(EzyResponseApi.class);
server.setResponseApi(responseApi);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = mock(EzySessionTicketsRequestQueues.class);
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
return (ExHandlerGroup) new ExHandlerGroup.Builder().session(session).decoder(decoder).sessionCount(new AtomicInteger()).networkStats((EzyNetworkStats) statistics.getSocketStats().getNetworkStats()).sessionStats((EzySessionStats) statistics.getSocketStats().getSessionStats()).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
}
Aggregations