|
1 | 1 | // Copyright (c) Kevin Zehrer |
2 | 2 | // Licensed under the MIT License. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | | -namespace MemoryStreamBenchmarks |
| 4 | +namespace MemoryStreamBenchmarks; |
| 5 | + |
| 6 | +//################################################################################ |
| 7 | +/// <summary> |
| 8 | +/// Helper class for filling a stream with random data and reading it back. |
| 9 | +/// </summary> |
| 10 | +public class StreamUtility |
5 | 11 | { |
6 | | - //################################################################################ |
| 12 | + //-------------------------------------------------------------------------------- |
| 13 | + /// <summary> |
| 14 | + /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
| 15 | + /// </summary> |
| 16 | + /// <param name="stream"> |
| 17 | + /// The stream instance being used. |
| 18 | + /// </param> |
| 19 | + /// <param name="destinationStream"> |
| 20 | + /// The asynchronous stream to copy the data to |
| 21 | + /// </param> |
| 22 | + /// <param name="fillData"> |
| 23 | + /// The test data used to fill the stream |
| 24 | + /// </param> |
| 25 | + /// <param name="dataLength"> |
| 26 | + /// The length of the data to fill and read back |
| 27 | + /// </param> |
| 28 | + public async Task BulkFillCopyToAsync (Stream stream, Stream destinationStream, byte[] fillData, |
| 29 | + int dataLength) |
| 30 | + { |
| 31 | + stream.Position = 0; |
| 32 | + destinationStream.Position = 0; |
| 33 | + // Write synchronously to the source stream to fill it as rapidly as possible |
| 34 | + // (this is not what we are benchmarking) |
| 35 | + // ReSharper disable once MethodHasAsyncOverload |
| 36 | + stream.Write(fillData, 0, dataLength); |
| 37 | + |
| 38 | + // Reset the position to the start of the stream for copying |
| 39 | + stream.Position = 0; |
| 40 | + // Copy asynchronously to the destination stream |
| 41 | + await stream.CopyToAsync(destinationStream); |
| 42 | + } |
| 43 | + //-------------------------------------------------------------------------------- |
7 | 44 | /// <summary> |
8 | | - /// Helper class for filling a stream with random data and reading it back. |
| 45 | + /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
9 | 46 | /// </summary> |
10 | | - public class StreamUtility |
| 47 | + /// <param name="stream"> |
| 48 | + /// The stream instance being used. |
| 49 | + /// </param> |
| 50 | + /// <param name="destinationStream"> |
| 51 | + /// The asynchronous stream to copy the data to |
| 52 | + /// </param> |
| 53 | + /// <param name="fillData"> |
| 54 | + /// The test data used to fill the stream |
| 55 | + /// </param> |
| 56 | + /// <param name="dataLength"> |
| 57 | + /// The length of the data to fill and read back |
| 58 | + /// </param> |
| 59 | + /// <param name="segmentLength"> |
| 60 | + /// The length of the segment to fill with each call and to read back with each call |
| 61 | + /// </param> |
| 62 | + public async Task SegmentFillCopyToAsync (Stream stream, Stream destinationStream, byte[] fillData, |
| 63 | + int dataLength, int segmentLength) |
11 | 64 | { |
12 | | - //-------------------------------------------------------------------------------- |
13 | | - /// <summary> |
14 | | - /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
15 | | - /// </summary> |
16 | | - /// <param name="stream"> |
17 | | - /// The stream instance being used. |
18 | | - /// </param> |
19 | | - /// <param name="destinationStream"> |
20 | | - /// The asynchronous stream to copy the data to |
21 | | - /// </param> |
22 | | - /// <param name="fillData"> |
23 | | - /// The test data used to fill the stream |
24 | | - /// </param> |
25 | | - /// <param name="dataLength"> |
26 | | - /// The length of the data to fill and read back |
27 | | - /// </param> |
28 | | - public async Task BulkFillCopyToAsync (Stream stream, Stream destinationStream, byte[] fillData, |
29 | | - int dataLength) |
| 65 | + stream.Position = 0; |
| 66 | + destinationStream.Position = 0; |
| 67 | + |
| 68 | + int writeSizeLeft = dataLength; |
| 69 | + while (writeSizeLeft > 0) |
30 | 70 | { |
31 | | - stream.Position = 0; |
32 | | - destinationStream.Position = 0; |
| 71 | + int writeSize = Math.Min(writeSizeLeft, segmentLength); |
33 | 72 | // Write synchronously to the source stream to fill it as rapidly as possible |
34 | 73 | // (this is not what we are benchmarking) |
35 | 74 | // ReSharper disable once MethodHasAsyncOverload |
36 | | - stream.Write(fillData, 0, dataLength); |
37 | | - |
38 | | - // Reset the position to the start of the stream for copying |
39 | | - stream.Position = 0; |
40 | | - // Copy asynchronously to the destination stream |
41 | | - await stream.CopyToAsync(destinationStream); |
| 75 | + stream.Write(fillData, 0, writeSize); |
| 76 | + writeSizeLeft -= writeSize; |
42 | 77 | } |
43 | | - //-------------------------------------------------------------------------------- |
44 | | - /// <summary> |
45 | | - /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
46 | | - /// </summary> |
47 | | - /// <param name="stream"> |
48 | | - /// The stream instance being used. |
49 | | - /// </param> |
50 | | - /// <param name="destinationStream"> |
51 | | - /// The asynchronous stream to copy the data to |
52 | | - /// </param> |
53 | | - /// <param name="fillData"> |
54 | | - /// The test data used to fill the stream |
55 | | - /// </param> |
56 | | - /// <param name="dataLength"> |
57 | | - /// The length of the data to fill and read back |
58 | | - /// </param> |
59 | | - /// <param name="segmentLength"> |
60 | | - /// The length of the segment to fill with each call and to read back with each call |
61 | | - /// </param> |
62 | | - public async Task SegmentFillCopyToAsync (Stream stream, Stream destinationStream, byte[] fillData, |
63 | | - int dataLength, int segmentLength) |
64 | | - { |
65 | | - stream.Position = 0; |
66 | | - destinationStream.Position = 0; |
67 | 78 |
|
68 | | - int writeSizeLeft = dataLength; |
69 | | - while (writeSizeLeft > 0) |
70 | | - { |
71 | | - int writeSize = Math.Min(writeSizeLeft, segmentLength); |
72 | | - // Write synchronously to the source stream to fill it as rapidly as possible |
73 | | - // (this is not what we are benchmarking) |
74 | | - // ReSharper disable once MethodHasAsyncOverload |
75 | | - stream.Write(fillData, 0, writeSize); |
76 | | - writeSizeLeft -= writeSize; |
77 | | - } |
78 | | - |
79 | | - // Reset the position to the start of the stream for copying |
80 | | - stream.Position = 0; |
81 | | - // Copy asynchronously to the destination stream |
82 | | - await stream.CopyToAsync(destinationStream); |
83 | | - } |
84 | | - //-------------------------------------------------------------------------------- |
85 | | - /// <summary> |
86 | | - /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
87 | | - /// </summary> |
88 | | - /// <param name="stream"> |
89 | | - /// The stream instance being used. |
90 | | - /// </param> |
91 | | - /// <param name="fillData"> |
92 | | - /// The test data used to fill the stream |
93 | | - /// </param> |
94 | | - /// <param name="readBuffer"> |
95 | | - /// The buffer to read back the data to |
96 | | - /// </param> |
97 | | - /// <param name="dataLength"> |
98 | | - /// The length of the data to fill and read back |
99 | | - /// </param> |
100 | | - public void BulkFillAndRead (Stream stream, byte[] fillData, byte[] readBuffer, int dataLength) |
| 79 | + // Reset the position to the start of the stream for copying |
| 80 | + stream.Position = 0; |
| 81 | + // Copy asynchronously to the destination stream |
| 82 | + await stream.CopyToAsync(destinationStream); |
| 83 | + } |
| 84 | + //-------------------------------------------------------------------------------- |
| 85 | + /// <summary> |
| 86 | + /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
| 87 | + /// </summary> |
| 88 | + /// <param name="stream"> |
| 89 | + /// The stream instance being used. |
| 90 | + /// </param> |
| 91 | + /// <param name="fillData"> |
| 92 | + /// The test data used to fill the stream |
| 93 | + /// </param> |
| 94 | + /// <param name="readBuffer"> |
| 95 | + /// The buffer to read back the data to |
| 96 | + /// </param> |
| 97 | + /// <param name="dataLength"> |
| 98 | + /// The length of the data to fill and read back |
| 99 | + /// </param> |
| 100 | + public void BulkFillAndRead (Stream stream, byte[] fillData, byte[] readBuffer, int dataLength) |
| 101 | + { |
| 102 | + stream.Position = 0; |
| 103 | + stream.Write(fillData, 0, dataLength); |
| 104 | + // Reset the position to the start of the stream for reading |
| 105 | + stream.Position = 0; |
| 106 | + if (stream.Read(readBuffer, 0, dataLength) < dataLength) |
| 107 | + throw new Exception("Failed to read all data back"); |
| 108 | + } |
| 109 | + //-------------------------------------------------------------------------------- |
| 110 | + /// <summary> |
| 111 | + /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
| 112 | + /// </summary> |
| 113 | + /// <param name="stream"> |
| 114 | + /// The stream instance being used. |
| 115 | + /// </param> |
| 116 | + /// <param name="fillData"> |
| 117 | + /// The test data used to fill the stream |
| 118 | + /// </param> |
| 119 | + /// <param name="readBuffer"> |
| 120 | + /// The buffer to read back the data to |
| 121 | + /// </param> |
| 122 | + /// <param name="dataLength"> |
| 123 | + /// The length of the data to fill and read back |
| 124 | + /// </param> |
| 125 | + /// <param name="segmentLength"> |
| 126 | + /// The length of the segment to fill with each call and to read back with each call |
| 127 | + /// </param> |
| 128 | + public void SegmentFillAndRead (Stream stream, byte[] fillData, byte[] readBuffer, |
| 129 | + long dataLength, int segmentLength) |
| 130 | + { |
| 131 | + stream.Position = 0; |
| 132 | + long writeSizeLeft = dataLength; |
| 133 | + while (writeSizeLeft > 0) |
101 | 134 | { |
102 | | - stream.Position = 0; |
103 | | - stream.Write(fillData, 0, dataLength); |
104 | | - // Reset the position to the start of the stream for reading |
105 | | - stream.Position = 0; |
106 | | - if (stream.Read(readBuffer, 0, dataLength) < dataLength) |
107 | | - throw new Exception("Failed to read all data back"); |
| 135 | + int writeSize = (int)Math.Min(writeSizeLeft, segmentLength); |
| 136 | + stream.Write(fillData, 0, writeSize); |
| 137 | + writeSizeLeft -= writeSize; |
108 | 138 | } |
109 | | - //-------------------------------------------------------------------------------- |
110 | | - /// <summary> |
111 | | - /// Test to fill the stream with the passed fill data and then read it back into the read buffer |
112 | | - /// </summary> |
113 | | - /// <param name="stream"> |
114 | | - /// The stream instance being used. |
115 | | - /// </param> |
116 | | - /// <param name="fillData"> |
117 | | - /// The test data used to fill the stream |
118 | | - /// </param> |
119 | | - /// <param name="readBuffer"> |
120 | | - /// The buffer to read back the data to |
121 | | - /// </param> |
122 | | - /// <param name="dataLength"> |
123 | | - /// The length of the data to fill and read back |
124 | | - /// </param> |
125 | | - /// <param name="segmentLength"> |
126 | | - /// The length of the segment to fill with each call and to read back with each call |
127 | | - /// </param> |
128 | | - public void SegmentFillAndRead (Stream stream, byte[] fillData, byte[] readBuffer, |
129 | | - long dataLength, int segmentLength) |
130 | | - { |
131 | | - stream.Position = 0; |
132 | | - long writeSizeLeft = dataLength; |
133 | | - while (writeSizeLeft > 0) |
134 | | - { |
135 | | - int writeSize = (int)Math.Min(writeSizeLeft, segmentLength); |
136 | | - stream.Write(fillData, 0, writeSize); |
137 | | - writeSizeLeft -= writeSize; |
138 | | - } |
139 | 139 |
|
140 | | - // Reset the position to the start of the stream for reading |
141 | | - stream.Position = 0; |
142 | | - long readSizeLeft = dataLength; |
143 | | - while (readSizeLeft > 0) |
144 | | - { |
145 | | - int readSize = (int)Math.Min(readSizeLeft, segmentLength); |
146 | | - int readInSize = stream.Read(readBuffer, 0, readSize); |
147 | | - if (readInSize < readSize) |
148 | | - throw new Exception("Failed to read all data back"); |
149 | | - readSizeLeft -= readInSize; |
150 | | - } |
| 140 | + // Reset the position to the start of the stream for reading |
| 141 | + stream.Position = 0; |
| 142 | + long readSizeLeft = dataLength; |
| 143 | + while (readSizeLeft > 0) |
| 144 | + { |
| 145 | + int readSize = (int)Math.Min(readSizeLeft, segmentLength); |
| 146 | + int readInSize = stream.Read(readBuffer, 0, readSize); |
| 147 | + if (readInSize < readSize) |
| 148 | + throw new Exception("Failed to read all data back"); |
| 149 | + readSizeLeft -= readInSize; |
151 | 150 | } |
152 | | - //-------------------------------------------------------------------------------- |
153 | 151 | } |
154 | | - //################################################################################ |
| 152 | + //-------------------------------------------------------------------------------- |
155 | 153 | } |
| 154 | +//################################################################################ |
0 commit comments