Advertisement
Guest User

Untitled

a guest
Oct 14th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using SuperSocket.SocketBase.Config;
  8. using SuperWebSocket;
  9. using WebSocket4Net;
  10.  
  11. /*
  12. binary error test results:
  13. ---------------------
  14. Client s:394353 r:394350 rps:32767 up:5
  15. Server s:394352 r:394353 rps:32767
  16. ---------------------
  17. Client s:430757 r:430754 rps:36400 up:5
  18. Server s:430760 r:430760 rps:36405
  19. ---------------------
  20. Client s:462859 r:462855 rps:32096 up:5
  21. Server s:462859 r:462859 rps:32099
  22. Client closed and 4 still working
  23. Client closed and 3 still working
  24. Session 73e0588e-32c0-4449-b1c6-f8d481d9a93a closed
  25. Session 42dbf9eb-09ae-4860-bde3-317ca54da742 closed
  26. Client closed and 2 still working
  27. Session 9d02f40a-40a1-401d-96fd-466bfc83eabc closed
  28. Client closed and 1 still working
  29. Session 1fbde0f5-5559-459e-b8f4-0782ce15188f closed
  30. ---------------------
  31. Client s:473326 r:473325 rps:10464 up:1
  32. Server s:473325 r:473325 rps:10464
  33. ---------------------
  34. Client s:473326 r:473325 rps:0 up:1
  35. Server s:473325 r:473325 rps:0
  36. ---------------------
  37. Client s:473326 r:473325 rps:0 up:1
  38. Server s:473325 r:473325 rps:0
  39. ---------------------
  40. Client s:473326 r:473325 rps:0 up:1
  41. Server s:473325 r:473325 rps:0
  42. ---------------------
  43. Client s:473326 r:473325 rps:0 up:1
  44. Server s:473325 r:473325 rps:0
  45.  
  46.  *
  47.  *
  48.  *
  49.  *
  50.  
  51. message error test result:
  52. Client s:373479 r:373478 rps:26770 up:5
  53. Server s:373479 r:373480 rps:26772
  54. ---------------------
  55. Client s:400303 r:400300 rps:26820 up:5
  56. Server s:400304 r:400305 rps:26823
  57. Client closed and 4 still working
  58. Session 20bfa035-e811-4ea6-bcb3-c33a2b21a4b9 closed
  59. Client closed and 3 still working
  60. Session d90f6f07-1b9a-40d6-9946-c9dcb302c4f3 closed
  61. Client closed and 2 still working
  62. Session 28454136-a8b3-40ee-99a9-6ccdcc1c6385 closed
  63. Client closed and 1 still working
  64. Session 995e669b-b8d2-4cda-bbfc-f234ab49355b closed
  65. ---------------------
  66. Client s:416210 r:416209 rps:15903 up:1
  67. Server s:416209 r:416209 rps:15903
  68. ---------------------
  69. Client s:416210 r:416209 rps:0 up:1
  70. Server s:416209 r:416209 rps:0
  71. ---------------------
  72. Client s:416210 r:416209 rps:0 up:1
  73. Server s:416209 r:416209 rps:0
  74. ---------------------
  75. Client s:416210 r:416209 rps:0 up:1
  76. Server s:416209 r:416209 rps:0
  77.  
  78. */
  79.  
  80. namespace WSTest {
  81.     class WSEchoSession : WebSocketSession<WSEchoSession> {
  82.     }
  83.  
  84.     class WSEchoServer : WebSocketServer<WSEchoSession> {
  85.         public int sent = 0;
  86.         public int received = 0;
  87.         ServerConfig config = new ServerConfig();
  88.         public WSEchoServer()
  89.             : base() {
  90.         }
  91.  
  92.         public override bool Start() {
  93.             config.Ip = "0.0.0.0";
  94.             config.Port = 12345;
  95.             config.MaxConnectionNumber = 1000;
  96.             config.MaxRequestLength = 65536;
  97.             if (Setup(config)) {
  98.                 NewMessageReceived += ws_NewMessageReceived;
  99.                 NewDataReceived += ws_NewDataReceived;
  100.                 NewSessionConnected += ws_NewSessionConnected;
  101.                 SessionClosed += ws_SessionClosed;
  102.                 return base.Start();
  103.             } else {
  104.                 return false;
  105.             }
  106.         }
  107.  
  108.         void ws_SessionClosed(WSEchoSession session, SuperSocket.SocketBase.CloseReason value) {
  109.             Console.WriteLine("Session {0} closed", session.SessionID);
  110.         }
  111.  
  112.         void ws_NewSessionConnected(WSEchoSession session) {
  113.             Console.WriteLine("Session {0} connected", session.SessionID);
  114.         }
  115.  
  116.         void ws_NewDataReceived(WSEchoSession session, byte[] value) {
  117.             Interlocked.Increment(ref received);
  118.             session.Send(value, 0, value.Length);
  119.             Interlocked.Increment(ref sent);
  120.         }
  121.  
  122.         void ws_NewMessageReceived(WSEchoSession session, string value) {
  123.             Interlocked.Increment(ref received);
  124.             session.Send(value);
  125.             Interlocked.Increment(ref sent);
  126.         }
  127.     }
  128.  
  129.     class WSEchoClient : WebSocket {
  130.         static bool binary = false;
  131.         Random rnd = new Random(DateTime.Now.Millisecond);
  132.         public static int working = 0;
  133.         public static int sent = 0;
  134.         public static int received = 0;
  135.         public int tests = 0;
  136.  
  137.         string uri = null;
  138.         public WSEchoClient(string uri, int tests)
  139.             : base(uri) {
  140.             this.uri = uri;
  141.             this.tests = tests;
  142.             Opened += WSEchoClient_Opened;
  143.             Closed += WSEchoClient_Closed;
  144.             MessageReceived += WSEchoClient_MessageReceived;
  145.             DataReceived += WSEchoClient_DataReceived;
  146.         }
  147.  
  148.         public void Start() {
  149.             Open();
  150.             Console.WriteLine("Client started for {0}", uri);
  151.         }
  152.  
  153.         void WSEchoClient_DataReceived(object sender, DataReceivedEventArgs e) {
  154.             Interlocked.Increment(ref received);
  155.             SendBinary();
  156.         }
  157.  
  158.         void WSEchoClient_MessageReceived(object sender, MessageReceivedEventArgs e) {
  159.             Interlocked.Increment(ref received);
  160.             SendMessage();
  161.         }
  162.  
  163.         void WSEchoClient_Opened(object sender, EventArgs e) {
  164.             Interlocked.Increment(ref working);
  165.             Console.WriteLine("Client connection opened and {0} total", working);
  166.             if (binary) {
  167.                 SendBinary();
  168.             } else {
  169.                 SendMessage();
  170.             }
  171.         }
  172.  
  173.         void WSEchoClient_Closed(object sender, EventArgs e) {
  174.             Interlocked.Decrement(ref working);
  175.             Console.WriteLine("Client closed and {0} still working", working);
  176.         }
  177.        
  178.         string RandomString() {
  179.             int size = (int)(16 + rnd.NextDouble() * 2048);
  180.             const string dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  181.             char[] buffer = new char[size];
  182.             for (int i = 0; i < size; i++) {
  183.                 buffer[i] = dictionary[Convert.ToInt32(Math.Floor(dictionary.Length * rnd.NextDouble()))];
  184.             }
  185.             return new string(buffer);
  186.         }
  187.  
  188.         void SendMessage() {
  189.             if (tests-- > 0) {
  190.                 string test = RandomString();
  191.                 Send(test);
  192.                 Interlocked.Increment(ref sent);
  193.             } else {
  194.                 Close();
  195.             }
  196.         }
  197.  
  198.         void SendBinary() {
  199.             if (tests-- > 0) {
  200.                 string test = RandomString();
  201.                 byte[] data = Encoding.UTF8.GetBytes(test);
  202.                 Send(data, 0, data.Length);
  203.                 Interlocked.Increment(ref sent);
  204.             } else {
  205.                 Close();
  206.             }
  207.         }
  208.     }
  209.  
  210.     class Program {
  211.         static int lastSSent = 0;
  212.         static int lastCSent = 0;
  213.         static WSEchoServer server = new WSEchoServer();
  214.  
  215.         static void StartClients(int num, int tests) {
  216.             String uri = String.Format("ws://127.0.0.1:12345/");
  217.             for (int a = 0; a < num; a++) {
  218.                 WSEchoClient client = new WSEchoClient(uri, tests);
  219.                 client.Start();
  220.             }
  221.         }
  222.        
  223.         static void Metrics() {
  224.             Console.WriteLine("---------------------");
  225.             Console.WriteLine("Client s:{0} r:{1} rps:{2} up:{3}", WSEchoClient.sent, WSEchoClient.received, WSEchoClient.sent - lastCSent, WSEchoClient.working);
  226.             Console.WriteLine("Server s:{0} r:{1} rps:{2}", server.sent, server.received, server.sent - lastSSent);
  227.             lastSSent = server.sent;
  228.             lastCSent = WSEchoClient.sent;
  229.         }
  230.  
  231.         static void Main(string[] args) {
  232.             if (server.Start()) {
  233.                 Console.WriteLine("Server started");
  234.                 StartClients(5, 100000);
  235.                 while (server.State == SuperSocket.SocketBase.ServerState.Running) {
  236.                     Thread.Sleep(1000);
  237.                     Metrics();
  238.                     if (WSEchoClient.working == 0) {
  239.                         Console.WriteLine("Test complete!");
  240.                         Metrics();
  241.                         server.Stop();
  242.                     }
  243.                 }
  244.             } else {
  245.                 Console.WriteLine("Server not started");
  246.             }
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement