예제 #1
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            var uptime = Stopwatch.StartNew();

            try
            {
                ConfigurationSection enumerationPerformanceTestConfiguration = this.Context.CodePackageActivationContext.GetConfigurationSection("EnumerationPerformanceTest");
                string connectionString = enumerationPerformanceTestConfiguration.GetStringValue("TargetConnectionString");
                connectionString = Helpers.GetServerAddressIfNotProvided(connectionString);

                ulong timeStreamId = enumerationPerformanceTestConfiguration.GetUInt64Value("TimeStream");

                while (!cancellationToken.IsCancellationRequested)
                {
                    using (var ringMaster = new RetriableRingMasterClient(s => Helpers.CreateRingMasterTimeStreamRequestHandler(s, cancellationToken, timeStreamId), connectionString))
                    {
                        await Task.Run(() => this.GetChildrenPerformanceTest(ringMaster, enumerationPerformanceTestConfiguration, cancellationToken));
                    }

                    await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
                }
            }
            catch (Exception ex)
            {
                EnumerationStressServiceEventSource.Log.RunAsyncFailed(ex.ToString());
                throw;
            }
            finally
            {
                EnumerationStressServiceEventSource.Log.Terminated((long)uptime.Elapsed.TotalSeconds);
            }
        }
        public void RetriableRingMasterClientStress()
        {
            int maxRequestCount       = 1000;
            int initiallyWorkingCount = 500;
            var mockRequestHandler    = Substitute.For <IRingMasterRequestHandler>();

            var rnd          = new Random();
            int requestCount = 0;

            mockRequestHandler.Request(Arg.Any <IRingMasterRequest>()).Returns <Task <RequestResponse> >((callInfo2) =>
            {
                Interlocked.Increment(ref requestCount);
                if (requestCount <= initiallyWorkingCount)
                {
                    if (rnd.NextDouble() < 0.1)
                    {
                        return(Task.FromResult(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Operationtimeout
                        }));
                    }
                    else
                    {
                        return(Task.FromResult(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Ok
                        }));
                    }
                }
                else if (rnd.NextDouble() < 0.5)
                {
                    return(Task.FromResult(new RequestResponse()
                    {
                        ResultCode = (int)RingMasterException.Code.Operationtimeout
                    }));
                }
                else
                {
                    throw RingMasterException.GetException(new RequestResponse()
                    {
                        ResultCode = (int)RingMasterException.Code.OperationCancelled
                    });
                }
            });

            var createClientFunc = Substitute.For <Func <string, IRingMasterRequestHandler> >();

            createClientFunc(Arg.Any <string>()).Returns((callInfo) =>
            {
                requestCount = 0;
                return(mockRequestHandler);
            });

            var vegaServiceInfoReader = Substitute.For <IVegaServiceInfoReader>();

            vegaServiceInfoReader.GetVegaServiceInfo().Returns((callInfo) =>
            {
                Thread.Sleep(rnd.Next(1, 5) * 1000);
                return(Tuple.Create(Arg.Any <string>(), Arg.Any <string>()));
            });

            var requestFunc = Substitute.For <Func <IRingMasterRequestHandler, Task <RequestResponse> > >();

            requestFunc(Arg.Any <IRingMasterRequestHandler>()).Returns((callInfo) => mockRequestHandler.Request(Arg.Any <IRingMasterRequest>()));

            RetriableRingMasterClient theClient = new RetriableRingMasterClient(createClientFunc, Arg.Any <string>(), vegaServiceInfoReader, log);

            int taskCount      = 0;
            int exceptionCount = 0;

            for (int i = 0; i < maxRequestCount; i++)
            {
                taskCount++;
                var unused = theClient.Request(requestFunc)
                             .ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        Interlocked.Increment(ref exceptionCount);
                    }

                    Interlocked.Decrement(ref taskCount);
                });
            }

            SpinWait.SpinUntil(() => taskCount == 0);

            Assert.IsTrue(exceptionCount < maxRequestCount - initiallyWorkingCount);
            log($"{exceptionCount}");
        }