Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 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,32 @@ 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 _);

[GlobalSetup(Target = nameof(Base64DecodeInvalidData))]
public void SetupBase64DecodeInvalidData()
{
_encodedBytes = ValuesGenerator.Array<byte>(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))]
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 = (byte)random.Next(0, s_encodingMap.Length - 1); // Do not pick '='
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