|
| 1 | +using AdvancedCsharpConcepts.Advanced.HighPerformance; |
| 2 | +using System.Diagnostics; |
| 3 | + |
| 4 | +namespace AdvancedCsharpConcepts.IntegrationTests; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Integration tests for high-performance patterns. |
| 8 | +/// Tests real-world scenarios combining multiple components. |
| 9 | +/// </summary> |
| 10 | +public class PerformanceIntegrationTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void ParallelProcessing_LargeDataset_ShouldBeFasterThanSequential() |
| 14 | + { |
| 15 | + // Arrange |
| 16 | + const int dataSize = 1_000_000; |
| 17 | + var sw = Stopwatch.StartNew(); |
| 18 | + |
| 19 | + // Act - Sequential |
| 20 | + sw.Restart(); |
| 21 | + var seqResult = ParallelProcessingExamples.SequentialSum(dataSize); |
| 22 | + var seqTime = sw.ElapsedMilliseconds; |
| 23 | + |
| 24 | + // Act - Parallel |
| 25 | + sw.Restart(); |
| 26 | + var parResult = ParallelProcessingExamples.ParallelForSum(dataSize); |
| 27 | + var parTime = sw.ElapsedMilliseconds; |
| 28 | + |
| 29 | + // Assert |
| 30 | + parResult.Should().Be(seqResult); |
| 31 | + parTime.Should().BeLessThan(seqTime); // Parallel should be faster |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void SpanMemory_ParsingLargeCSV_ShouldBeMoreEfficientThanTraditional() |
| 36 | + { |
| 37 | + // Arrange |
| 38 | + var numbers = string.Join(",", Enumerable.Range(1, 1000)); |
| 39 | + |
| 40 | + // Act |
| 41 | + var traditional = SpanMemoryExamples.ParseNumbersTraditional(numbers); |
| 42 | + var modern = SpanMemoryExamples.ParseNumbersModern(numbers); |
| 43 | + |
| 44 | + // Assert |
| 45 | + traditional.Should().Equal(modern); |
| 46 | + traditional.Should().HaveCount(1000); |
| 47 | + traditional.Should().Equal(Enumerable.Range(1, 1000)); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public async Task AsyncMemoryOperations_ShouldWorkCorrectly() |
| 52 | + { |
| 53 | + // Act |
| 54 | + var result = await SpanMemoryExamples.AsyncMemoryExample(); |
| 55 | + |
| 56 | + // Assert |
| 57 | + result.Should().Be(499500); // Sum of 0..999 |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void MatrixMultiplication_Parallel_ShouldProduceCorrectResults() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + var matrixA = new double[10, 10]; |
| 65 | + var matrixB = new double[10, 10]; |
| 66 | + |
| 67 | + for (int i = 0; i < 10; i++) |
| 68 | + { |
| 69 | + for (int j = 0; j < 10; j++) |
| 70 | + { |
| 71 | + matrixA[i, j] = i + j; |
| 72 | + matrixB[i, j] = i * j; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Act |
| 77 | + var result = ParallelProcessingExamples.ParallelMatrixMultiply(matrixA, matrixB); |
| 78 | + |
| 79 | + // Assert |
| 80 | + result.Should().NotBeNull(); |
| 81 | + result.GetLength(0).Should().Be(10); |
| 82 | + result.GetLength(1).Should().Be(10); |
| 83 | + } |
| 84 | + |
| 85 | + [Theory] |
| 86 | + [InlineData(100)] |
| 87 | + [InlineData(1000)] |
| 88 | + [InlineData(10000)] |
| 89 | + public void ParallelPipeline_DifferentSizes_ShouldProduceCorrectResults(int size) |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var input = Enumerable.Range(1, size); |
| 93 | + |
| 94 | + // Act |
| 95 | + var result = ParallelProcessingExamples.ParallelPipeline(input); |
| 96 | + |
| 97 | + // Assert - Should contain only even squares |
| 98 | + result.Should().AllSatisfy(x => (x % 2).Should().Be(0)); |
| 99 | + result.Should().AllSatisfy(x => Math.Sqrt(x).Should().BeGreaterThan(0)); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void CombinedPatterns_SpanWithParallel_ShouldWorkTogether() |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + var data = Enumerable.Range(0, 100).ToArray(); |
| 107 | + |
| 108 | + // Act - Use Span for zero-allocation access |
| 109 | + var span = data.AsSpan(); |
| 110 | + |
| 111 | + // Process chunks in parallel |
| 112 | + var sum = 0; |
| 113 | + System.Threading.Tasks.Parallel.For(0, 4, i => |
| 114 | + { |
| 115 | + var chunkSize = span.Length / 4; |
| 116 | + var chunk = span.Slice(i * chunkSize, chunkSize); |
| 117 | + var localSum = 0; |
| 118 | + foreach (var val in chunk) |
| 119 | + { |
| 120 | + localSum += val; |
| 121 | + } |
| 122 | + System.Threading.Interlocked.Add(ref sum, localSum); |
| 123 | + }); |
| 124 | + |
| 125 | + // Assert |
| 126 | + sum.Should().Be(Enumerable.Range(0, 100).Sum()); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void RealWorldScenario_DataProcessingPipeline() |
| 131 | + { |
| 132 | + // Arrange - Simulate real-world data processing |
| 133 | + var csvData = string.Join(",", Enumerable.Range(1, 10000).Select(x => x.ToString())); |
| 134 | + |
| 135 | + // Act - Parse, transform, and aggregate |
| 136 | + var numbers = SpanMemoryExamples.ParseNumbersModern(csvData); |
| 137 | + var transformed = numbers.AsParallel() |
| 138 | + .Select(x => x * 2) |
| 139 | + .Where(x => x % 10 == 0) |
| 140 | + .ToArray(); |
| 141 | + |
| 142 | + // Assert |
| 143 | + transformed.Should().NotBeEmpty(); |
| 144 | + transformed.Should().AllSatisfy(x => (x % 10).Should().Be(0)); |
| 145 | + } |
| 146 | +} |
0 commit comments