TestThroughput.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2011 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package jme3test.network;
  33. //import com.jme3.network.connection.OldClient;
  34. //import com.jme3.network.connection.OldServer;
  35. import com.jme3.network.Client;
  36. import com.jme3.network.Message;
  37. import com.jme3.network.MessageConnection;
  38. import com.jme3.network.MessageListener;
  39. import com.jme3.network.Network;
  40. import com.jme3.network.Server;
  41. //import com.jme3.network.events.MessageAdapter;
  42. //import com.jme3.network.message.Message;
  43. import com.jme3.network.serializing.Serializable;
  44. import com.jme3.network.serializing.Serializer;
  45. import java.io.IOException;
  46. public class TestThroughput implements MessageListener<MessageConnection> { //extends MessageAdapter {
  47. private static long lastTime = -1;
  48. private static long counter = 0;
  49. private static long total = 0;
  50. // private static OldClient client;
  51. private static Client client;
  52. private boolean isOnServer;
  53. public TestThroughput( boolean isOnServer ) {
  54. this.isOnServer = isOnServer;
  55. }
  56. @Override
  57. public void messageReceived( MessageConnection source, Message msg){
  58. if( !isOnServer ) {
  59. // It's local to the client so we got it back
  60. counter++;
  61. total++;
  62. long time = System.currentTimeMillis();
  63. //System.out.println( "total:" + total + " counter:" + counter + " lastTime:" + lastTime + " time:" + time );
  64. if( lastTime < 0 ) {
  65. lastTime = time;
  66. } else if( time - lastTime > 1000 ) {
  67. long delta = time - lastTime;
  68. double scale = delta / 1000.0;
  69. double pps = counter / scale;
  70. System.out.println( "messages per second:" + pps + " total messages:" + total );
  71. counter = 0;
  72. lastTime = time;
  73. }
  74. } else {
  75. if( source == null ) {
  76. System.out.println( "Received a message from a not fully connected source, msg:"+ msg );
  77. } else {
  78. //System.out.println( "sending:" + msg + " back to client:" + source );
  79. source.send(msg);
  80. }
  81. }
  82. }
  83. public static void main(String[] args) throws IOException, InterruptedException{
  84. Serializer.registerClass(TestMessage.class);
  85. //OldServer server = new OldServer(5110, 5110);
  86. //server.start();
  87. Server server = Network.createServer( 5110 );
  88. server.start();
  89. //client = new OldClient("localhost", 5110, 5110);
  90. //client.start();
  91. Client client = Network.connectToServer( "hydra", 5110, 5000 );
  92. client.start();
  93. client.addMessageListener(new TestThroughput(false), TestMessage.class);
  94. server.addMessageListener(new TestThroughput(true), TestMessage.class);
  95. Thread.sleep(1);
  96. TestMessage test = new TestMessage();
  97. // for( int i = 0; i < 10; i++ ) {
  98. while( true ) {
  99. //System.out.println( "sending." );
  100. client.send(test);
  101. }
  102. //Thread.sleep(5000);
  103. }
  104. @Serializable
  105. public static class TestMessage extends com.jme3.network.message.Message {
  106. public TestMessage(){
  107. setReliable(true);
  108. }
  109. }
  110. }