Exemplo n.º 1
0
        private Task SetupOneStream(
            Guid streamId, string streamProviderName,
            AsyncPipeline pipeline,
            int numConsumers,
            int numProducers,
            bool normalSubscribeCalls)
        {
            //output.WriteLine("Initializing Stream {0} with Consumers={1} Producers={2}", streamId, numConsumers, numProducers);

            List <Task> promises = new List <Task>();

            if (producersFirst && numProducers > 0)
            {
                // Producers
                var p1 = SetupProducers(streamId, this.StreamNamespace, streamProviderName, pipeline, numProducers);
                promises.AddRange(p1);
            }

            // Consumers
            if (numConsumers > 0)
            {
                var c = SetupConsumers(streamId, this.StreamNamespace, streamProviderName, pipeline, numConsumers, normalSubscribeCalls);
                promises.AddRange(c);
            }

            if (!producersFirst && numProducers > 0)
            {
                // Producers
                var p2 = SetupProducers(streamId, this.StreamNamespace, streamProviderName, pipeline, numProducers);
                promises.AddRange(p2);
            }

            return(Task.WhenAll(promises));
        }
Exemplo n.º 2
0
        public void Test1()
        {
            // plain pipeline definition objects
            PipelineDefinition <string, string> concat4As = new PipelineDefinition <string, string>();

            concat4As.Append <AddA>();
            concat4As.Append <AddA>();
            concat4As.Append <AddA>();
            concat4As.Append <AddA>();

            concat4As.Append <AddB>();
            concat4As.Append <AddB>();
            concat4As.Append <AddB>();
            concat4As.Append <AddB>();

            // ioc fake for pipe construction
            IServiceProvider serviceProviderMock = Substitute.For <IServiceProvider>();

            serviceProviderMock.GetService(Arg.Is(typeof(AddA))).Returns(new AddA());

            serviceProviderMock.GetService(Arg.Is(typeof(AddB))).Returns(new AddB());

            IPipeline <string, string> pipe = new AsyncPipeline <string, string>(concat4As, serviceProviderMock);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            string result = (string)pipe.Process("");

            sw.Stop();

            Assert.True(result == "AAAABBBB");
        }
Exemplo n.º 3
0
        public List <Task> ProcessNodes(
            Func <ChirperUserInfo, Task> action,
            Action <long> intervalAction = null,
            AsyncPipeline pipeline       = null)
        {
            List <Task> promises = new List <Task>();
            long        i        = 0;

            foreach (XElement nodeData in Nodes)
            {
                ChirperUserInfo userData = ParseUserInfo(nodeData);

                // Call main processing action
                Task n = action(userData);
                if (n != null)
                {
                    if (pipeline != null)
                    {
                        pipeline.Add(n);
                    }
                    promises.Add(n);
                }
                // else skip this node

                if (intervalAction != null && ProgressInterval != 0)
                {
                    if ((++i % ProgressInterval) == 0)
                    {
                        // Call progress interval action
                        intervalAction(i);
                    }
                }
            }
            return(promises);
        }
Exemplo n.º 4
0
        public void AsyncPipelineSimpleTest()
        {
            int       step     = 1000;
            var       done     = TimedCompletions(step, step, step);
            var       pipeline = new AsyncPipeline(2);
            Stopwatch watch    = new Stopwatch();

            watch.Start();
            pipeline.Add(done[0]);
            const int epsilon  = 100;
            var       elapsed0 = watch.ElapsedMilliseconds;

            Assert.IsTrue(elapsed0 < epsilon, elapsed0.ToString());
            pipeline.Add(done[2]);
            var elapsed1 = watch.ElapsedMilliseconds;

            Assert.IsTrue(elapsed1 < epsilon, elapsed1.ToString());
            pipeline.Add(done[1]);
            var elapsed2 = watch.ElapsedMilliseconds;

            Assert.IsTrue(step - epsilon <= elapsed2 && elapsed2 <= step + epsilon);
            pipeline.Wait();
            watch.Stop();
            Assert.IsTrue(3 * step - epsilon <= watch.ElapsedMilliseconds && watch.ElapsedMilliseconds <= 3 * step + epsilon);
        }
Exemplo n.º 5
0
        public List<Task> ProcessNodes(
            Func<ChirperUserInfo, Task> action,
            Action<long> intervalAction = null,
            AsyncPipeline pipeline = null)
        {
            List<Task> promises = new List<Task>();
            long i = 0;
            foreach (XElement nodeData in Nodes)
            {
                ChirperUserInfo userData = ParseUserInfo(nodeData);

                // Call main processing action
                Task n = action(userData);
                if (n != null)
                {
                    if (pipeline != null) pipeline.Add(n);
                    promises.Add(n);
                }
                // else skip this node

                if (intervalAction != null && ProgressInterval != 0)
                {
                    if ((++i % ProgressInterval) == 0)
                    {
                        // Call progress interval action
                        intervalAction(i);
                    }
                }
            }
            return promises;
        }
Exemplo n.º 6
0
        public void AsyncPipelineSimpleTest()
        {
            int       step     = 2000;
            int       epsilon  = 200;
            var       pipeline = new AsyncPipeline(2);
            var       done     = TimedCompletions(step, step, step);
            Stopwatch watch    = Stopwatch.StartNew();

            pipeline.Add(done[0]);
            var elapsed0 = watch.ElapsedMilliseconds;

            Assert.True(elapsed0 < epsilon, $"{elapsed0}ms");
            pipeline.Add(done[2]);
            var elapsed1 = watch.ElapsedMilliseconds;

            Assert.True(elapsed1 < epsilon, $"{elapsed1}ms");
            pipeline.Add(done[1]);
            var elapsed2 = watch.ElapsedMilliseconds;

            Assert.True(step - epsilon <= elapsed2, $"{elapsed2}ms");
            Assert.True(elapsed2 <= step + epsilon, $"{elapsed2}ms");
            pipeline.Wait();
            watch.Stop();
            Assert.True(3 * step - epsilon <= watch.ElapsedMilliseconds, $"{watch.ElapsedMilliseconds}ms");
            Assert.True(watch.ElapsedMilliseconds <= 3 * step + epsilon, $"{watch.ElapsedMilliseconds}ms");
        }
        public async Task Execute(ExecutionContext context, AsyncPipeline <ExecutionContext> .NextDelegate next)
        {
            var applicableValidators = validators.Where(v => v.CanValidateInstancesOfType(context.Request.GetType()));
            var validationContext    = new ValidationContext(context.Request)
            {
                RootContextData = { [ExecutionContextKey] = context }
            };

            var validationMessageBuilder = new StringBuilder();
            var isInvalid = false;

            foreach (var validator in applicableValidators)
            {
                var validationResult =
                    await validator.ValidateAsync(validationContext).ConfigureAwait(false);

                if (!validationResult.IsValid)
                {
                    validationMessageBuilder.AppendLine(validationResult.ToString());
                    isInvalid = true;
                }
            }

            if (!isInvalid)
            {
                await next.Invoke().ConfigureAwait(false);
            }
            else
            {
                context.Errors.Add(ExecutionErrorType.ValidationFailed, validationMessageBuilder.ToString());
            }
        }
