Skip to content

Commit

Permalink
Base64Tests: Use valid input data (#2479)
Browse files Browse the repository at this point in the history
  • Loading branch information
a74nh authored Jun 20, 2022
1 parent 3b807b2 commit 281bdef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/benchmarks/micro/libraries/System.Buffers/Base64Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ public void SetupConvertToBase64CharArray()
[GlobalSetup(Target = nameof(Base64Decode))]
public void SetupBase64Decode()
{
_encodedBytes = ValuesGenerator.Array<byte>(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<byte>(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))]
Expand Down
26 changes: 26 additions & 0 deletions src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ public static T[] Array<T>(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 = random.Next(0, s_encodingMap.Length);
result[i] = s_encodingMap[index];
}

return result;
}

public static Dictionary<TKey, TValue> Dictionary<TKey, TValue>(int count)
{
var dictionary = new Dictionary<TKey, TValue>();
Expand Down

0 comments on commit 281bdef

Please sign in to comment.