forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnixDomainSocket.cs
More file actions
43 lines (37 loc) · 1.57 KB
/
UnixDomainSocket.cs
File metadata and controls
43 lines (37 loc) · 1.57 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
using System;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace Npgsql.Benchmarks
{
public class UnixDomainSocket
{
readonly NpgsqlConnection _tcpipConn;
readonly NpgsqlCommand _tcpipCmd;
readonly NpgsqlConnection _unixConn;
readonly NpgsqlCommand _unixCmd;
public UnixDomainSocket()
{
_tcpipConn = BenchmarkEnvironment.OpenConnection();
_tcpipCmd = new NpgsqlCommand("SELECT @p", _tcpipConn);
_tcpipCmd.Parameters.AddWithValue("p", new string('x', 10000));
var port = new NpgsqlConnectionStringBuilder(BenchmarkEnvironment.ConnectionString).Port;
var candidateDirectories = new[] { "/var/run/postgresql", "/tmp" };
var dir = candidateDirectories.FirstOrDefault(d => File.Exists(Path.Combine(d, $".s.PGSQL.{port}")));
if (dir == null)
throw new Exception("No PostgreSQL unix domain socket was found");
var connString = new NpgsqlConnectionStringBuilder(BenchmarkEnvironment.ConnectionString)
{
Host = dir
}.ToString();
_unixConn = new NpgsqlConnection(connString);
_unixConn.Open();
_unixCmd = new NpgsqlCommand("SELECT @p", _unixConn);
_unixCmd.Parameters.AddWithValue("p", new string('x', 10000));
}
[Benchmark(Baseline = true)]
public string Tcpip() => (string)_tcpipCmd.ExecuteScalar();
[Benchmark]
public string UnixDomain() => (string)_unixCmd.ExecuteScalar();
}
}