Exemplo n.º 8
0
        public async Task Pipeline_AsyncFilterExecution_Test()
        {
            var types = new List <Type>
            {
                typeof(FooComponent),
                typeof(PipelineExecutionTerminatingComponent),
                typeof(BarComponent)
            };

            var config = new Dictionary <string, IDictionary <string, string> >();

            foreach (var t in types)
            {
                config.Add(t.Name, new Dictionary <string, string> {
                    { "test", "value" }
                });
            }

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config);
            var result = await target.ExecuteAsync(new TestPayload());

            result.Should().NotBeNull();
            result.Count.Should().Be(2);
            result.FooStatus.Should().Be($"{nameof(FooComponent)} executed!");
            result.BarStatus.Should().BeNull();
        }
Exemplo n.º 9
0
        public async Task AsyncPipeline_DuplicateComponentsConfiguredDifferently_Test()
        {
            var settings = new Dictionary <string, IDictionary <string, string> >
            {
                { "Component1", new Dictionary <string, string> {
                      { "TestValue", "Component1Value" }, { "UseFoo", "true" }
                  } },
                { "Component2", new Dictionary <string, string> {
                      { "TestValue", "Component2Value" }, { "UseFoo", "false" }
                  } }
            };

            var payload = new TestPayload();
            var target  = new AsyncPipeline <TestPayload>(
                PipelineComponentResolver, new List <string> {
                "Component1", "Component2"
            }, settings);

            var result = await target.ExecuteAsync(payload);

            result.Should().NotBeNull();
            result.Should().Be(payload);
            result.FooStatus.Should().Be("Component1Value");
            payload.BarStatus.Should().Be("Component2Value");
        }
Exemplo n.º 10
0
        public void AsyncPipelineWaitTest()
        {
            Random rand = new Random(222);
            int started = 0;
            int finished1 = 0;
            int finished2 = 0;
            int numActions = 1000;
            Action action1 = (() => 
                {
                    lock (this) started++;
                    Thread.Sleep((int)(rand.NextDouble() * 100));
                    lock (this) finished1++;
                });
            Action action2 = (() =>
            {
                Thread.Sleep((int)(rand.NextDouble() * 100));
                lock (this) finished2++;
            });

            var pipeline = new AsyncPipeline(10);
            for (int i = 0; i < numActions; i++)
            {
                var async1 = Task.Run(action1);
                pipeline.Add(async1);
                var async2 = async1.ContinueWith(_ => action2());
                pipeline.Add(async2);
            }
            pipeline.Wait();
            Assert.AreEqual(numActions, started);
            Assert.AreEqual(numActions, finished1);
            Assert.AreEqual(numActions, finished2);
        }
Exemplo n.º 11
0
        public async Task Execute_RunSamePipelineTwice_SuccessfullyExecute()
        {
            var pipeline = new AsyncPipeline <PersonModel>(new ActivatorMiddlewareResolver())
                           .Add <PersonWithEvenId>()
                           .Add <PersonWithOddId>()
                           .Add <PersonWithEmailName>()
                           .Add <PersonWithGenderProperty>();

            // This person model has a name that matches the 'PersonWithEmailName' middleware.
            var personModel = new PersonModel
            {
                Name = "*****@*****.**"
            };

            await pipeline.Execute(personModel);

            // Check if the level of 'personModel' is 3, which is configured by 'PersonWithEmailName' middleware.
            Assert.AreEqual(3, personModel.Level);


            // Creates a new instance with a 'Gender' property. The 'PersonWithGenderProperty'
            // middleware should be the last one to be executed.
            personModel = new PersonModel
            {
                Name   = "*****@*****.**",
                Gender = Gender.Other
            };

            pipeline.Execute(personModel);

            // Check if the level of 'personModel' is 4, which is configured by 'PersonWithGenderProperty' middleware.
            Assert.AreEqual(4, personModel.Level);
        }
Exemplo n.º 12
0
        public void AsyncPipelineWaitTest()
        {
            Random rand       = new Random(222);
            int    started    = 0;
            int    finished1  = 0;
            int    finished2  = 0;
            int    numActions = 1000;
            Action action1    = (() =>
            {
                lock (this) started++;
                Thread.Sleep((int)(rand.NextDouble() * 100));
                lock (this) finished1++;
            });
            Action action2 = (() =>
            {
                Thread.Sleep((int)(rand.NextDouble() * 100));
                lock (this) finished2++;
            });

            var pipeline = new AsyncPipeline(10);

            for (int i = 0; i < numActions; i++)
            {
                var async1 = Task.Run(action1);
                pipeline.Add(async1);
                var async2 = async1.ContinueWith(_ => action2());
                pipeline.Add(async2);
            }
            pipeline.Wait();
            Assert.AreEqual(numActions, started);
            Assert.AreEqual(numActions, finished1);
            Assert.AreEqual(numActions, finished2);
        }
Exemplo n.º 13
0
        public IAsyncPipelineBuilder <TRequest> AsyncPipeline()
        {
            var asyncPipeline = new AsyncPipeline <TRequest>(_mediatorBuilder);

            _mediatorBuilder.AddAsyncPipeline <TRequest>(asyncPipeline);
            return(asyncPipeline);
        }
