-
Notifications
You must be signed in to change notification settings - Fork 272
/
BinaryReaderTests.cs
101 lines (83 loc) · 3.15 KB
/
BinaryReaderTests.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Linq;
using System.Text;
namespace System.IO.Tests
{
[BenchmarkCategory(Categories.Libraries)]
public class BinaryReaderTests
{
private BinaryReader _dummyReader;
private MemoryStream _asciiChar, _nonAsciiChar;
private BinaryReader _asciiCharReader, _nonAsciiCharReader;
private MemoryStream _smallString, _largeString;
private BinaryReader _smallStringReader, _largeStringReader;
[GlobalSetup]
public void Setup()
{
_dummyReader = new BinaryReader(new DummyReadStream());
_asciiCharReader = new BinaryReader(_asciiChar = new MemoryStream(new byte[] { (byte)'a' }));
_nonAsciiCharReader = new BinaryReader(_nonAsciiChar = new MemoryStream(new byte[] { 0xC3, 0xA0 /* U+00E0 */ }), Encoding.UTF8);
_smallStringReader = new BinaryReader(_smallString = CreateMemoryStreamFromString("hello world"), Encoding.UTF8);
_largeStringReader = new BinaryReader(_largeString = CreateMemoryStreamFromString(new string('a', 512)), Encoding.UTF8);
static MemoryStream CreateMemoryStreamFromString(string value)
{
var stream = new MemoryStream();
var writer = new BinaryWriter(stream, Encoding.UTF8);
writer.Write(value);
return stream;
}
}
[Benchmark]
public BinaryReader DefaultCtor() => new BinaryReader(Stream.Null);
[Benchmark]
[MemoryRandomization]
public bool ReadBool() => _dummyReader.ReadBoolean();
[Benchmark]
[MemoryRandomization]
public char ReadAsciiChar()
{
_asciiChar.Position = 0;
return _asciiCharReader.ReadChar();
}
[Benchmark]
public char ReadNonAsciiChar()
{
_nonAsciiChar.Position = 0;
return _nonAsciiCharReader.ReadChar();
}
[Benchmark]
[MemoryRandomization]
public ushort ReadUInt16() => _dummyReader.ReadUInt16();
[Benchmark]
public uint ReadUInt32() => _dummyReader.ReadUInt32();
[Benchmark]
public ulong ReadUInt64() => _dummyReader.ReadUInt64();
#if NET5_0_OR_GREATER
[Benchmark]
[MemoryRandomization]
public Half ReadHalf() => _dummyReader.ReadHalf();
#endif
[Benchmark]
[MemoryRandomization]
public float ReadSingle() => _dummyReader.ReadSingle();
[Benchmark]
public double ReadDouble() => _dummyReader.ReadDouble();
[Benchmark]
[MemoryRandomization]
public string ReadSmallString()
{
_smallString.Position = 0;
return _smallStringReader.ReadString();
}
[Benchmark]
public string ReadLargeString()
{
_largeString.Position = 0;
return _largeStringReader.ReadString();
}
}
}