Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using SuperSocket.SocketBase.Config;
- using SuperWebSocket;
- using WebSocket4Net;
- /*
- binary error test results:
- ---------------------
- Client s:394353 r:394350 rps:32767 up:5
- Server s:394352 r:394353 rps:32767
- ---------------------
- Client s:430757 r:430754 rps:36400 up:5
- Server s:430760 r:430760 rps:36405
- ---------------------
- Client s:462859 r:462855 rps:32096 up:5
- Server s:462859 r:462859 rps:32099
- Client closed and 4 still working
- Client closed and 3 still working
- Session 73e0588e-32c0-4449-b1c6-f8d481d9a93a closed
- Session 42dbf9eb-09ae-4860-bde3-317ca54da742 closed
- Client closed and 2 still working
- Session 9d02f40a-40a1-401d-96fd-466bfc83eabc closed
- Client closed and 1 still working
- Session 1fbde0f5-5559-459e-b8f4-0782ce15188f closed
- ---------------------
- Client s:473326 r:473325 rps:10464 up:1
- Server s:473325 r:473325 rps:10464
- ---------------------
- Client s:473326 r:473325 rps:0 up:1
- Server s:473325 r:473325 rps:0
- ---------------------
- Client s:473326 r:473325 rps:0 up:1
- Server s:473325 r:473325 rps:0
- ---------------------
- Client s:473326 r:473325 rps:0 up:1
- Server s:473325 r:473325 rps:0
- ---------------------
- Client s:473326 r:473325 rps:0 up:1
- Server s:473325 r:473325 rps:0
- *
- *
- *
- *
- message error test result:
- Client s:373479 r:373478 rps:26770 up:5
- Server s:373479 r:373480 rps:26772
- ---------------------
- Client s:400303 r:400300 rps:26820 up:5
- Server s:400304 r:400305 rps:26823
- Client closed and 4 still working
- Session 20bfa035-e811-4ea6-bcb3-c33a2b21a4b9 closed
- Client closed and 3 still working
- Session d90f6f07-1b9a-40d6-9946-c9dcb302c4f3 closed
- Client closed and 2 still working
- Session 28454136-a8b3-40ee-99a9-6ccdcc1c6385 closed
- Client closed and 1 still working
- Session 995e669b-b8d2-4cda-bbfc-f234ab49355b closed
- ---------------------
- Client s:416210 r:416209 rps:15903 up:1
- Server s:416209 r:416209 rps:15903
- ---------------------
- Client s:416210 r:416209 rps:0 up:1
- Server s:416209 r:416209 rps:0
- ---------------------
- Client s:416210 r:416209 rps:0 up:1
- Server s:416209 r:416209 rps:0
- ---------------------
- Client s:416210 r:416209 rps:0 up:1
- Server s:416209 r:416209 rps:0
- */
- namespace WSTest {
- class WSEchoSession : WebSocketSession<WSEchoSession> {
- }
- class WSEchoServer : WebSocketServer<WSEchoSession> {
- public int sent = 0;
- public int received = 0;
- ServerConfig config = new ServerConfig();
- public WSEchoServer()
- : base() {
- }
- public override bool Start() {
- config.Ip = "0.0.0.0";
- config.Port = 12345;
- config.MaxConnectionNumber = 1000;
- config.MaxRequestLength = 65536;
- if (Setup(config)) {
- NewMessageReceived += ws_NewMessageReceived;
- NewDataReceived += ws_NewDataReceived;
- NewSessionConnected += ws_NewSessionConnected;
- SessionClosed += ws_SessionClosed;
- return base.Start();
- } else {
- return false;
- }
- }
- void ws_SessionClosed(WSEchoSession session, SuperSocket.SocketBase.CloseReason value) {
- Console.WriteLine("Session {0} closed", session.SessionID);
- }
- void ws_NewSessionConnected(WSEchoSession session) {
- Console.WriteLine("Session {0} connected", session.SessionID);
- }
- void ws_NewDataReceived(WSEchoSession session, byte[] value) {
- Interlocked.Increment(ref received);
- session.Send(value, 0, value.Length);
- Interlocked.Increment(ref sent);
- }
- void ws_NewMessageReceived(WSEchoSession session, string value) {
- Interlocked.Increment(ref received);
- session.Send(value);
- Interlocked.Increment(ref sent);
- }
- }
- class WSEchoClient : WebSocket {
- static bool binary = false;
- Random rnd = new Random(DateTime.Now.Millisecond);
- public static int working = 0;
- public static int sent = 0;
- public static int received = 0;
- public int tests = 0;
- string uri = null;
- public WSEchoClient(string uri, int tests)
- : base(uri) {
- this.uri = uri;
- this.tests = tests;
- Opened += WSEchoClient_Opened;
- Closed += WSEchoClient_Closed;
- MessageReceived += WSEchoClient_MessageReceived;
- DataReceived += WSEchoClient_DataReceived;
- }
- public void Start() {
- Open();
- Console.WriteLine("Client started for {0}", uri);
- }
- void WSEchoClient_DataReceived(object sender, DataReceivedEventArgs e) {
- Interlocked.Increment(ref received);
- SendBinary();
- }
- void WSEchoClient_MessageReceived(object sender, MessageReceivedEventArgs e) {
- Interlocked.Increment(ref received);
- SendMessage();
- }
- void WSEchoClient_Opened(object sender, EventArgs e) {
- Interlocked.Increment(ref working);
- Console.WriteLine("Client connection opened and {0} total", working);
- if (binary) {
- SendBinary();
- } else {
- SendMessage();
- }
- }
- void WSEchoClient_Closed(object sender, EventArgs e) {
- Interlocked.Decrement(ref working);
- Console.WriteLine("Client closed and {0} still working", working);
- }
- string RandomString() {
- int size = (int)(16 + rnd.NextDouble() * 2048);
- const string dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- char[] buffer = new char[size];
- for (int i = 0; i < size; i++) {
- buffer[i] = dictionary[Convert.ToInt32(Math.Floor(dictionary.Length * rnd.NextDouble()))];
- }
- return new string(buffer);
- }
- void SendMessage() {
- if (tests-- > 0) {
- string test = RandomString();
- Send(test);
- Interlocked.Increment(ref sent);
- } else {
- Close();
- }
- }
- void SendBinary() {
- if (tests-- > 0) {
- string test = RandomString();
- byte[] data = Encoding.UTF8.GetBytes(test);
- Send(data, 0, data.Length);
- Interlocked.Increment(ref sent);
- } else {
- Close();
- }
- }
- }
- class Program {
- static int lastSSent = 0;
- static int lastCSent = 0;
- static WSEchoServer server = new WSEchoServer();
- static void StartClients(int num, int tests) {
- String uri = String.Format("ws://127.0.0.1:12345/");
- for (int a = 0; a < num; a++) {
- WSEchoClient client = new WSEchoClient(uri, tests);
- client.Start();
- }
- }
- static void Metrics() {
- Console.WriteLine("---------------------");
- Console.WriteLine("Client s:{0} r:{1} rps:{2} up:{3}", WSEchoClient.sent, WSEchoClient.received, WSEchoClient.sent - lastCSent, WSEchoClient.working);
- Console.WriteLine("Server s:{0} r:{1} rps:{2}", server.sent, server.received, server.sent - lastSSent);
- lastSSent = server.sent;
- lastCSent = WSEchoClient.sent;
- }
- static void Main(string[] args) {
- if (server.Start()) {
- Console.WriteLine("Server started");
- StartClients(5, 100000);
- while (server.State == SuperSocket.SocketBase.ServerState.Running) {
- Thread.Sleep(1000);
- Metrics();
- if (WSEchoClient.working == 0) {
- Console.WriteLine("Test complete!");
- Metrics();
- server.Stop();
- }
- }
- } else {
- Console.WriteLine("Server not started");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement