summaryrefslogtreecommitdiff
path: root/qpid/dotnet/Qpid.Integration.Tests/old/ServiceRequestingClient.tmp
blob: da0f764bcd1d65bc04b143f47efb369589c7dd22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
using System;
using System.Threading;
using log4net;
using NUnit.Framework;
using Apache.Qpid.Messaging;

namespace Apache.Qpid.Integration.Tests.testcases
{
    public class ServiceRequestingClient : BaseMessagingTestFixture
    {
        private const int MESSAGE_SIZE = 1024;
        private static string MESSAGE_DATA = new string('x', MESSAGE_SIZE);

        private const int PACK = 100;
        private const int NUM_MESSAGES = PACK*10; // increase when in standalone

        private static ILog _log = LogManager.GetLogger(typeof(ServiceRequestingClient));

        ManualResetEvent _finishedEvent = new ManualResetEvent(false);
        
        private int _expectedMessageCount = NUM_MESSAGES;

        private long _startTime = 0;        
        
        private string _commandQueueName = "ServiceQ1";
        
        private IMessagePublisher _publisher;

        Avergager averager = new Avergager();
        
        private void InitialiseProducer()
        {
            try
            {
                _publisher = _channel.CreatePublisherBuilder()
                    .WithRoutingKey(_commandQueueName)
                    .WithDeliveryMode(DeliveryMode.NonPersistent)
                    .Create();
                _publisher.DisableMessageTimestamp = true; // XXX: need a "with" for this in builder?
            }
            catch (QpidException e)
            {
                _log.Error("Error: " + e, e);
            }
        }
        
        [Test]
        public void SendMessages()
        {
            InitialiseProducer();

            string replyQueueName = _channel.GenerateUniqueName();
            
            _channel.DeclareQueue(replyQueueName, false, true, true);

            IMessageConsumer messageConsumer = _channel.CreateConsumerBuilder(replyQueueName)
                .WithPrefetchLow(100)
                .WithPrefetchHigh(200)
                .WithNoLocal(true)
                .WithExclusive(true).Create();
 
            _startTime = DateTime.Now.Ticks;

            messageConsumer.OnMessage = new MessageReceivedDelegate(OnMessage);
            _connection.Start();
            for (int i = 0; i < _expectedMessageCount; i++)
            {
                ITextMessage msg;
                try
                {
                    msg = _channel.CreateTextMessage(MESSAGE_DATA + i);
                }
                catch (Exception e)
                {
                    _log.Error("Error creating message: " + e, e);
                    break;
                }
                msg.ReplyToRoutingKey = replyQueueName;

                // Added timestamp.
                long timeNow = DateTime.Now.Ticks;
                string timeSentString = String.Format("{0:G}", timeNow);
                msg.Headers.SetLong("timeSent", timeNow);
                
                 _publisher.Send(msg);
            }

            // Assert that the test finishes within a reasonable amount of time.
            const int waitSeconds = 40;
            const int waitMilliseconds = waitSeconds * 1000;
            _log.Info("Finished sending " + _expectedMessageCount + " messages");
            _log.Info(String.Format("Waiting {0} seconds to receive last message...", waitSeconds));
            Assert.IsTrue(_finishedEvent.WaitOne(waitMilliseconds, false), 
                          String.Format("Expected to finish in {0} seconds", waitSeconds));
        }

        public void OnMessage(IMessage m)
        {
            if (_log.IsDebugEnabled)
            {
                _log.Debug("Message received: " + m);
            }

            if (!m.Headers.Contains("timeSent"))
            {
                throw new Exception("Set timeSent!");
            }

            long sentAt = m.Headers.GetLong("timeSent");
            long now = DateTime.Now.Ticks;
            long latencyTicks = now - sentAt;
            long latencyMilliseconds = latencyTicks / TimeSpan.TicksPerMillisecond;

            averager.Add(latencyMilliseconds);

            if (averager.Num % PACK == 0)
            {
                _log.Info("Ticks per millisecond = " + TimeSpan.TicksPerMillisecond);
                _log.Info(String.Format("Average latency (ms) = {0}", averager));
                _log.Info("Received message count: " + averager.Num);
            }

            if (averager.Num == _expectedMessageCount)
            {
                _log.Info(String.Format("Final average latency (ms) = {0}", averager));

                double timeTakenSeconds = (DateTime.Now.Ticks - _startTime) * 1.0 / (TimeSpan.TicksPerMillisecond * 1000);
                _log.Info("Total time taken to receive " + _expectedMessageCount + " messages was " +
                          timeTakenSeconds + "s, equivalent to " +
                          (_expectedMessageCount/timeTakenSeconds) + " messages per second");

                _finishedEvent.Set(); // Notify main thread to quit.
            }
        }
    }
    
    class Avergager
    {
        long num = 0;
        long sum = 0;

        long min = long.MaxValue;
        long max = long.MinValue;
        
        public void Add(long item)
        {
            ++num;
            sum += item;
            if (item < min) min = item;
            if (item > max) max = item;
        }
        
        public long Average { get { return sum/num; }}

        public long Num { get { return num;  } }
        
        public override string ToString()
        {
            return String.Format("average={0} min={1} max={2}", Average, min, max);
        }
    }
}