-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.Booleans.cs
58 lines (46 loc) · 1.6 KB
/
Perf.Booleans.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
54
55
56
57
58
// 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 BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Text.Json.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_Booleans
{
private const int DataSize = 100_000;
private ArrayBufferWriter<byte> _arrayBufferWriter;
private bool[] _boolArrayValues;
[Params(true, false)]
public bool Formatted;
[Params(true, false)]
public bool SkipValidation;
[GlobalSetup]
public void Setup()
{
_arrayBufferWriter = new ArrayBufferWriter<byte>();
var random = new Random(42);
_boolArrayValues = new bool[DataSize];
for (int i = 0; i < DataSize; i++)
{
_boolArrayValues[i] = random.NextDouble() > 0.5;
}
}
[Benchmark]
public void WriteBooleans()
{
_arrayBufferWriter.Clear();
using (var json = new Utf8JsonWriter(_arrayBufferWriter, new JsonWriterOptions { Indented = Formatted, SkipValidation = SkipValidation }))
{
json.WriteStartArray();
for (int i = 0; i < DataSize; i++)
{
json.WriteBooleanValue(_boolArrayValues[i]);
}
json.WriteEndArray();
json.Flush();
}
}
}
}