Example #1
0
        private async Task RandomClient(IWorkContext context, MonitorReport report, int clientNumber, CancellationToken token)
        {
            string          agentName = $"{_options.AgentName}_Send_{clientNumber}";
            AgentContractV1 agent     = (await _client.Message.GetAgentId(context, _options.QueueName, agentName)).Value;

            using (var monitor = new MonitorRate(report, agentName))
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        EnqueueMessageContractV1          request  = CreateMessage(context, agent.AgentId, _options.QueueName);
                        RestResponse <EnqueuedContractV1> response = await _client.Message.EnqueueMessage(context, request);

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            monitor.IncrementError();
                            continue;
                        }

                        monitor.IncrementNew();

                        await Utility.Delay(_options.Delay, token);
                    }
                    catch
                    {
                        monitor.IncrementError();
                    }
                }
            }
        }
Example #2
0
        public MonitorRate(MonitorReport report, string name)
        {
            Verify.IsNotNull(nameof(report), report);
            Verify.IsNotEmpty(nameof(name), name);

            Report = report;
            Name   = name;

            _timer             = new Timer(x => Flush(), null, _period, _period);
            _currentRateDetail = new RateDetail(name);
        }
Example #3
0
        public async Task Run(IWorkContext context, CancellationToken token)
        {
            Console.WriteLine($"Running {_options.ClientCount} clients");

            await SetupQueue(context);

            using (var report = new MonitorReport("Monitor", _options))
            {
                var tasks = new List <Task>();

                foreach (var clientNumber in Enumerable.Range(0, _options.ClientCount))
                {
                    tasks.Add(Task.Run(() => RandomClient(context, report, clientNumber, token)));
                }

                Task.WaitAll(tasks.ToArray());
            }
        }
        private async Task RandomClient(IWorkContext context, MonitorReport report, int clientNumber, CancellationToken token)
        {
            string          agentName = $"{_options.AgentName}_Receive_{clientNumber}";
            AgentContractV1 agent     = (await _client.Message.GetAgentId(context, _options.QueueName, agentName)).Value;

            TimeSpan?waitFor = _options.NoWait ? (TimeSpan?)null : TimeSpan.FromSeconds(30);

            using (var monitor = new MonitorRate(report, agentName))
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        RestResponse <MessageContractV1> readMessage;

                        if (_options.NoLock)
                        {
                            readMessage = await _client.Message.DequeueMessageAndDelete(context, _options.QueueName, waitFor, token);
                        }
                        else
                        {
                            readMessage = await _client.Message.DequeueMessageWithLock(context, _options.QueueName, agent, waitFor, token);
                        }

                        if (readMessage.StatusCode == HttpStatusCode.NoContent)
                        {
                            monitor.AddRetry();
                            await Utility.Delay(TimeSpan.FromMilliseconds(500), token);

                            continue;
                        }

                        WorkRequest workRequest = readMessage.Value.Deserialize <WorkRequest>();

                        lock (_lock)
                        {
                            if (workRequest.MessageCount > _highMessageCount)
                            {
                                _highMessageCount = workRequest.MessageCount;
                            }
                        }

                        await Utility.Delay(_options.Delay, token);

                        if (!_options.NoLock)
                        {
                            var settleMessage = new SettleMessageContractV1
                            {
                                AgentId    = agent.AgentId,
                                QueueName  = _options.QueueName,
                                SettleType = SettleType.Processed,
                                MessageId  = readMessage.Value.MessageId,
                            };

                            await _client.Message.SettleMessage(context, settleMessage);
                        }

                        monitor.IncrementRead();
                    }
                    catch (Exception ex)
                    {
                        monitor.IncrementError(ex.ToString());
                    }
                }
            }
        }