Esempio n. 1
0
        public void Main(string[] args)
        {
            var messageBroker = new MessageBroker(new SlowMessagePublisher());
            var messageGenerator = new MessageGenerator(messageBroker);

            // TEST: On Request 
            //     - Can register subscriber 
            //     - That they run on the same thread as the origin request 
            //     - One slow subscriber doesn't block other on request subscribers 
            messageBroker.OnRequestThread.Subscribe(message =>
            {
                if (Debug)
                    Console.WriteLine($"On Req Observable v   - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");

                // Simulate some amount of work.
                GC.KeepAlive("Hello".GetHashCode());

                if (Debug)
                    Console.WriteLine($"On Req Observable ^   - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
            });

            // TEST: Off Request 
            //     - Can register subscriber 
            //     - That they run on a different thread as the origin request 
            //     - One slow subscriber doesn't block other off request subscribers 
            //     - When needed other workers are brought in to handel the load
            messageBroker.OffRequestThread.Subscribe(async message =>
            {
                if (Debug)
                    Console.WriteLine($"Off Req Observable v  - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
                
                // Simulate some expensive async operation
                await Task.Delay(Thread.CurrentThread.ManagedThreadId % 2 == 0 ? 150 : 30);

                if (Debug)
                    Console.WriteLine($"Off Req Observable ^  - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
            });


            var stopwatch = Stopwatch.StartNew();
            messageGenerator.Start();
            
            Console.WriteLine("Press the ANY key to get a summary.");
            Console.WriteLine("Press Ctrl+C to quit.");
            while (true)
            {
                Console.ReadLine();

                var elapsed = stopwatch.Elapsed;
                Console.WriteLine($"Messages Generated: {MessagesGenerated}");
                Console.WriteLine($"Messages Sent: {MessagesSent}");
                Console.WriteLine($"Time Elapsed: {elapsed}");
                Console.WriteLine($"Requests/sec: {(double)MessagesSent / ((double)elapsed.Ticks / (double)TimeSpan.FromSeconds(1).Ticks)}");
                Console.WriteLine($"Generated/sec: {(double)MessagesGenerated / ((double)elapsed.Ticks / (double)TimeSpan.FromSeconds(1).Ticks)}");
                Console.WriteLine();
                Console.WriteLine();
            }
        }
Esempio n. 2
0
        public void Main(string[] args)
        {
            var messageBroker    = new MessageBroker(new SlowMessagePublisher());
            var messageGenerator = new MessageGenerator(messageBroker);

            // TEST: On Request
            //     - Can register subscriber
            //     - That they run on the same thread as the origin request
            //     - One slow subscriber doesn't block other on request subscribers
            messageBroker.OnRequestThread.Subscribe(message =>
            {
                if (Debug)
                {
                    Console.WriteLine($"On Req Observable v   - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
                }

                // Simulate some amount of work.
                GC.KeepAlive("Hello".GetHashCode());

                if (Debug)
                {
                    Console.WriteLine($"On Req Observable ^   - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
                }
            });

            // TEST: Off Request
            //     - Can register subscriber
            //     - That they run on a different thread as the origin request
            //     - One slow subscriber doesn't block other off request subscribers
            //     - When needed other workers are brought in to handel the load
            messageBroker.OffRequestThread.Subscribe(async message =>
            {
                if (Debug)
                {
                    Console.WriteLine($"Off Req Observable v  - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
                }

                // Simulate some expensive async operation
                await Task.Delay(Thread.CurrentThread.ManagedThreadId % 2 == 0 ? 150 : 30);

                if (Debug)
                {
                    Console.WriteLine($"Off Req Observable ^  - Thread {Thread.CurrentThread.ManagedThreadId}     - \"{message.Description}\"");
                }
            });


            var stopwatch = Stopwatch.StartNew();

            messageGenerator.Start();

            Console.WriteLine("Press the ANY key to get a summary.");
            Console.WriteLine("Press Ctrl+C to quit.");
            while (true)
            {
                Console.ReadLine();

                var elapsed = stopwatch.Elapsed;
                Console.WriteLine($"Messages Generated: {MessagesGenerated}");
                Console.WriteLine($"Messages Sent: {MessagesSent}");
                Console.WriteLine($"Time Elapsed: {elapsed}");
                Console.WriteLine($"Requests/sec: {(double)MessagesSent / ((double)elapsed.Ticks / (double)TimeSpan.FromSeconds(1).Ticks)}");
                Console.WriteLine($"Generated/sec: {(double)MessagesGenerated / ((double)elapsed.Ticks / (double)TimeSpan.FromSeconds(1).Ticks)}");
                Console.WriteLine();
                Console.WriteLine();
            }
        }