Exemplo n.º 14
0
        public async Task AsyncPipeline_Execution_Test()
        {
            //Arrange
            PipelineComponentResolver.AddAsync(new FooComponent(), new BarComponent());

            var types = new List <Type> {
                typeof(FooComponent), typeof(BarComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config, null);

            //Act
            var result = await target.ExecuteAsync(new TestPayload());

            //Assert
            result.Should().NotBeNull();
            result.Count.Should().Be(2);
            result.FooStatus.Should().Be($"{nameof(FooComponent)} executed!");
            result.BarStatus.Should().Be($"{nameof(BarComponent)} executed!");
        }
Exemplo n.º 15
0
        public async Task AsyncPipeline_Dispose_Test()
        {
            //Arrange
            var component1 = Substitute.For <IDisposableAsyncPipelineComponent>();
            var component2 = Substitute.For <IAsyncPipelineComponent <TestPayload> >();

            var components = new[] { component1, component2 };

            var resolver = new DictionaryPipelineComponentResolver();

            resolver.AddAsync(components);

            var payload = new TestPayload();

            component1.ExecuteAsync(Arg.Any <TestPayload>(), Arg.Any <CancellationToken>()).Returns(payload);
            component2.ExecuteAsync(Arg.Any <TestPayload>(), Arg.Any <CancellationToken>()).Returns(payload);

            TestPayload result;

            //Act
            using (var sut = new AsyncPipeline <TestPayload>(resolver, components.Select(c => c.GetType()), null, null))
            {
                result = await sut.ExecuteAsync(payload, CancellationToken.None).ConfigureAwait(false);
            }

            //Assert
            result.Should().NotBeNull();
            component1.Received().Dispose();
        }
Exemplo n.º 16
0
        private Task Test_Stream_Churn_NumStreams(
            string streamProviderName,
            int pipelineSize,
            int numStreams,
            int numConsumers          = 9,
            int numProducers          = 1,
            bool warmUpPubSub         = true,
            bool normalSubscribeCalls = true)
        {
            output.WriteLine("Testing churn with {0} Streams with {1} Consumers and {2} Producers per Stream NormalSubscribe={3}",
                             numStreams, numConsumers, numProducers, normalSubscribeCalls);

            AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);
            var           promises = new List <Task>();

            // Create streamId Guids
            Guid[] streamIds = new Guid[numStreams];
            for (int i = 0; i < numStreams; i++)
            {
                streamIds[i] = Guid.NewGuid();
            }

            if (warmUpPubSub)
            {
                WarmUpPubSub(streamProviderName, streamIds, pipeline);

                pipeline.Wait();

                int activePubSubGrains = ActiveGrainCount(typeof(PubSubRendezvousGrain).FullName);
                Assert.Equal(streamIds.Length, activePubSubGrains);   //  "Initial PubSub count -- should all be warmed up"
            }

            int activeConsumerGrains = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);

            Assert.Equal(0, activeConsumerGrains);   //  "Initial Consumer count should be zero"

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < numStreams; i++)
            {
                Task promise = SetupOneStream(streamIds[i], streamProviderName, pipeline, numConsumers, numProducers, normalSubscribeCalls);
                promises.Add(promise);
            }
            Task.WhenAll(promises).Wait();
            sw.Stop();

            int consumerCount = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);

            Assert.Equal(activeConsumerGrains + (numStreams * numConsumers), consumerCount);   //  "The correct number of new Consumer grains are active"

            TimeSpan elapsed            = sw.Elapsed;
            int      totalSubscriptions = numStreams * numConsumers;
            double   rps = totalSubscriptions / elapsed.TotalSeconds;

            output.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
            Assert.NotEqual(0.0, rps);   // "RPS greater than zero"
            return(Task.CompletedTask);
        }
        public static IServiceCollection AddBookingPipelines2(this IServiceCollection services)
        {
            var mapping = new MappingStep2();

            var searchPipelineAsync = new AsyncPipeline <HubRequest, HubResponse>(hubRq => hubRq
                                                                                  .AddStep(mapping));

            return(services);
        }
Exemplo n.º 18
0
        public void Add_AddTypeThatIsNotAMiddleware_ThrowsException()
        {
            var pipeline = new AsyncPipeline <PersonModel>(new ActivatorMiddlewareResolver());

            PipelineNetAssert.ThrowsException <ArgumentException>(() =>
            {
                pipeline.Add(typeof(AsyncPipelineTests));
            });
        }
Exemplo n.º 19
0
        public ChirperNetworkLoader(AsyncPipeline pipeline = null)
        {
            this.pipeline     = pipeline;
            this.Users        = new Dictionary <long, IChirperAccount>();
            this.PipelineSize = (pipeline == null) ? DefaultPipelineSize : pipeline.Capacity;

            if (!Orleans.GrainClient.IsInitialized)
            {
                Orleans.GrainClient.Initialize();
            }
            runtimeStopwatch.Start();
        }
Exemplo n.º 20
0
 public ChirperNetworkLoader(AsyncPipeline pipeline = null)
 {
     this.pipeline = pipeline;
     this.Users = new Dictionary<long, IChirperAccount>();
     this.PipelineSize = (pipeline == null) ? DefaultPipelineSize : pipeline.Capacity;
     
     if (!Orleans.GrainClient.IsInitialized)
     {
         Orleans.GrainClient.Initialize();
     }
     runtimeStopwatch.Start();
 }
        public async Task DeactivateOnIdleTest_Stress_3_MayInterleave_With_TaskCompletionSource_UnderConstantLoad()
        {
            foreach (var i in Enumerable.Range(0, 100))
            {
                Initialize();

                output.WriteLine($"Running test #{i}");
                await RunTest();

                Dispose();
            }

            async Task RunTest()
            {
                var a = this.testCluster.GrainFactory.GetGrain <ICollectionTestGrain>(1, "UnitTests.Grains.MayInterleaveCollectionTestGrainWithTcs");

                var activated = await a.GetActivationTime();

                Assert.Equal(activated, await a.GetActivationTime());

                var pipeline     = new AsyncPipeline(20);
                var cts          = new CancellationTokenSource();
                var cancellation = cts.Token;

                var requests = new List <Task <int> >();
                var senders  = Task.Run(() =>
                {
                    while (!cancellation.IsCancellationRequested)
                    {
                        var request = a.IncrCounter();
                        requests.Add(request);
                        pipeline.Add(request);
                    }
                });

                // give it some time to fill the queue
                await Task.Delay(TimeSpan.FromSeconds(1));

                output.WriteLine($"Requests (before): {requests.Count}");
                await a.DeactivateSelf();

                cts.Cancel();
                pipeline.Wait();
                await senders;

                output.WriteLine("After deactivation");
                Assert.NotEqual(activated, await a.GetActivationTime());

                output.WriteLine($"Requests (total): {requests.Count}");
                Assert.All(requests, x => Assert.True(x.Status == TaskStatus.RanToCompletion));
            }
        }
Exemplo n.º 22
0
 public ChirperNetworkLoader(AsyncPipeline pipeline = null)
 {
     this.pipeline = pipeline;
     this.Users = new Dictionary<long, IChirperAccount>();
     this.PipelineSize = (pipeline == null) ? DefaultPipelineSize : pipeline.Capacity;
     
     if (!GrainClient.IsInitialized)
     {
         var config = ClientConfiguration.LocalhostSilo();
         GrainClient.Initialize(config);
     }
     runtimeStopwatch.Start();
 }
        public ChirperNetworkLoader(AsyncPipeline pipeline = null)
        {
            this.pipeline     = pipeline;
            this.Users        = new Dictionary <long, IChirperAccount>();
            this.PipelineSize = (pipeline == null) ? DefaultPipelineSize : pipeline.Capacity;

            if (!GrainClient.IsInitialized)
            {
                var config = ClientConfiguration.LocalhostSilo();
                GrainClient.Initialize(config);
            }
            runtimeStopwatch.Start();
        }
Exemplo n.º 24
0
        public async Task AsyncPipeline_Execution_NullSettings_Test()
        {
            var types = new List <Type> {
                typeof(FooComponent), typeof(BarComponent)
            };

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, null);
            var result = await target.ExecuteAsync(new TestPayload());

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count == 2);
            Assert.IsTrue(result.FooStatus == $"{nameof(FooComponent)} executed!");
            Assert.IsTrue(result.BarStatus == $"{nameof(BarComponent)} executed!");
        }
        internal async Task Dispatch(ExecutionContext context, AsyncPipeline <ExecutionContext> .NextDelegate next)
        {
            foreach (var controller in controllers)
            {
                if (controller.CanExecute(context))
                {
                    await controller.Execute(context).ConfigureAwait(false);

                    return;
                }
            }

            await next.Invoke().ConfigureAwait(false);
        }
