示例#1
0
        public static void Run(int clientNumber, string clientListenPipeName, string clientSendPipeName, string dataserverListenPipeName, string dataserverSendPipeName)
        {
            Console.WriteLine($"AppServer Sync Start {clientNumber}");

            object lockId = new object();

            NamedPipeServerSync listenToClient     = new NamedPipeServerSync(clientListenPipeName);
            NamedPipeServerSync listenToDataServer = new NamedPipeServerSync(dataserverListenPipeName);

            listenToClient.TokenReceivedEvent += (t) =>
            {
                // Start the time when the first value comes in from the first client
                if (!Program.Start.HasValue)
                {
                    Program.Start = ConsoleOutput.StartTime = DateTime.Now;
                }

                lock (lockId)
                {
                    Count++;
                    t.AppServerID = Count;
                }

                ConsoleOutput.UpdateStatus("AppServer Sync: ", t, "R"); // R - Message Received

                // Key: The thread is locked until the DataServer responds
                NamedPipeClientSync.Send(dataserverSendPipeName, t);    // Blocks Thread until the message is sent to the data server

                ConsoleOutput.UpdateStatus("AppServer Sync: ", t, "D"); // D - Waiting for DataServer (DataServer purposefully delays)
            };

            listenToDataServer.TokenReceivedEvent += (t) =>
            {
                ConsoleOutput.UpdateStatus("AppServer Sync: ", t, "C"); // C - Respond to client
                NamedPipeClientSync.Send(clientSendPipeName, t);        // Blocks Thread until the message is sent to the client
                ConsoleOutput.UpdateStatus("AppServer Sync: ", t, "F"); // F - Finished
            };


            Task[] listenTasks = new Task[2];

            listenTasks[0] = Task.Run(() => listenToDataServer.Start());
            listenTasks[1] = Task.Run(() => listenToClient.Start());

            Task.WaitAll(listenTasks);

            //Console.WriteLine($"AppServer Sync End {clientNumber}");
        }
示例#2
0
        static async Task Main(string[] args)
        {
            if (args.Length != 3)
            {
                throw new Exception("Invalid number of command line arguments");
            }

            var    numToSend      = int.Parse(args[0]);
            var    clientNumber   = int.Parse(args[1]);
            string uniquePipeName = args[2];

            var sendPipeName   = NamedPipeClientSync.ClientToAppServerPipeName(clientNumber, uniquePipeName);
            var listenPipeName = NamedPipeClientSync.AppServerToClientPipeName(clientNumber, uniquePipeName);

            try
            {
                await PerfClient.RunAsync(numToSend, sendPipeName, listenPipeName);
            }
            catch (Exception ex)
            {
                File.AppendAllLines($@"..\Results.txt", new string[] { $"Client Error {sendPipeName} [{ex?.Message}]" });
                await Task.Delay(TimeSpan.FromDays(1)); // Hang and PowerShell script will Kill the process
            }
        }
示例#3
0
        static async Task Main(string[] args)
        {
            if (args.Length != 2)
            {
                throw new Exception("Invalid number of command line args");
            }

            int    clientID       = int.Parse(args[0]);
            string uniquePipeName = args[1];

            var listenPipeName = NamedPipeClientSync.AppServerToDataServerPipeName(clientID, uniquePipeName);
            var sendPipeName   = NamedPipeClientSync.DataServerToAppServerPipeName(clientID, uniquePipeName);

            Console.WriteLine($"DataServer Start: {listenPipeName}");
            try
            {
                await PerfDataServer.RunAsync(listenPipeName, sendPipeName);
            }
            catch (Exception ex)
            {
                File.AppendAllLines($@"..\Results.txt", new string[] { $"DataServer Error {sendPipeName} [{ex?.Message}]" });
                await Task.Delay(TimeSpan.FromDays(1)); // Hang and PowerShell script will Kill the process
            }
        }
        static async Task Main(string[] args)
        {
            if (!(3, 4).Contains(args.Length))
            {
                throw new Exception("Invalid number of command line arguments");
            }

            bool   isAsync        = args[0] == "async";
            int    numClients     = int.Parse(args[1]);
            string uniquePipeName = args[2];

            if (args.Length == 4 && int.TryParse(args[3], out var upThreads))
            {
                // NEVER USE THIS IN PRODUCTION!!!
                // Dramatically improves the performance of Sync
                // This shows that Task.Run MAY use a new thread\
                // But .NET is well aware of the cost of threads
                ThreadPool.SetMinThreads(upThreads, upThreads);
                Console.WriteLine($"Threads: {upThreads}");
            }

            try
            {
                Task[] runTasks = new Task[numClients];

                for (var i = 0; i < numClients; i++)
                {
                    var clientToAppServerPipeName     = NamedPipeClientSync.ClientToAppServerPipeName(i + 1, uniquePipeName);
                    var appServerToClientPipeName     = NamedPipeClientSync.AppServerToClientPipeName(i + 1, uniquePipeName);
                    var appServerToDataServerPipeName = NamedPipeClientSync.AppServerToDataServerPipeName(i + 1, uniquePipeName);
                    var dataServerToAppServerPipeName = NamedPipeClientSync.DataServerToAppServerPipeName(i + 1, uniquePipeName);
                    var clientNum = i + 1;

                    if (!isAsync)
                    {
                        runTasks[i] = Task.Run(() => PerfAppServerSync.Run(clientNum, clientToAppServerPipeName, appServerToClientPipeName, dataServerToAppServerPipeName, appServerToDataServerPipeName));
                    }
                    else
                    {
                        runTasks[i] = PerfAppServerAsync.RunAsync(clientNum, clientToAppServerPipeName, appServerToClientPipeName, dataServerToAppServerPipeName, appServerToDataServerPipeName);
                    }
                }

                if (isAsync)
                {
                    await Task.WhenAll(runTasks);
                }
                else
                {
                    Task.WaitAll(runTasks);
                }

                Stop = DateTime.Now;


                // CLose the DataServer
                //            await NamedPipeClientAsync.SendAsync(NamedPipeClientSync.DataServerListenPipe(uniquePipeName), new Token(true)).ConfigureAwait(false);

                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine($"AppServer {args[0]} Completed: Clients: {numClients} Count: {(isAsync ? PerfAppServerAsync.ID : PerfAppServerSync.Count)} Elapsed Time: {(Stop.Value - Start.Value).TotalMilliseconds}");
                Console.ResetColor();

#if !DEBUG
                File.AppendAllLines($@"..\Results.txt", new string[] { $"{(isAsync ? "Async" : "Sync")} {numClients} {(isAsync ? PerfAppServerAsync.ID : PerfAppServerSync.Count)} {(Stop.Value - Start.Value).TotalMilliseconds}" });
#endif

#if DEBUG
                Console.ReadKey();
#endif
            }
            catch (Exception ex)
            {
                File.AppendAllLines($@"..\Results.txt", new string[] { $"AppServer Error {numClients} [{ex?.Message}]" });
                await Task.Delay(TimeSpan.FromDays(1)); // Hang and PowerShell script will Kill the process
            }
        }