1- #if RELEASE
1+ #if RELEASE
22namespace Roydl . Crypto . Test . BenchmarkTests
33{
44 using System ;
@@ -17,26 +17,24 @@ namespace Roydl.Crypto.Test.BenchmarkTests
1717 public class ChecksumPerformanceTests
1818 {
1919 private const int BenchmarkRepeats = 20 ;
20+ private const int DefaultDataSize = 65536 ;
2021
2122 private static readonly TestCaseData [ ] BenchmarkTestData =
2223 [
23- new ( ChecksumAlgo . Adler32 , 65536 ) ,
24- new ( ChecksumAlgo . Crc16 , 65536 ) ,
25- new ( ChecksumAlgo . Crc32 , 65536 ) ,
26- new ( ChecksumAlgo . Crc32Xz , 65536 ) ,
27- new ( ChecksumAlgo . Crc32Posix , 65536 ) ,
28- new ( ChecksumAlgo . Crc64 , 65536 ) ,
29- new ( ChecksumAlgo . Crc64Xz , 65536 ) ,
30- new ( ChecksumAlgo . Crc82 , 65536 ) ,
31- new ( ChecksumAlgo . Sha2 , 65536 ) ,
32- new ( ChecksumAlgo . Sha2Bit384 , 65536 ) ,
33- new ( ChecksumAlgo . Sha2Bit512 , 65536 ) ,
34- new ( ChecksumAlgo . Sha3 , 65536 ) ,
35- new ( ChecksumAlgo . Sha3Bit384 , 65536 ) ,
36- new ( ChecksumAlgo . Sha3Bit512 , 65536 )
24+ new ( ChecksumAlgo . Adler32 , DefaultDataSize ) ,
25+ new ( ChecksumAlgo . Crc16 , DefaultDataSize ) ,
26+ new ( ChecksumAlgo . Crc32 , DefaultDataSize ) ,
27+ new ( ChecksumAlgo . Crc32Xz , DefaultDataSize ) ,
28+ new ( ChecksumAlgo . Crc32Posix , DefaultDataSize ) ,
29+ new ( ChecksumAlgo . Crc64 , DefaultDataSize ) ,
30+ new ( ChecksumAlgo . Crc64Xz , DefaultDataSize ) ,
31+ new ( ChecksumAlgo . Crc82 , DefaultDataSize ) ,
32+ new ( ChecksumAlgo . Sha2 , DefaultDataSize ) ,
33+ new ( ChecksumAlgo . Sha3 , DefaultDataSize ) ,
3734 ] ;
3835
39- private static readonly ConcurrentDictionary < string , ConcurrentBag < double > > BenchmarkResults = new ( Environment . ProcessorCount , BenchmarkTestData . Length ) ;
36+ private static readonly ConcurrentDictionary < string , ConcurrentBag < double > > BenchmarkResults =
37+ new ( Environment . ProcessorCount , BenchmarkTestData . Length ) ;
4038
4139 [ OneTimeTearDown ]
4240 [ SetCulture ( "en-US" ) ]
@@ -52,16 +50,16 @@ public void CreateResultFiles()
5250 var sorted = value . OrderByDescending ( x => x ) . ToArray ( ) ;
5351 var digits = BenchmarkRepeats . ToString ( NumberFormatInfo . InvariantInfo ) . Length ;
5452 var content =
55- $ "Average: { sorted . Sum ( ) / sorted . Length : 0.0 ,6} MiB /s" +
53+ $ "Average: { sorted . Sum ( ) / sorted . Length : 0.000 ,6} GiB /s" +
5654 Environment . NewLine +
57- $ " Best: { sorted [ 0 ] : 0.0 ,6} MiB /s" +
55+ $ " Best: { sorted [ 0 ] : 0.000 ,6} GiB /s" +
5856 Environment . NewLine +
59- $ " Worst: { sorted [ ^ 1 ] : 0.0 ,6} MiB /s" +
57+ $ " Worst: { sorted [ ^ 1 ] : 0.000 ,6} GiB /s" +
6058 Environment . NewLine +
6159 Environment . NewLine +
6260 $ "Results of { sorted . Length } runs with a total duration of { sorted . Length * 9 } seconds:" +
6361 Environment . NewLine +
64- string . Join ( Environment . NewLine , sorted . Select ( ( x , i ) => $ "{ ( i + 1 ) . ToString ( ) . PadLeft ( digits ) } : { x : 0.0 ,6} MiB /s") ) ;
62+ string . Join ( Environment . NewLine , sorted . Select ( ( x , i ) => $ "{ ( i + 1 ) . ToString ( ) . PadLeft ( digits ) } : { x : 0.000 ,6} GiB /s") ) ;
6563 File . WriteAllText ( file , content ) ;
6664 } ) ;
6765 }
@@ -84,19 +82,19 @@ private static void RunBenchmark(IChecksumAlgorithm algorithm, int packetSize, b
8482 total += data . Length ;
8583 }
8684 sw . Stop ( ) ;
87- rate = Math . Max ( total / sw . Elapsed . TotalSeconds / 1024 / 1024 , rate ) ;
85+ rate = Math . Max ( total / sw . Elapsed . TotalSeconds / 1024 / 1024 / 1024 , rate ) ;
8886 }
8987
9088 if ( ! saveResults )
9189 {
92- TestContext . Write ( @" {0} Benchmark - Throughput: '{1:0.0} MiB /s'; " , algorithm . AlgorithmName , rate ) ;
90+ TestContext . Write ( @"{0} Benchmark - Throughput: '{1:0.000} GiB /s'; " , algorithm . AlgorithmName , rate ) ;
9391 switch ( packetSize )
9492 {
9593 case > 1024 :
96- TestContext . Write ( @"Packet Size: '{0:0} KiB';" , packetSize / 1024 ) ;
94+ TestContext . WriteLine ( @"Packet Size: '{0:0} KiB';" , packetSize / 1024 ) ;
9795 break ;
9896 default :
99- TestContext . Write ( @"Packet Size: '{0:0} Bytes';" , packetSize ) ;
97+ TestContext . WriteLine ( @"Packet Size: '{0:0} Bytes';" , packetSize ) ;
10098 break ;
10199 }
102100 return ;
@@ -110,15 +108,15 @@ private static void RunBenchmark(IChecksumAlgorithm algorithm, int packetSize, b
110108
111109 [ Test ]
112110 [ TestCaseSource ( nameof ( BenchmarkTestData ) ) ]
113- public void BenchmarkOnce ( ChecksumAlgo algorithm , int dataSize ) =>
114- RunBenchmark ( algorithm . GetDefaultInstance ( ) , dataSize , false ) ;
111+ public void BenchmarkOnce ( ChecksumAlgo algorithm , int packetSize ) =>
112+ RunBenchmark ( algorithm . GetDefaultInstance ( ) , packetSize , false ) ;
115113
116114 [ Explicit ]
117115 [ Test ]
118116 [ TestCaseSource ( nameof ( BenchmarkTestData ) ) ]
119117 [ Repeat ( BenchmarkRepeats ) ]
120- public void BenchmarkRepeat ( ChecksumAlgo algorithm , int dataSize ) =>
121- RunBenchmark ( algorithm . GetDefaultInstance ( ) , dataSize , true ) ;
118+ public void BenchmarkRepeat ( ChecksumAlgo algorithm , int packetSize ) =>
119+ RunBenchmark ( algorithm . GetDefaultInstance ( ) , packetSize , true ) ;
122120 }
123121}
124122#endif
0 commit comments