Exemplo n.º 26
0
        public void AsyncPipelineComponent_Exception_Test()
        {
            var types = new List <Type> {
                typeof(FooComponent), typeof(BarExceptionComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });


            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config);
            Func <Task <TestPayload> > act = () => target.ExecuteAsync(new TestPayload());

            act.Should().ThrowExactly <PipelineExecutionException>()
            .And.InnerException.Should().BeOfType <NotImplementedException>();
        }
Exemplo n.º 27
0
        //private async Task Test_Stream_Churn_TimePeriod(
        //    string streamProviderName,
        //    int pipelineSize,
        //    TimeSpan duration,
        //    int numConsumers = 9,
        //    int numProducers = 1)
        //{
        //    output.WriteLine("Testing Subscription churn for duration {0} with {1} Consumers and {2} Producers per Stream",
        //        duration, numConsumers, numProducers);

        //    AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);
        //    var promises = new List<Task>();

        //    Stopwatch sw = Stopwatch.StartNew();

        //    for (int i = 0; sw.Elapsed <= duration; i++)
        //    {
        //        Guid streamId = Guid.NewGuid();
        //        Task promise = SetupOneStream(streamId, streamProviderName, pipeline, numConsumers, numProducers);
        //        promises.Add(promise);
        //    }
        //    await Task.WhenAll(promises);
        //    sw.Stop();
        //    TimeSpan elapsed = sw.Elapsed;
        //    int totalSubscription = numSt* numConsumers);
        //    double rps = totalSubscription/elapsed.TotalSeconds;
        //    output.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
        //    Assert.NotEqual(0.0, rps, "RPS greater than zero");
        //}

        private void WarmUpPubSub(string streamProviderName, StreamId[] streamIds, AsyncPipeline pipeline)
        {
            int numStreams = streamIds.Length;

            // Warm up PubSub for the appropriate streams
            for (int i = 0; i < numStreams; i++)
            {
                var streamId = new InternalStreamId(streamProviderName, streamIds[i]);
                _ = streamProviderName + "_" + this.StreamNamespace;

                IPubSubRendezvousGrain pubsub = this.GrainFactory.GetGrain <IPubSubRendezvousGrain>(streamId.ToString());

                Task promise = pubsub.Validate();

                pipeline.Add(promise);
            }
            pipeline.Wait();
        }
Exemplo n.º 28
0
        //private async Task Test_Stream_Churn_TimePeriod(
        //    string streamProviderName,
        //    int pipelineSize,
        //    TimeSpan duration,
        //    int numConsumers = 9,
        //    int numProducers = 1)
        //{
        //    output.WriteLine("Testing Subscription churn for duration {0} with {1} Consumers and {2} Producers per Stream",
        //        duration, numConsumers, numProducers);

        //    AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);
        //    var promises = new List<Task>();

        //    Stopwatch sw = Stopwatch.StartNew();

        //    for (int i = 0; sw.Elapsed <= duration; i++)
        //    {
        //        Guid streamId = Guid.NewGuid();
        //        Task promise = SetupOneStream(streamId, streamProviderName, pipeline, numConsumers, numProducers);
        //        promises.Add(promise);
        //    }
        //    await Task.WhenAll(promises);
        //    sw.Stop();
        //    TimeSpan elapsed = sw.Elapsed;
        //    int totalSubscription = numSt* numConsumers);
        //    double rps = totalSubscription/elapsed.TotalSeconds;
        //    output.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
        //    Assert.NotEqual(0.0, rps, "RPS greater than zero");
        //}

        private void WarmUpPubSub(string streamProviderName, Guid[] streamIds, AsyncPipeline pipeline)
        {
            int numStreams = streamIds.Length;

            // Warm up PubSub for the appropriate streams
            for (int i = 0; i < numStreams; i++)
            {
                Guid   streamId = streamIds[i];
                string extKey   = streamProviderName + "_" + this.StreamNamespace;

                IPubSubRendezvousGrain pubsub = this.GrainFactory.GetGrain <IPubSubRendezvousGrain>(streamId, extKey, null);

                Task promise = pubsub.Validate();

                pipeline.Add(promise);
            }
            pipeline.Wait();
        }
Exemplo n.º 29
0
        public async Task Execute_RunSeveralMiddlewareWithTwoBeingDynamiccalyAdded_SuccessfullyExecute()
        {
            var pipeline = new AsyncPipeline <PersonModel>(new ActivatorMiddlewareResolver())
                           .Add <PersonWithEvenId>()
                           .Add(typeof(PersonWithOddId))
                           .Add <PersonWithEmailName>()
                           .Add(typeof(PersonWithGenderProperty));

            // This person model has a gender, so the last middleware will be the one handling the input.
            var personModel = new PersonModel
            {
                Gender = Gender.Female
            };

            await pipeline.Execute(personModel);

            // Check if the level of 'personModel' is 4, which is configured by 'PersonWithGenderProperty' middleware.
            Assert.AreEqual(4, personModel.Level);
        }
Exemplo n.º 30
0
        public async Task AsyncPipeline_Execution_Test()
        {
            var types = new List <Type> {
                typeof(FooComponent), typeof(BarComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config);
            var result = await target.ExecuteAsync(new TestPayload());

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count == 2);
            Assert.IsTrue(result.FooStatus == $"{nameof(FooComponent)} executed!");
            Assert.IsTrue(result.BarStatus == $"{nameof(BarComponent)} executed!");
        }
Exemplo n.º 31
0
        public async Task Execute_RunSeveralMiddleware_SuccessfullyExecute()
        {
            var pipeline = new AsyncPipeline <PersonModel>(new ActivatorMiddlewareResolver())
                           .Add <PersonWithEvenId>()
                           .Add <PersonWithOddId>()
                           .Add <PersonWithEmailName>()
                           .Add <PersonWithGenderProperty>();

            // This person model has a name that matches the 'PersonWithEmailName' middleware.
            var personModel = new PersonModel
            {
                Name = "*****@*****.**"
            };

            await pipeline.Execute(personModel);

            // Check if the level of 'personModel' is 3, which is configured by 'PersonWithEmailName' middleware.
            Assert.AreEqual(3, personModel.Level);
        }
Exemplo n.º 32
0
        public void AsyncPipelineComponent_SettingNotFoundException_Test()
        {
            var types = new List <Type> {
                typeof(FooSettingNotFoundComponent)
            };
            var config = new Dictionary <string, IDictionary <string, string> >();

            foreach (var t in types)
            {
                config.Add(t.Name, new Dictionary <string, string> {
                    { "test", "value" }
                });
            }

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config);
            Func <Task <TestPayload> > act = () => target.ExecuteAsync(new TestPayload());

            act.Should().ThrowExactly <PipelineExecutionException>()
            .And.InnerException.Should().BeOfType <PipelineComponentSettingNotFoundException>();
        }
