From ef883399d202598067fec9913f01080534d0dc08 Mon Sep 17 00:00:00 2001 From: Alan Hayward Date: Mon, 13 Jun 2022 09:17:46 +0100 Subject: [PATCH 1/5] Base64Tests: Use valid input data The tests were using random generated chars. This would cause the Decode functions to exit early (usually after a single iteration). --- .../libraries/System.Buffers/Base64Tests.cs | 10 +++---- .../ValuesGenerator.cs | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs index 1b4fdb89b5e..d409b04c8f4 100644 --- a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs +++ b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs @@ -52,22 +52,22 @@ public void SetupConvertToBase64CharArray() [GlobalSetup(Target = nameof(Base64Decode))] public void SetupBase64Decode() { - _encodedBytes = ValuesGenerator.Array(NumberOfBytes); + _encodedBytes = ValuesGenerator.ArrayBase64EncodingBytes(NumberOfBytes); _decodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(NumberOfBytes)]; } [Benchmark] public OperationStatus Base64Decode() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); - [GlobalSetup(Target = nameof(Base64DecodeDetinationTooSmall))] - public void SetupBase64DecodeDetinationTooSmall() + [GlobalSetup(Target = nameof(Base64DecodeDestinationTooSmall))] + public void SetupBase64DecodeDestinationTooSmall() { - _encodedBytes = ValuesGenerator.Array(NumberOfBytes); + _encodedBytes = ValuesGenerator.ArrayBase64EncodingBytes(NumberOfBytes); _decodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(NumberOfBytes) - 1]; } [Benchmark] - public OperationStatus Base64DecodeDetinationTooSmall() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); + public OperationStatus Base64DecodeDestinationTooSmall() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); #if !NETFRAMEWORK // API added in .NET Core 2.1 [GlobalSetup(Target = nameof(ConvertTryFromBase64Chars))] diff --git a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs index f7f988adba3..f1b1b135dd7 100644 --- a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs +++ b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs @@ -71,6 +71,32 @@ public static T[] Array(int count) return result; } + public static readonly byte[] s_encodingMap = { + 65, 66, 67, 68, 69, 70, 71, 72, //A..H + 73, 74, 75, 76, 77, 78, 79, 80, //I..P + 81, 82, 83, 84, 85, 86, 87, 88, //Q..X + 89, 90, 97, 98, 99, 100, 101, 102, //Y..Z, a..f + 103, 104, 105, 106, 107, 108, 109, 110, //g..n + 111, 112, 113, 114, 115, 116, 117, 118, //o..v + 119, 120, 121, 122, 48, 49, 50, 51, //w..z, 0..3 + 52, 53, 54, 55, 56, 57, 43, 47 //4..9, +, / + }; + + public static byte[] ArrayBase64EncodingBytes(int count) + { + var result = new byte[count]; + + var random = new Random(Seed); + + for (int i = 0; i < result.Length; i++) + { + int index = (byte)random.Next(0, s_encodingMap.Length - 1); // Do not pick '=' + result[i] = s_encodingMap[index]; + } + + return result; + } + public static Dictionary Dictionary(int count) { var dictionary = new Dictionary(); From f87cc3e69b4c8ae43fb626e014e226c8ea6d02b6 Mon Sep 17 00:00:00 2001 From: Alan Hayward Date: Wed, 15 Jun 2022 17:58:00 +0100 Subject: [PATCH 2/5] Add Base64DecodeInvalidData --- .../micro/libraries/System.Buffers/Base64Tests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs index d409b04c8f4..7039b336878 100644 --- a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs +++ b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs @@ -69,6 +69,16 @@ public void SetupBase64DecodeDestinationTooSmall() [Benchmark] public OperationStatus Base64DecodeDestinationTooSmall() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); + [GlobalSetup(Target = nameof(Base64DecodeInvalidData))] + public void SetupBase64DecodeInvalidData() + { + _encodedBytes = ValuesGenerator.Array(NumberOfBytes); + _decodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(NumberOfBytes)]; + } + + [Benchmark] + public OperationStatus Base64DecodeInvalidData() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); + #if !NETFRAMEWORK // API added in .NET Core 2.1 [GlobalSetup(Target = nameof(ConvertTryFromBase64Chars))] public void SetupConvertTryFromBase64Chars() From 5aab3df1abbb11a4f49d5377e61678ccdaf35dab Mon Sep 17 00:00:00 2001 From: Alan Hayward Date: Thu, 16 Jun 2022 15:55:47 +0100 Subject: [PATCH 3/5] Revert "Add Base64DecodeInvalidData" This reverts commit f87cc3e69b4c8ae43fb626e014e226c8ea6d02b6. --- .../micro/libraries/System.Buffers/Base64Tests.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs index 7039b336878..d409b04c8f4 100644 --- a/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs +++ b/src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs @@ -69,16 +69,6 @@ public void SetupBase64DecodeDestinationTooSmall() [Benchmark] public OperationStatus Base64DecodeDestinationTooSmall() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); - [GlobalSetup(Target = nameof(Base64DecodeInvalidData))] - public void SetupBase64DecodeInvalidData() - { - _encodedBytes = ValuesGenerator.Array(NumberOfBytes); - _decodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(NumberOfBytes)]; - } - - [Benchmark] - public OperationStatus Base64DecodeInvalidData() => Base64.DecodeFromUtf8(_encodedBytes, _decodedBytes, out _, out _); - #if !NETFRAMEWORK // API added in .NET Core 2.1 [GlobalSetup(Target = nameof(ConvertTryFromBase64Chars))] public void SetupConvertTryFromBase64Chars() From df893bcbc5a9c9000899455dcb5a58acc6e5ce07 Mon Sep 17 00:00:00 2001 From: Alan Hayward Date: Thu, 16 Jun 2022 15:56:29 +0100 Subject: [PATCH 4/5] Use all of s_encodingMap --- src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs index f1b1b135dd7..0209d5557c4 100644 --- a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs +++ b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs @@ -90,7 +90,7 @@ public static byte[] ArrayBase64EncodingBytes(int count) for (int i = 0; i < result.Length; i++) { - int index = (byte)random.Next(0, s_encodingMap.Length - 1); // Do not pick '=' + int index = (byte)random.Next(0, s_encodingMap.Length); result[i] = s_encodingMap[index]; } From f5d32c3564568479bdbc479a89a3567f111a2505 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sun, 19 Jun 2022 17:53:22 -0600 Subject: [PATCH 5/5] Update src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs Unnecessary cast. --- src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs index 0209d5557c4..a99a7c66cc1 100644 --- a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs +++ b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs @@ -90,7 +90,7 @@ public static byte[] ArrayBase64EncodingBytes(int count) for (int i = 0; i < result.Length; i++) { - int index = (byte)random.Next(0, s_encodingMap.Length); + int index = random.Next(0, s_encodingMap.Length); result[i] = s_encodingMap[index]; }