示例#1
0
        private static async Task Start()
        {
            IConnection conn = null;

            try
            {
                conn = await ConnectionFactory.Connect(TargetHost, vhost : VHost, username : _username, password : _password);

                Console.WriteLine("[Connected]");

                var newChannel = await conn.CreateChannel();

//				var newChannel = await conn.CreateChannelWithPublishConfirmation();
                Console.WriteLine("[channel created] " + newChannel.ChannelNumber);
                await newChannel.BasicQos(0, Prefetch, false);

                await newChannel.ExchangeDeclare("test_ex", "direct", true, false, null, true);

                var qInfo = await newChannel.QueueDeclare("queue1", false, true, false, false, null, true);

                Console.WriteLine("[qInfo] " + qInfo);

                await newChannel.QueueBind("queue1", "test_ex", "routing1", null, true);

                var prop = new BasicProperties()
                {
                    // DeliveryMode = 2,
                    Type = "type1",
                };
                prop.Headers["serialization"] = 0;

                newChannel.MessageUndeliveredHandler += (undelivered) =>
                {
                    Console.WriteLine("\t(Ops, message not routed: " +
                                      undelivered.replyCode + " " +
                                      undelivered.replyText + " " +
                                      undelivered.routingKey + ")");
                };

                Console.WriteLine("Started Publishing...");

                var watch = Stopwatch.StartNew();
                for (int i = 0; i < TotalPublish; i++)
                {
                    prop.Headers["serialization"] = i;
                    // var buffer = Encoding.ASCII.GetBytes("The " + i + " " + Message);
                    await newChannel.BasicPublish("test_ex", "routing1", true, prop, new ArraySegment <byte>(MessageContent));

                    // await Task.Delay(TimeSpan.FromMilliseconds(100));
                }
                watch.Stop();

                Console.WriteLine(" BasicPublish stress. Took " + watch.Elapsed.TotalMilliseconds +
                                  "ms - rate of " + (TotalPublish / watch.Elapsed.TotalSeconds) + " message per second");

                await Task.Delay(TimeSpan.FromMilliseconds(200));


                var newChannel2 = await conn.CreateChannel();

                Console.WriteLine("[channel created] " + newChannel2.ChannelNumber);
                await newChannel2.BasicQos(0, Prefetch, false);

//				var temp = new byte[1000000];

                watch = Stopwatch.StartNew();
                int totalReceived = 0;

                Console.WriteLine("[subscribing to queue] ");
                var sub = await newChannel2.BasicConsume(ConsumeMode.SingleThreaded, async (delivery) =>
                {
                    // var len = await delivery.stream.ReadAsync(temp, 0, (int) delivery.bodySize);
                    // var str = Encoding.UTF8.GetString(temp, 0, len);
                    // Console.WriteLine("Received : " + str.Length);

                    if (WithAcks)
                    {
                        if (totalReceived % 2 == 0)
                        {
                            newChannel2.BasicAck(delivery.deliveryTag, false);
                        }
                        else
                        {
                            newChannel2.BasicNAck(delivery.deliveryTag, false, false);
                        }
                    }

//					var val = Interlocked.Increment(ref totalReceived);
                    var val = ++totalReceived;

                    if (val == TotalPublish)
                    {
                        watch.Stop();
                        Console.WriteLine("Consume stress. Took " +
                                          watch.Elapsed.TotalMilliseconds +
                                          "ms - rate of " + (TotalPublish / watch.Elapsed.TotalSeconds) + " message per second");
                        totalReceived = 0;
                    }

                    // return Task.CompletedTask;
                }, "queue1", "tag123", !WithAcks, false, null, true);

                Console.WriteLine("[subscribed to queue] " + sub);

                await Task.Delay(TimeSpan.FromMinutes(5));

                await newChannel.Close();

                await newChannel2.Close();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("[Captured error] " + ex.Flatten().Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("[Captured error 2] " + ex.Message);
            }

            if (conn != null)
            {
                conn.Dispose();
            }
        }
示例#2
0
        private static async Task StartRpc()
        {
            IConnection conn1 = null;
            IConnection conn2 = null;

            try
            {
                conn1 = await ConnectionFactory.Connect(TargetHost, vhost : VHost, username : _username, password : _password);

                conn2 = await ConnectionFactory.Connect(TargetHost, vhost : VHost, username : _username, password : _password);

                Console.WriteLine("[Connected]");

                var newChannel = await conn1.CreateChannel();

                Console.WriteLine("[channel created] " + newChannel.ChannelNumber);
                await newChannel.BasicQos(0, Prefetch, false);

                await newChannel.ExchangeDeclare("test_ex", "direct", true, false, null, true);

                var qInfo = await newChannel.QueueDeclare("rpc1", false, true, false, false, null, true);

                Console.WriteLine("[qInfo] " + qInfo);

                await newChannel.QueueBind("rpc1", "test_ex", "rpc1", null, true);

                var temp = new byte[1000];

                Console.WriteLine("Starting Rpc channel consumer...");
                await newChannel.BasicConsume(ConsumeMode.SingleThreaded, delivery =>
                {
//					if (delivery.stream != null)
                    delivery.stream.Read(temp, 0, delivery.bodySize);

                    var replyProp           = newChannel.RentBasicProperties();
                    replyProp.CorrelationId = delivery.properties.CorrelationId;

                    newChannel.BasicPublishFast("",
                                                delivery.properties.ReplyTo, false,
                                                replyProp, new ArraySegment <byte>(temp, 0, 4));

                    return(Task.CompletedTask);
                }, "rpc1", "", true, false, null, waitConfirmation : true);

                Console.WriteLine("Starting Rpc calls...");

                var newChannel2 = await conn2.CreateChannel();

                Console.WriteLine("[channel created] " + newChannel2.ChannelNumber);
                await newChannel2.BasicQos(0, Prefetch, false);

                var rpcHelper = await newChannel2.CreateRpcHelper(ConsumeMode.SingleThreaded, timeoutInMs : null);

                var watch = new Stopwatch();
                watch.Start();

                var totalReceived = 0;
                for (int i = 0; i < TotalPublish; i++)
                {
                    await MakeCall(rpcHelper, i);

                    // var val = Interlocked.Increment(ref totalReceived);
                    var val = ++totalReceived;

                    if (val == TotalPublish)
                    {
                        watch.Stop();
                        Console.WriteLine("Rpc stress. Took " +
                                          watch.Elapsed.TotalMilliseconds +
                                          "ms - rate of " + (TotalPublish / watch.Elapsed.TotalSeconds) + " message per second");
                        totalReceived = 0;
                    }
                }

//				watch = Stopwatch.StartNew();
//				int totalReceived = 0;

//				Console.WriteLine("[subscribed to queue] " + sub);

                await Task.Delay(TimeSpan.FromMinutes(5));

                await newChannel.Close();

                await newChannel2.Close();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("[Captured error] " + ex.Flatten().Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("[Captured error 2] " + ex.Message);
            }

            if (conn1 != null)
            {
                conn1.Dispose();
            }
            if (conn2 != null)
            {
                conn2.Dispose();
            }
        }