Exemplo n.º 33
0
 public void AsyncPipelineSimpleTest()
 {
     int step = 1000;
     var done = TimedCompletions(step, step, step);
     var pipeline = new AsyncPipeline(2);
     Stopwatch watch = new Stopwatch();
     watch.Start();
     pipeline.Add(done[0]);
     const int epsilon = 100;
     var elapsed0 = watch.ElapsedMilliseconds;
     Assert.IsTrue(elapsed0 < epsilon, elapsed0.ToString());
     pipeline.Add(done[2]);
     var elapsed1 = watch.ElapsedMilliseconds;
     Assert.IsTrue(elapsed1 < epsilon, elapsed1.ToString());
     pipeline.Add(done[1]);
     var elapsed2 = watch.ElapsedMilliseconds;
     Assert.IsTrue(step - epsilon <= elapsed2 && elapsed2 <= step + epsilon);
     pipeline.Wait();
     watch.Stop();
     Assert.IsTrue(3 * step - epsilon <= watch.ElapsedMilliseconds && watch.ElapsedMilliseconds <= 3 * step + epsilon);
 }
Exemplo n.º 34
0
        public void AsyncPipeline_Execution_Cancellation_Test()
        {
            var types = new List <Type> {
                typeof(DelayComponent), typeof(BarComponent)
            };
            var config = new Dictionary <string, IDictionary <string, string> >();

            foreach (var t in types)
            {
                config.Add(t.Name, new Dictionary <string, string> {
                    { "test", "value" }
                });
            }

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, config);
            Func <Task <TestPayload> > act = () => target.ExecuteAsync(new TestPayload(), cts.Token);

            act.Should().Throw <OperationCanceledException>();
        }
Exemplo n.º 35
0
        public void AsyncPipeline_ComponentReturnsNull_Test()
        {
            //Arrange
            PipelineComponentResolver.AddAsync(new NullTaskComponent());

            var types = new List <Type>
            {
                typeof(NullTaskComponent)
            };

            var target = new AsyncPipeline <TestPayload>(PipelineComponentResolver, types, null, null);

            //Act
            Func <Task> act = () => target.ExecuteAsync(new TestPayload());

            //Assert
            act.Should()
            .ThrowExactly <PipelineExecutionException>()
            .WithInnerExceptionExactly <InvalidOperationException>()
            .WithMessage($"AsyncPipelineComponent named '{nameof(NullTaskComponent)}' returned a null task.");
        }
Exemplo n.º 36
0
        public List<Task> ProcessEdges(
            Func<long, long, Task> action,
            Action<long> intervalAction = null,
            AsyncPipeline pipeline = null)
        {
            List<Task> promises = new List<Task>();
            long i = 0;
            foreach (XElement edgeData in Edges)
            {
                long fromUserId = Int64.Parse(edgeData.Attribute("source").Value);
                long toUserId = Int64.Parse(edgeData.Attribute("target").Value);

                Task n = action(fromUserId, toUserId);
                if (n != null)
                {
                    if (pipeline != null) pipeline.Add(n);
                    promises.Add(n);
                }
                else
                {
                    // skip this edge
                    continue;
                }


                if (intervalAction != null && ProgressInterval != 0)
                {
                    if ((++i % ProgressInterval) == 0)
                    {
                        // Call progress interval action
                        intervalAction(i);
                    }
                }
            }
            return promises;
        }
Exemplo n.º 37
0
        private Task Test_Stream_Churn_NumStreams_FewPublishers(
            string streamProviderName,
            int pipelineSize,
            int numStreams,
            int numConsumers = 9,
            int numProducers = 4,
            bool warmUpPubSub = true,
            bool warmUpProducers = false,
            bool normalSubscribeCalls = true)
        {
            Console.WriteLine("Testing churn with {0} Streams on {1} Producers with {2} Consumers per Stream",
                numStreams, numProducers, numConsumers);

            AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);

            // Create streamId Guids
            Guid[] streamIds = new Guid[numStreams];
            for (int i = 0; i < numStreams; i++)
            {
                streamIds[i] = Guid.NewGuid();
            }

            int activeConsumerGrains = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);
            Assert.AreEqual(0, activeConsumerGrains, "Initial Consumer count should be zero");
            int activeProducerGrains = ActiveGrainCount(typeof(StreamLifecycleProducerGrain).FullName);
            Assert.AreEqual(0, activeProducerGrains, "Initial Producer count should be zero");

            if (warmUpPubSub)
            {
                WarmUpPubSub(streamProviderName, streamIds, pipeline);

                pipeline.Wait();

                int activePubSubGrains = ActiveGrainCount(typeof(PubSubRendezvousGrain).FullName);
                Assert.AreEqual(streamIds.Length, activePubSubGrains, "Initial PubSub count -- should all be warmed up");
            }

            Guid[] producerIds = new Guid[numProducers];

            if (numProducers > 0 && warmUpProducers)
            {
                // Warm up Producers to pre-create grains
                for (int i = 0; i < numProducers; i++)
                {
                    producerIds[i] = Guid.NewGuid();
                    var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleProducerGrain>(producerIds[i]);
                    Task promise = grain.Ping();

                    pipeline.Add(promise);
                }
                pipeline.Wait();

                int activePublisherGrains = ActiveGrainCount(typeof(StreamLifecycleProducerGrain).FullName);
                Assert.AreEqual(numProducers, activePublisherGrains, "Initial Publisher count -- should all be warmed up");
            }

            var promises = new List<Task>();

            Stopwatch sw = Stopwatch.StartNew();

            if (numProducers > 0)
            {
                // Producers
                for (int i = 0; i < numStreams; i++)
                {
                    Guid streamId = streamIds[i];
                    Guid producerId = producerIds[i % numProducers];
                    var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleProducerGrain>(producerId);

                    Task promise = grain.BecomeProducer(streamId, this.StreamNamespace, streamProviderName);

                    promises.Add(promise);
                    pipeline.Add(promise);
                }
                pipeline.Wait();
                promises.Clear();
            }

            // Consumers
            for (int i = 0; i < numStreams; i++)
            {
                Guid streamId = streamIds[i];

                Task promise = SetupOneStream(streamId, streamProviderName, pipeline, numConsumers, 0, normalSubscribeCalls);
                promises.Add(promise);
            }
            pipeline.Wait();

            Task.WhenAll(promises).Wait();
            sw.Stop();

            int consumerCount = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);
            Assert.AreEqual(activeConsumerGrains + (numStreams * numConsumers), consumerCount, "The right number of Consumer grains are active");

            int producerCount = ActiveGrainCount(typeof(StreamLifecycleProducerGrain).FullName);
            Assert.AreEqual(activeProducerGrains + (numStreams * numProducers), producerCount, "The right number of Producer grains are active");

            int pubSubCount = ActiveGrainCount(typeof(PubSubRendezvousGrain).FullName);
            Assert.AreEqual(streamIds.Length, pubSubCount, "Final PubSub count -- no more started");

            TimeSpan elapsed = sw.Elapsed;
            int totalSubscriptions = numStreams * numConsumers;
            double rps = totalSubscriptions / elapsed.TotalSeconds;
            Console.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
            Assert.AreNotEqual(0.0, rps, "RPS greater than zero");
            return TaskDone.Done;
        }
