public void block_start_and_end_is_reported(ResolverType resolverType) { // Arrange BlockStartedEvent?startedEvent = null; BlockEndedEvent? endedEvent = null; var events = new PipelineExecutionEvents(); events.BlockStarted += x => startedEvent = x; events.BlockEnded += x => endedEvent = x; var block = new PipelineBlock <int>( "Block", x => { }); var task = new TaskDefinition( new PipelineTaskStep <int>( "Step", new[] { 0 }, new List <PipelineBlock <int> > { block })); // Act task.Execute(resolverType, events: events); // Assert startedEvent.Should().NotBeNull(); startedEvent.Value.Block.Should().BeSameAs(block); endedEvent.Should().NotBeNull(); endedEvent.Value.Block.Should().BeSameAs(block); endedEvent.Value.Duration.Should().BePositive(); }
public void GivenPipelineBlock_WhenSendMessage_ShouldReceiveMessage() { const int max = 100; var list = new List <int>(); var list2 = new List <int>(); var jobs = new PipelineBlock <int>() .Broadcast() .DoAction(x => list.Add(x)) .DoAction(x => list2.Add(x + 1000)); Enumerable.Range(0, max) .ForEach(x => jobs.Post(x).Verify().Assert(x => x == true, "Failed to post")); jobs.Complete(); jobs.Completion.Wait(); list.Count.Should().Be(max); list2.Count.Should().Be(max); list .Select((i, x) => new { i, x }) .All(x => x.i == x.x) .Should().BeTrue(); list2 .Select((x, i) => new { i, x }) .All(x => x.i + 1000 == x.x) .Should().BeTrue(); }
public void GivenPipelineBlock_ApplyTransform_ShouldPass() { const int max = 1000; const int offset = 10000; var list = new List <int>(); var list2 = new List <int>(); var jobs = new PipelineBlock <int>() .Select(x => x + 5) .Broadcast() .DoAction(x => list.Add(x)) .DoAction(x => list2.Add(x + offset)); Enumerable.Range(0, max) .ForEach(async x => await jobs.Send(x)); jobs.Complete(); jobs.Completion.Wait(); list.Count.Should().Be(max); list2.Count.Should().Be(max); list .Select((x, i) => new { x, i }) .All(x => x.x == x.i + 5) .Should().BeTrue(); list2 .Select((x, i) => new { i, x }) .All(x => x.i + offset + 5 == x.x) .Should().BeTrue(); }
public void GivenBuffer_ApplyTransformAndTwoSubpiplines_ShouldPass() { const int max = 1000; const int offset = 10000; var list = new List <int>(); var list2 = new List <int>(); var s1 = new List <int>(); var s1Pipeline = new PipelineBlock <int>().DoAction(x => s1.Add(x)); var s2 = new List <int>(); var s2Pipeline = new PipelineBlock <int>().DoAction(x => s2.Add(x)); var jobs = new PipelineBlock <int>() .Register(s1Pipeline) .Register(s2Pipeline) .Select(x => x + 5) .Broadcast() .DoAction(x => list.Add(x)) .DoAction(x => list2.Add(x + offset)) .DoAction(x => s1Pipeline.Post(x)) .DoAction(x => s2Pipeline.Post(x)); Enumerable.Range(0, max) .ForEach(async x => await jobs.Send(x)); jobs.CompleteAndWait().Wait(); list.Count.Should().Be(max); list2.Count.Should().Be(max); s1.Count.Should().Be(max); s2.Count.Should().Be(max); list .Select((x, i) => new { x, i }) .All(x => x.x == x.i + 5) .Should().BeTrue(); list2 .Select((x, i) => new { i, x }) .All(x => x.i + offset + 5 == x.x) .Should().BeTrue(); s1 .Select((x, i) => new { x, i }) .All(x => x.x == x.i + 5) .Should().BeTrue(); s2 .Select((x, i) => new { x, i }) .All(x => x.x == x.i + 5) .Should().BeTrue(); }
public void GivenSimplePipeline_WhenPost_ShouldProcessMessage() { const int postValue = 100; var s1 = new List <int>(); var s1Pipeline = new PipelineBlock <int>().DoAction(x => s1.Add(x)); s1Pipeline.Post(postValue); s1Pipeline.Complete(); s1Pipeline.Completion.Wait(); s1.Count.Should().Be(1); s1[0].Should().Be(postValue); }
public void GivenRegisteredPipeline_WhenCompleteAndWait_ShouldProcessAllMessage() { const int postValue = 100; var s1 = new List <int>(); var s1Pipeline = new PipelineBlock <int>().DoAction(x => s1.Add(x)); var job = new PipelineBlock <int>() .Register(s1Pipeline) .DoAction(x => s1Pipeline.Post(x)); job.Post(postValue); job.CompleteAndWait().Wait(); s1.Count.Should().Be(1); s1[0].Should().Be(postValue); }
public void GivenPipelineBlock_WhenPostMessage_ShouldReceiveMessage() { const int max = 10; int count = 0; var list = new List <string>(); var pipeline = new PipelineBlock <string>() .Broadcast() .DoAction(x => count++) .DoAction(x => list.Add(x)); Enumerable.Range(0, max) .ForEach(x => pipeline.Post($"{x} message")); pipeline.Complete(); pipeline.Completion.Wait(); count.Should().Be(max); list.Count.Should().Be(max); }
private TransformBlock <PipelineItem <TItem>, PipelineItem <TItem> > BuildProcessingBlock <TItem>( PipelineBlock <TItem> block, PipelineTaskStep <TItem> step, DataflowExecutionContext context, CancellationToken cancellation) { return(new TransformBlock <PipelineItem <TItem>, PipelineItem <TItem> >( x => { if (x == null) { return x; } context.Events?.Raise(e => e.OnBlockStarted(block, x.Number, x.Item, step, context.StepContext.Task)); var sw = Stopwatch.StartNew(); try { block.Body(x.Item); } catch (Exception e) { throw new TaskExecutionException(e, step, block.GetExceptionData(x.Number)); } sw.Stop(); context.TotalBlockDurations.AddOrUpdate(block.Name, sw.Elapsed, (_, duration) => duration + sw.Elapsed); context.Events?.Raise(e => e.OnBlockEnded(block, x.Number, x.Item, step, context.StepContext.Task, sw.Elapsed)); return x; }, new ExecutionDataflowBlockOptions { NameFormat = block.Name, MaxDegreeOfParallelism = block.MaxDegreeOfParallelism, BoundedCapacity = block.MaxDegreeOfParallelism, EnsureOrdered = true, CancellationToken = cancellation })); }
public void pipeline_end_is_reported(ResolverType resolverType) { // Arrange PipelineEndedEvent?endedEvent = null; var events = new PipelineExecutionEvents(); events.PipelineEnded += x => endedEvent = x; var block1 = new PipelineBlock <int>( "Block1", x => { }); var block2 = new PipelineBlock <int>( "Block1", x => { }); var task = new TaskDefinition( new PipelineTaskStep <int>( "Step", new[] { 0 }, 1, new List <PipelineBlock <int> > { block1, block2 })); // Act task.Execute(resolverType, events: events); // Assert endedEvent.Should().NotBeNull(); endedEvent.Value.Step.Should().BeSameAs(task.Steps[0]); endedEvent.Value.TotalInputMaterializationDuration.Should().BePositive(); endedEvent.Value.TotalBlockDurations.Should().NotBeNull(); var totalBlockDurations = endedEvent.Value.TotalBlockDurations.ToDictionary(x => x.Key, x => x.Value); totalBlockDurations.Should().ContainKey(block1.Name).WhichValue.Should().BePositive(); totalBlockDurations.Should().ContainKey(block2.Name).WhichValue.Should().BePositive(); }
/// <summary> /// Initializes a new instance of the <see cref="EffectPass" /> class. /// </summary> /// <param name="logger">The logger used to log errors.</param> /// <param name="effect"> The effect. </param> /// <param name="pass"> The pass. </param> /// <param name="name"> The name. </param> internal EffectPass(Logger logger, Effect effect, EffectTechnique technique, EffectData.Pass pass, string name) : base(name) { this.Technique = technique; this.pass = pass; this.Effect = effect; this.graphicsDevice = effect.GraphicsDevice; pipeline = new PipelineBlock() { Stages = new StageBlock[EffectPass.StageCount], }; Attributes = PrepareAttributes(logger, pass.Attributes); IsSubPass = pass.IsSubPass; // Don't create SubPasses collection for subpass. if (!IsSubPass) SubPasses = new EffectPassCollection(); }
public PipelineTaskStepBuilder <TItem> WithBlock( PipelineBlock <TItem> block) { _blocks.Add(block); return(this); }