|
8 | 8 |
|
9 | 9 |
|
10 | 10 | [MemoryDiagnoser] |
11 | | -[ThreadingDiagnoser] |
12 | 11 | public class ByteStreamTest |
13 | 12 | { |
14 | 13 | Guid Guid = Guid.NewGuid(); |
15 | 14 | byte[] byteBuffer = new byte[34]; |
16 | | - int initialBuffer = 512; |
| 15 | + byte[] readByteBuffer = new byte[34]; |
| 16 | + int initialBuffer = 4096; |
| 17 | + byte[] intData = Array.Empty<byte>(); |
| 18 | + byte[] byteData = Array.Empty<byte>(); |
| 19 | + byte[] guidData = Array.Empty<byte>(); |
| 20 | + byte[] stringData = Array.Empty<byte>(); |
17 | 21 |
|
18 | 22 | [GlobalSetup] |
19 | 23 | public void Setup() |
20 | 24 | { |
| 25 | + { |
| 26 | + var writer = ByteStream.Create( initialBuffer ); |
| 27 | + try |
| 28 | + { |
| 29 | + for ( int i = 0; i < 512; i++ ) |
| 30 | + { |
| 31 | + writer.Write( i ); |
| 32 | + } |
| 33 | + |
| 34 | + intData = writer.ToArray(); |
| 35 | + } |
| 36 | + finally |
| 37 | + { |
| 38 | + writer.Dispose(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + { |
| 43 | + var writer = ByteStream.Create( initialBuffer ); |
| 44 | + try |
| 45 | + { |
| 46 | + for ( int i = 0; i < 512; i++ ) |
| 47 | + { |
| 48 | + writer.Write( byteBuffer ); |
| 49 | + } |
| 50 | + |
| 51 | + byteData = writer.ToArray(); |
| 52 | + } |
| 53 | + finally |
| 54 | + { |
| 55 | + writer.Dispose(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + var writer = ByteStream.Create( initialBuffer ); |
| 61 | + try |
| 62 | + { |
| 63 | + for ( int i = 0; i < 512; i++ ) |
| 64 | + { |
| 65 | + writer.Write( Guid ); |
| 66 | + } |
21 | 67 |
|
| 68 | + guidData = writer.ToArray(); |
| 69 | + } |
| 70 | + finally |
| 71 | + { |
| 72 | + writer.Dispose(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + { |
| 77 | + var writer = ByteStream.Create( initialBuffer ); |
| 78 | + try |
| 79 | + { |
| 80 | + for ( int i = 0; i < 512; i++ ) |
| 81 | + { |
| 82 | + writer.Write( "Hello there" ); |
| 83 | + } |
| 84 | + |
| 85 | + stringData = writer.ToArray(); |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + writer.Dispose(); |
| 90 | + } |
| 91 | + } |
22 | 92 | } |
23 | 93 |
|
24 | 94 | [Benchmark] |
@@ -65,6 +135,63 @@ public void ByteStreamString() |
65 | 135 | } |
66 | 136 | } |
67 | 137 |
|
| 138 | + [Benchmark] |
| 139 | + public int ByteStreamReadInt() |
| 140 | + { |
| 141 | + using var reader = ByteStream.CreateReader( intData ); |
| 142 | + int sum = 0; |
| 143 | + |
| 144 | + for ( int i = 0; i < 512; i++ ) |
| 145 | + { |
| 146 | + sum += reader.Read<int>(); |
| 147 | + } |
| 148 | + |
| 149 | + return sum; |
| 150 | + } |
| 151 | + |
| 152 | + [Benchmark] |
| 153 | + public int ByteStreamReadByte() |
| 154 | + { |
| 155 | + using var reader = ByteStream.CreateReader( byteData ); |
| 156 | + int sum = 0; |
| 157 | + for ( int i = 0; i < 512; i++ ) |
| 158 | + { |
| 159 | + reader.Read( readByteBuffer, 0, readByteBuffer.Length ); |
| 160 | + sum += readByteBuffer[0]; |
| 161 | + } |
| 162 | + |
| 163 | + return sum; |
| 164 | + } |
| 165 | + |
| 166 | + [Benchmark] |
| 167 | + public int ByteStreamReadGuid() |
| 168 | + { |
| 169 | + using var reader = ByteStream.CreateReader( guidData ); |
| 170 | + int hash = 0; |
| 171 | + |
| 172 | + for ( int i = 0; i < 512; i++ ) |
| 173 | + { |
| 174 | + hash ^= reader.Read<Guid>().GetHashCode(); |
| 175 | + } |
| 176 | + |
| 177 | + return hash; |
| 178 | + } |
| 179 | + |
| 180 | + [Benchmark] |
| 181 | + public int ByteStreamReadString() |
| 182 | + { |
| 183 | + using var reader = ByteStream.CreateReader( stringData ); |
| 184 | + int totalLength = 0; |
| 185 | + |
| 186 | + for ( int i = 0; i < 512; i++ ) |
| 187 | + { |
| 188 | + var value = reader.Read<string>(); |
| 189 | + totalLength += value?.Length ?? 0; |
| 190 | + } |
| 191 | + |
| 192 | + return totalLength; |
| 193 | + } |
| 194 | + |
68 | 195 | [Benchmark] |
69 | 196 | public void PooledMemoryStreamInt() |
70 | 197 | { |
@@ -173,3 +300,87 @@ protected override void Dispose( bool disposing ) |
173 | 300 | } |
174 | 301 |
|
175 | 302 | } |
| 303 | + |
| 304 | +[MemoryDiagnoser] |
| 305 | +public class ByteStreamPoolingBenchmarks |
| 306 | +{ |
| 307 | + const int BatchSize = 100; |
| 308 | + readonly int[] randomInitialSizes = new int[BatchSize]; |
| 309 | + readonly byte[][] payloads = new byte[BatchSize][]; |
| 310 | + readonly byte[] mediumPayload = new byte[4096]; |
| 311 | + readonly byte[] largePayload = new byte[64 * 1024]; |
| 312 | + |
| 313 | + [GlobalSetup] |
| 314 | + public void Setup() |
| 315 | + { |
| 316 | + var rng = new Random( 42 ); |
| 317 | + |
| 318 | + for ( int i = 0; i < BatchSize; i++ ) |
| 319 | + { |
| 320 | + // Keep sizes inside typical pool buckets to surface contention |
| 321 | + var size = rng.Next( 64, 64 * 1024 ); |
| 322 | + randomInitialSizes[i] = size; |
| 323 | + |
| 324 | + var payloadLength = rng.Next( 16, 512 ); |
| 325 | + var payload = new byte[payloadLength]; |
| 326 | + rng.NextBytes( payload ); |
| 327 | + payloads[i] = payload; |
| 328 | + } |
| 329 | + |
| 330 | + rng.NextBytes( mediumPayload ); |
| 331 | + rng.NextBytes( largePayload ); |
| 332 | + } |
| 333 | + |
| 334 | + [Benchmark( Description = "Create 100 random streams sequentially" )] |
| 335 | + public void CreateRandomBatchSequential() |
| 336 | + { |
| 337 | + for ( int i = 0; i < BatchSize; i++ ) |
| 338 | + { |
| 339 | + using var stream = ByteStream.Create( randomInitialSizes[i] ); |
| 340 | + stream.Write( payloads[i] ); |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + [Benchmark( Description = "Hold 100 random streams simultaneously" )] |
| 345 | + public void CreateRandomBatchHeld() |
| 346 | + { |
| 347 | + CreateBatchRecursive( 0 ); |
| 348 | + } |
| 349 | + |
| 350 | + [Benchmark( Description = "Stress growth with large payload writes" )] |
| 351 | + public int LargePayloadWriteAndCopy() |
| 352 | + { |
| 353 | + using var stream = ByteStream.Create( 128 ); |
| 354 | + |
| 355 | + for ( int i = 0; i < 8; i++ ) |
| 356 | + { |
| 357 | + stream.Write( largePayload ); |
| 358 | + } |
| 359 | + |
| 360 | + return stream.ToArray().Length; |
| 361 | + } |
| 362 | + |
| 363 | + [Benchmark( Description = "Rapid reuse of pooled buffers" )] |
| 364 | + public void RapidReuseMixedSizes() |
| 365 | + { |
| 366 | + for ( int i = 0; i < BatchSize; i++ ) |
| 367 | + { |
| 368 | + using var stream = ByteStream.Create( 256 ); |
| 369 | + stream.Write( mediumPayload ); |
| 370 | + stream.Write( payloads[i] ); |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + void CreateBatchRecursive( int index ) |
| 375 | + { |
| 376 | + if ( index >= BatchSize ) |
| 377 | + { |
| 378 | + return; |
| 379 | + } |
| 380 | + |
| 381 | + using var stream = ByteStream.Create( randomInitialSizes[index] ); |
| 382 | + stream.Write( payloads[index] ); |
| 383 | + |
| 384 | + CreateBatchRecursive( index + 1 ); |
| 385 | + } |
| 386 | +} |
0 commit comments