Exemplo n.º 38
0
        public int Run()
        {
            this.perfCounters = new ChirperPerformanceCounters(this.GraphDataFile.FullName);
            perfCounters.ChirpsPerSecond.RawValue = 0;

            pipeline = new AsyncPipeline(PipelineLength);
            loader = new ChirperNetworkLoader(pipeline);
            //if (this.Verbose) loader.SetVerbose();

            Console.WriteLine("Loading Chirper network data file " + this.GraphDataFile.FullName);
            loader.FileToLoad = this.GraphDataFile;
            loader.LoadData();
            loader.CreateUserNodes(); // Connect/create users

            Console.WriteLine(
                "Starting Chirper network traffic simulation for {0} users.\n"
                + "Chirp publication time base = {1}\n"
                + "Random time distribution = {2}\n"
                + "Rechirp rate = {3}", 
                loader.Users.Count, this.ChirpPublishTimebase, this.ChirpPublishTimeRandom, this.ShouldRechirpRate);

            ForEachUser(user =>
            {
                SimulatedUser u = new SimulatedUser(user);
                u.ShouldRechirpRate = this.ShouldRechirpRate;
                u.ChirpPublishTimebase = this.ChirpPublishTimebase;
                u.ChirpPublishTimeRandom = this.ChirpPublishTimeRandom;
                u.Verbose = this.Verbose;
                lock (activeUsers)
                {
                    activeUsers.Add(u);
                }
                u.Start();
            });

            Console.WriteLine("Starting sending chirps...");

            Random rand = new Random();
            int count = 0;
            Stopwatch stopwatch = Stopwatch.StartNew();
            do
            {
                int i = rand.Next(activeUsers.Count);
                SimulatedUser u = activeUsers[i];
                if (u == null)
                {
                    Console.WriteLine("User {0} not found.", i);
                    return -1;
                }

                string msg = fortune.GetFortune();

                pipeline.Add(u.PublishMessage(msg));
                count++;
                if (count % 10000 == 0)
                {
                    Console.WriteLine("{0:0.#}/sec: {1} in {2}ms.  Pipeline contains {3} items.",
                        ((float)10000 / stopwatch.ElapsedMilliseconds) * 1000, count, stopwatch.ElapsedMilliseconds, pipeline.Count);
                    perfCounters.ChirpsPerSecond.RawValue = (int) (((float) 10000 / stopwatch.ElapsedMilliseconds) * 1000);

                    stopwatch.Restart();
                }

                if (ChirpPublishTimebase > 0)
                {
                    Thread.Sleep(ChirpPublishTimebase * 1000);
                }
            } while (true);
        }
Exemplo n.º 39
0
        public async Task<int> Run()
        {
            LogMessage("********************************************************");
            LogMessage("{0}\t{1:G}", "Network Load Starting at:", DateTime.Now);

            LoadData();

            LogMessage("Pipeline={0}", this.PipelineSize);
            if (pipeline == null)
                this.pipeline = new AsyncPipeline(this.PipelineSize);

            try
            {
                loadStopwatch.Start();

                LogMessage("Creating {0} user nodes...", loader.Nodes.Count);
                List<Task> work = CreateUserNodes();
                LogMessage("Waiting for {0} promises to complete.", work.Count);
                await Task.WhenAll(work);
                LogMessage("Finished creating {0} users in {1}", numUsers, runtimeStopwatch.Elapsed);

                LogMessage("Creating {0} user relationship links...", loader.Edges.Count);
                edgeStopwatch.Start();
                LogMessage("Processing user relationship links group #1");
                work = CreateRelationshipEdges(true);
                LogMessage("Waiting for {0} promises to complete.", work.Count);
                await Task.WhenAll(work);
                LogMessage("Processing user relationship links group #2");
                work = CreateRelationshipEdges(false);
                LogMessage("Waiting for {0} promises to complete.", work.Count);
                await Task.WhenAll(work);

                edgeStopwatch.Stop();
                LogMessage("Finished creating {0} user relationship links in {1}", numRelationships, runtimeStopwatch.Elapsed);
            }
            catch(Exception exc)
            {
                ReportError("Error creating Chirper data network", exc);
                throw exc.GetBaseException();
            }

            loadStopwatch.Stop();
            runtimeStopwatch.Stop();

            LogMessage(string.Empty);
            LogMessage("Loading Completed:");
            LogMessage("\t{0} users ({1} duplicates, {2} new)", this.numUsers, this.duplicateNodes, this.numUsers - this.duplicateNodes);
            LogMessage("\t{0} relationship links", this.numRelationships);
            LogMessage("\t{0} errors", this.numErrors);
            LogMessage(string.Empty);
            LogMessage("\tNode Processing Time:\t{0}", loadStopwatch.Elapsed - edgeStopwatch.Elapsed);
            LogMessage("\tEdge Processing Time:\t{0}", edgeStopwatch.Elapsed);
            LogMessage("\tTotal Load Time:\t{0}", loadStopwatch.Elapsed);

            LogMessage(string.Empty);
            LogMessage("Execution Time:\t\t{0}", runtimeStopwatch.Elapsed);
            LogMessage("Network Load Finished at:\t{0:G}", DateTime.Now);
            LogMessage(string.Empty);

            return 0;
        }
Exemplo n.º 40
0
        private Task Test_Stream_Churn_NumStreams(
            string streamProviderName,
            int pipelineSize,
            int numStreams,
            int numConsumers = 9,
            int numProducers = 1,
            bool warmUpPubSub = true,
            bool normalSubscribeCalls = true)
        {
            Console.WriteLine("Testing churn with {0} Streams with {1} Consumers and {2} Producers per Stream NormalSubscribe={3}",
                numStreams, numConsumers, numProducers, normalSubscribeCalls);

            AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);
            var promises = new List<Task>();

            // Create streamId Guids
            Guid[] streamIds = new Guid[numStreams];
            for (int i = 0; i < numStreams; i++)
            {
                streamIds[i] = Guid.NewGuid();
            }

            if (warmUpPubSub)
            {
                WarmUpPubSub(streamProviderName, streamIds, pipeline);

                pipeline.Wait();

                int activePubSubGrains = ActiveGrainCount(typeof(PubSubRendezvousGrain).FullName);
                Assert.AreEqual(streamIds.Length, activePubSubGrains, "Initial PubSub count -- should all be warmed up");
            }
            
            int activeConsumerGrains = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);
            Assert.AreEqual(0, activeConsumerGrains, "Initial Consumer count should be zero");

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < numStreams; i++)
            {
                Task promise = SetupOneStream(streamIds[i], streamProviderName, pipeline, numConsumers, numProducers, normalSubscribeCalls);
                promises.Add(promise);
            }
            Task.WhenAll(promises).Wait();
            sw.Stop();

            int consumerCount = ActiveGrainCount(typeof(StreamLifecycleConsumerGrain).FullName);
            Assert.AreEqual(activeConsumerGrains + (numStreams * numConsumers), consumerCount, "The correct number of new Consumer grains are active");

            TimeSpan elapsed = sw.Elapsed;
            int totalSubscriptions = numStreams * numConsumers;
            double rps = totalSubscriptions / elapsed.TotalSeconds;
            Console.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
            Assert.AreNotEqual(0.0, rps, "RPS greater than zero");
            return TaskDone.Done;
        }
