コード例 #1
0
        protected override async Task OnOpenAsync(CancellationToken cancellationToken)
        {
            if (Delay > 0)
            {
                ServiceEventSource.Current.ServiceMessage(Context, $"Sleeping for {Delay} seconds.");
                await Task.Delay(TimeSpan.FromSeconds(Delay), cancellationToken);
            }

            var allMessages = CreateMessages();

            // Subscribe to all types
            foreach (var type in _messageTypes)
            {
                await _brokerClient.SubscribeAsync <object>(this.CreateReferenceWrapper(), type, message => null, true);
            }

            ServiceEventSource.Current.ServiceMessage(Context, "Load Test begin.");

            var options = new ParallelOptions
            {
                MaxDegreeOfParallelism = -1
            };
            Stopwatch sw = Stopwatch.StartNew();

            Parallel.For(0, allMessages.Length, options, i =>
            {
                var message = allMessages[i];
                _brokerClient.PublishMessageAsync(message).ConfigureAwait(false).GetAwaiter().GetResult();
            });
            sw.Stop();

            ServiceEventSource.Current.ServiceMessage(Context, $"In {sw.ElapsedMilliseconds}ms - Published {Amount} instances of Message Types {string.Join(", ", _messageTypes.Select(t => t.FullName))}.");
        }