-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathPerf.Base64.cs
53 lines (44 loc) · 1.67 KB
/
Perf.Base64.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Text.Json.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_Base64
{
private ArrayBufferWriter<byte> _arrayBufferWriter;
private byte[] _dataWithNoEscaping;
private byte[] _dataWithEscaping;
[Params(100, 1000)]
public int NumberOfBytes { get; set; }
[GlobalSetup]
public void Setup()
{
_arrayBufferWriter = new ArrayBufferWriter<byte>();
// Results in a number of A plus padding
_dataWithNoEscaping = new byte[NumberOfBytes];
// Results in a lot + and /
_dataWithEscaping = Enumerable.Range(0, NumberOfBytes)
.Select(i => i % 2 == 0 ? 0xFB : 0xFF)
.Select(i => (byte)i)
.ToArray();
}
[Benchmark]
public void WriteByteArrayAsBase64_NoEscaping() => WriteByteArrayAsBase64Core(_dataWithNoEscaping);
[Benchmark]
public void WriteByteArrayAsBase64_HeavyEscaping() => WriteByteArrayAsBase64Core(_dataWithEscaping);
private void WriteByteArrayAsBase64Core(byte[] data)
{
_arrayBufferWriter.Clear();
using (var json = new Utf8JsonWriter(_arrayBufferWriter))
{
json.WriteBase64StringValue(data);
json.Flush();
}
}
}
}