Exemplo n.º 41
0
        private Task SetupOneStream(
            Guid streamId, string streamProviderName,
            AsyncPipeline pipeline,
            int numConsumers,
            int numProducers,
            bool normalSubscribeCalls)
        {
            //Console.WriteLine("Initializing Stream {0} with Consumers={1} Producers={2}", streamId, numConsumers, numProducers);

            List<Task> promises = new List<Task>();

            if (producersFirst && numProducers > 0)
            {
                // Producers
                var p1 = SetupProducers(streamId, this.StreamNamespace, streamProviderName, pipeline, numProducers);
                promises.AddRange(p1);
            }

            // Consumers
            if (numConsumers > 0)
            {
                var c = SetupConsumers(streamId, this.StreamNamespace, streamProviderName, pipeline, numConsumers, normalSubscribeCalls);
                promises.AddRange(c);
            }

            if (!producersFirst && numProducers > 0)
            {
                // Producers
                var p2 = SetupProducers(streamId, this.StreamNamespace, streamProviderName, pipeline, numProducers);
                promises.AddRange(p2);
            }

            return Task.WhenAll(promises);
        }
Exemplo n.º 42
0
        //private async Task Test_Stream_Churn_TimePeriod(
        //    string streamProviderName,
        //    int pipelineSize,
        //    TimeSpan duration,
        //    int numConsumers = 9,
        //    int numProducers = 1)
        //{
        //    Console.WriteLine("Testing Subscription churn for duration {0} with {1} Consumers and {2} Producers per Stream",
        //        duration, numConsumers, numProducers);

        //    AsyncPipeline pipeline = new AsyncPipeline(pipelineSize);
        //    var promises = new List<Task>();

        //    Stopwatch sw = Stopwatch.StartNew();

        //    for (int i = 0; sw.Elapsed <= duration; i++)
        //    {
        //        Guid streamId = Guid.NewGuid();
        //        Task promise = SetupOneStream(streamId, streamProviderName, pipeline, numConsumers, numProducers);
        //        promises.Add(promise);
        //    }
        //    await Task.WhenAll(promises);
        //    sw.Stop();
        //    TimeSpan elapsed = sw.Elapsed;
        //    int totalSubscription = numSt* numConsumers);
        //    double rps = totalSubscription/elapsed.TotalSeconds;
        //    Console.WriteLine("Subscriptions-per-second = {0} during period {1}", rps, elapsed);
        //    Assert.AreNotEqual(0.0, rps, "RPS greater than zero");
        //}

        private void WarmUpPubSub(string streamProviderName, Guid[] streamIds, AsyncPipeline pipeline)
        {
            int numStreams = streamIds.Length;
            // Warm up PubSub for the appropriate streams
            for (int i = 0; i < numStreams; i++)
            {
                Guid streamId = streamIds[i];
                string extKey = streamProviderName + "_" + StreamNamespace;

                IPubSubRendezvousGrain pubsub = GrainClient.GrainFactory.GetGrain<IPubSubRendezvousGrain>(streamId, extKey, null);

                Task promise = pubsub.Validate();

                pipeline.Add(promise);
            }
            pipeline.Wait();
        }
Exemplo n.º 43
0
        private async Task AsyncPipelineBlackBoxConsistencyTest(int workerCount)
        {
            if (workerCount < 1)
                throw new ArgumentOutOfRangeException("You must specify at least one worker.", "workerCount");

            int loopCount = _iterationCount / workerCount;
            const double variance = 0.1;
            int expectedTasksCompleted = loopCount * workerCount;
            var delayLength = TimeSpan.FromSeconds(1);
            const int pipelineCapacity = _defaultPipelineCapacity;
            var pipeline = new AsyncPipeline(pipelineCapacity);
            int tasksCompleted = 0;
            // the following value is wrapped within an array to avoid a modified closure warning from ReSharper.
            int[] pipelineSize = { 0 };
            var capacityReached = new InterlockedFlag();

            Action workFunc =
                () =>
                {
                    var sz = Interlocked.Increment(ref pipelineSize[0]);
                    CheckPipelineState(sz, pipelineCapacity, capacityReached);
                    Task.Delay(delayLength).Wait();
                    Interlocked.Decrement(ref pipelineSize[0]);
                    Interlocked.Increment(ref tasksCompleted);
                };

            Action workerFunc =
                () =>
                {
                    for (var j = 0; j < loopCount; j++)
                    {
                        Task task = new Task(workFunc);
                        pipeline.Add(task, whiteBox: null);
                        task.Start();
                    }
                };

            Func<Task> monitorFunc =
                async () =>
                {
                    var delay = TimeSpan.FromSeconds(5);
                    while (tasksCompleted < expectedTasksCompleted)
                    {
                        output.WriteLine("test in progress: tasksCompleted = {0}.", tasksCompleted);
                        await Task.Delay(delay);
                    }
                };

            var workers = new Task[workerCount];
            var stopwatch = Stopwatch.StartNew();
            for (var i = 0; i < workerCount; ++i)
                workers[i] = Task.Run(workerFunc);
            Task.Run(monitorFunc).Ignore();
            await Task.WhenAll(workers);
            pipeline.Wait();
            stopwatch.Stop();
            Assert.AreEqual(expectedTasksCompleted, tasksCompleted, "The test did not complete the expected number of tasks.");

            var targetTimeSec = expectedTasksCompleted * delayLength.TotalSeconds / pipelineCapacity;
            var minTimeSec = (1.0 - variance) * targetTimeSec;
            var maxTimeSec = (1.0 + variance) * targetTimeSec;
            var actualSec = stopwatch.Elapsed.TotalSeconds;
            output.WriteLine(
                "Test finished in {0} sec, {1}% of target time {2} sec. Permitted variance is +/-{3}%",
                actualSec,
                actualSec / targetTimeSec * 100,
                targetTimeSec,
                variance * 100);

            Assert.IsTrue(capacityReached.IsSet, "Pipeline capacity not reached; the delay length probably is too short to be useful.");
            Assert.IsTrue(
                actualSec >= minTimeSec, 
                string.Format("The unit test completed too early ({0} sec < {1} sec).", actualSec, minTimeSec));
            Assert.IsTrue(
                actualSec <= maxTimeSec, 
                string.Format("The unit test completed too late ({0} sec > {1} sec).", actualSec, maxTimeSec));
        }
