Repository navigation
Expand file tree
/
Copy pathConnectionBenchmarks.cs
More file actions
86 lines (78 loc) · 3.08 KB
/
Copy pathConnectionBenchmarks.cs
File metadata and controls
86 lines (78 loc) · 3.08 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
using System;
using ServiceWire.NamedPipes;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using ServiceWire.TcpIp;
using System.Net;
namespace ServiceWire.Benchmarks
{
//explicit, modest invocation counts: every operation opens a real TCP
//connection whose close lingers in TIME_WAIT for minutes, so an unbounded run
//exhausts the Windows ephemeral port pool and fails with address-in-use
[SimpleJob(RuntimeMoniker.Net80, baseline: true, warmupCount: 2, iterationCount: 15, invocationCount: 16)]
[SimpleJob(RuntimeMoniker.Net10_0, warmupCount: 2, iterationCount: 15, invocationCount: 16)]
[SimpleJob(RuntimeMoniker.Net48, warmupCount: 2, iterationCount: 15, invocationCount: 16)]
[MemoryDiagnoser]
[HtmlExporter]
public class ConnectionBenchmarks
{
private INetTester _tester;
private Random _rnd;
//distinct pipe name: sharing it with the steady-state benchmark classes
//lets a stray host instance in another process serve these clients
private readonly string PipeName = "ServiceWireBenchmarkConn";
//a genuinely free port per operation: this benchmark opens and closes a host
//every iteration, and connections closed by the previous iteration's server
//linger in TIME_WAIT for minutes, which blocks rebinding their port on
//Windows. The OS-assigned probe port never collides; its ~10 microsecond
//cost is part of every measured connection setup equally.
private static IPEndPoint GetFreeEndPoint()
{
var probe = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
probe.Start();
var endPoint = (IPEndPoint)probe.LocalEndpoint;
probe.Stop();
return endPoint;
}
private NpEndPoint CreateNpEndPoint(string offset)
{
return new NpEndPoint(PipeName + offset);
}
public ConnectionBenchmarks()
{
_rnd = new Random();
_tester = new NetTester();
}
[Benchmark]
public void TcpConn()
{
var endPoint = GetFreeEndPoint();
using (var tcpHost = new TcpHost(endPoint))
{
tcpHost.AddService<INetTester>(_tester);
tcpHost.Open();
using (var tcpClient = new TcpClient<INetTester>(endPoint))
{
var a = _rnd.Next(0, 100);
var b = _rnd.Next(0, 100);
var result = tcpClient.Proxy.Min(a, b);
}
}
}
[Benchmark]
public void NpConn()
{
using (var npHost = new NpHost(PipeName))
{
npHost.AddService<INetTester>(_tester);
npHost.Open();
using (var npClient = new NpClient<INetTester>(CreateNpEndPoint(string.Empty)))
{
var a = _rnd.Next(0, 100);
var b = _rnd.Next(0, 100);
var result = npClient.Proxy.Min(a, b);
}
}
}
}
}