Repository navigation
Expand file tree
/
Copy pathDnsClientBenchmarks.Bytes.cs
More file actions
144 lines (127 loc) · 4.23 KB
/
Copy pathDnsClientBenchmarks.Bytes.cs
File metadata and controls
144 lines (127 loc) · 4.23 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Buffers;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using DnsClient;
namespace Benchmarks
{
public static partial class DnsClientBenchmarks
{
public class IntToBytes
{
private const int Ops = 100000;
private readonly byte[] forMemoryBuffer;
public IntToBytes()
{
// could use array pool, but that has overhead, unfair to compair as we would
// not get a new array everytime to write an integer...
forMemoryBuffer = new byte[4];
}
[Benchmark(Baseline = true)]
public void BitConverterGetBytes()
{
for (int i = 0; i < Ops; i++)
{
var bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(i));
}
}
[Benchmark()]
public void MemoryMarshalTryWrite()
{
for (int i = 0; i < Ops; i++)
{
var netInt = IPAddress.HostToNetworkOrder(i);
MemoryMarshal.TryWrite(forMemoryBuffer, ref netInt);
}
}
[Benchmark()]
public void MemoryMarshalAsBytes()
{
var intArray = new int[1];
for (int i = 0; i < Ops; i++)
{
intArray[0] = IPAddress.HostToNetworkOrder(i);
var result = MemoryMarshal.AsBytes(new ReadOnlySpan<int>(intArray));
}
}
}
public class SetBytesComparision
{
public static byte[] sourceSmall = new byte[] { 192, 168, 178, 23 };
public static byte[] sourceBig = Enumerable.Repeat((byte)150, 1000).ToArray();
public SetBytesComparision()
{
}
[Benchmark]
public ArraySegment<byte> ForLoop()
{
using (var writer = new DnsDatagramWriter())
{
foreach (var b in sourceSmall)
{
writer.WriteByte(b);
}
return writer.Data;
}
}
[Benchmark(Baseline = true)]
public ArraySegment<byte> BlockCopy()
{
using (var writer = new DnsDatagramWriter())
{
writer.WriteBytes(sourceSmall, sourceSmall.Length);
return writer.Data;
}
}
[Benchmark]
public ArraySegment<byte> ForLoopLarge()
{
using (var writer = new DnsDatagramWriter())
{
foreach (var b in sourceBig)
{
writer.WriteByte(b);
}
return writer.Data;
}
}
[Benchmark]
public ArraySegment<byte> BlockCopyLarge()
{
using (var writer = new DnsDatagramWriter())
{
writer.WriteBytes(sourceBig, sourceBig.Length);
return writer.Data;
}
}
}
public class AllocateVsPooledWriteHostName
{
public const string domainName = "www.hello.world.fcking.funny.com";
public AllocateVsPooledWriteHostName()
{
}
[Benchmark(Baseline = true)]
public ArraySegment<byte> Allocate()
{
// allocate array manually (not pooled)
using (var writer = new DnsDatagramWriter(new ArraySegment<byte>(new byte[DnsDatagramWriter.BufferSize])))
{
writer.WriteHostName(domainName);
return writer.Data;
}
}
[Benchmark]
public ArraySegment<byte> Pooled()
{
using (var writer = new DnsDatagramWriter())
{
writer.WriteHostName(domainName);
return writer.Data;
}
}
}
}
}