Exemplo n.º 44
0
        private static IList<Task> SetupProducers(Guid streamId, string streamNamespace, string streamProviderName, AsyncPipeline pipeline, int numProducers)
        {
            var producers = new List<IStreamLifecycleProducerGrain>();
            var promises = new List<Task>();

            for (int loopCount = 0; loopCount < numProducers; loopCount++)
            {
                var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleProducerGrain>(Guid.NewGuid());
                producers.Add(grain);

                Task promise = grain.BecomeProducer(streamId, streamNamespace, streamProviderName);

                if (loopCount == 0)
                {
                    // First call for this stream, so wait for call to complete successfully so we know underlying infrastructure is set up.
                    promise.Wait();
                }
                promises.Add(promise);
                pipeline.Add(promise);
            }

            return promises;
        }
Exemplo n.º 45
0
        private static IList<Task> SetupConsumers(Guid streamId, string streamNamespace, string streamProviderName, AsyncPipeline pipeline, int numConsumers, bool normalSubscribeCalls)
        {
            var consumers = new List<IStreamLifecycleConsumerGrain>();
            var promises = new List<Task>();

            long consumerIdStart = random.Next();
            for (int loopCount = 0; loopCount < numConsumers; loopCount++)
            {
                var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleConsumerGrain>(Guid.NewGuid());
                consumers.Add(grain);

                Task promise; 
                if (normalSubscribeCalls)
                {
                    promise = grain.BecomeConsumer(streamId, streamNamespace, streamProviderName);
                }
                else
                {
                    promise = grain.TestBecomeConsumerSlim(streamId, streamNamespace, streamProviderName);
                }

                //if (loopCount == 0)
                //{
                //    // First call for this stream, so wait for call to complete successfully so we know underlying infrastructure is set up.
                //    promise.Wait();
                //}
                promises.Add(promise);
                pipeline.Add(promise);
            }

            return promises;
        }
Exemplo n.º 46
0
        private async Task<double> TestOneStream(Guid streamId, string streamProviderName,
            int numProducers, int numConsumers, int numMessages,
            bool useFanOut = true)
        {
            Console.WriteLine("Testing Stream {0} with Producers={1} Consumers={2} x {3} messages",
                streamId, numProducers, numConsumers, numMessages);

            Stopwatch sw = Stopwatch.StartNew();

            List<IStreamLifecycleConsumerGrain> consumers = new List<IStreamLifecycleConsumerGrain>();
            List<IStreamLifecycleProducerGrain> producers = new List<IStreamLifecycleProducerGrain>();

            await InitializeTopology(streamId, this.StreamNamespace, streamProviderName,
                numProducers, numConsumers,
                producers, consumers, useFanOut);

            var promises = new List<Task>();

            // Producers send M message each
            int item = 1;
            AsyncPipeline pipeline = new AsyncPipeline(MessagePipelineSize);
            foreach (var grain in producers)
            {
                for (int m = 0; m < numMessages; m++)
                {
                    Task promise = grain.SendItem(item++);

                    if (useFanOut)
                    {
                        pipeline.Add(promise);
                        promises.Add(promise);
                    }
                    else
                    {
                        await promise;
                    }
                }
            }
            if (useFanOut)
            {
                //Console.WriteLine("Test: Waiting for {0} producers to finish sending {1} messages", producers.Count, promises.Count);
                await Task.WhenAll(promises);
                promises.Clear();
            }

            var pubSub = StreamTestUtils.GetStreamPubSub();

            // Check Consumer counts
            int consumerCount = await pubSub.ConsumerCount(streamId, streamProviderName, StreamNamespace);
            Assert.AreEqual(numConsumers, consumerCount, "ConsumerCount for Stream {0}", streamId);

            // Check Producer counts
            int producerCount = await pubSub.ProducerCount(streamId, streamProviderName, StreamNamespace);
            Assert.AreEqual(numProducers, producerCount, "ProducerCount for Stream {0}", streamId);

            // Check message counts received by consumers
            int totalMessages = (numMessages + 1) * numProducers;
            foreach (var grain in consumers)
            {
                int count = await grain.GetReceivedCount();
                Assert.AreEqual(totalMessages, count, "ReceivedCount for Consumer grain {0}", grain.GetPrimaryKey());
            }

            double rps = totalMessages/sw.Elapsed.TotalSeconds;
            //Console.WriteLine("Sent {0} messages total from {1} Producers to {2} Consumers in {3} at {4} RPS",
            //    totalMessages, numProducers, numConsumers,
            //    sw.Elapsed, rps);

            return rps;
        }
Exemplo n.º 47
0
        private static async Task InitializeTopology(Guid streamId, string streamNamespace, string streamProviderName,
            int numProducers, int numConsumers,
            List<IStreamLifecycleProducerGrain> producers, List<IStreamLifecycleConsumerGrain> consumers,
            bool useFanOut)
        {
            long nextGrainId = random.Next();

            //var promises = new List<Task>();
            AsyncPipeline pipeline = new AsyncPipeline(InitPipelineSize);

            // Consumers
            long consumerIdStart = nextGrainId;
            for (int loopCount = 0; loopCount < numConsumers; loopCount++)
            {
                var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleConsumerGrain>(Guid.NewGuid());
                consumers.Add(grain);

                Task promise = grain.BecomeConsumer(streamId, streamNamespace, streamProviderName);

                if (useFanOut)
                {
                    pipeline.Add(promise);
                    //promises.Add(promise);

                    //if (loopCount%WaitBatchSize == 0)
                    //{
                    //    Console.WriteLine("InitializeTopology: Waiting for {0} consumers to initialize", promises.Count);
                    //    await Task.WhenAll(promises);
                    //    promises.Clear();
                    //}
                }
                else
                {
                    await promise;
                }
            }
            if (useFanOut)
            {
                //Console.WriteLine("InitializeTopology: Waiting for {0} consumers to initialize", promises.Count);
                //await Task.WhenAll(promises);
                //promises.Clear();
                //Console.WriteLine("InitializeTopology: Waiting for {0} consumers to initialize", pipeline.Count);
                pipeline.Wait();
            }
            nextGrainId += numConsumers;

            // Producers
            long producerIdStart = nextGrainId;
            pipeline = new AsyncPipeline(InitPipelineSize);
            for (int loopCount = 0; loopCount < numProducers; loopCount++)
            {
                var grain = GrainClient.GrainFactory.GetGrain<IStreamLifecycleProducerGrain>(Guid.NewGuid());
                producers.Add(grain);

                Task promise = grain.BecomeProducer(streamId, streamNamespace, streamProviderName);

                if (useFanOut)
                {
                    pipeline.Add(promise);
                    //promises.Add(promise);
                }
                else
                {
                    await promise;
                }
            }
            if (useFanOut)
            {
                //Console.WriteLine("InitializeTopology: Waiting for {0} producers to initialize", promises.Count);
                //await Task.WhenAll(promises);
                //promises.Clear();
                //Console.WriteLine("InitializeTopology: Waiting for {0} producers to initialize", pipeline.Count);
                pipeline.Wait();
            }
            //nextGrainId += numProducers;
        }