forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceCounterTests.cs
More file actions
149 lines (138 loc) · 6.58 KB
/
PerformanceCounterTests.cs
File metadata and controls
149 lines (138 loc) · 6.58 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
145
146
147
148
149
#if NET461
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
namespace Npgsql.Tests
{
[NonParallelizable]
[Explicit]
public class PerformanceCounterTests : TestBase
{
[Test]
public void HardConnectsAndDisconnectsPerSecond()
{
var hardConnectCounter = Counters.HardConnectsPerSecond.DiagnosticsCounter;
var hardDisconnectCounter = Counters.HardDisconnectsPerSecond.DiagnosticsCounter;
var softConnectCounter = Counters.SoftConnectsPerSecond.DiagnosticsCounter;
var softDisconnectCounter = Counters.SoftDisconnectsPerSecond.DiagnosticsCounter;
var nonPooledConnString = new NpgsqlConnectionStringBuilder(ConnectionString)
{
Pooling = false
}.ToString();
using (var pooledConn = new NpgsqlConnection(ConnectionString))
using (var nonPooledConn = new NpgsqlConnection(nonPooledConnString))
{
Thread.Sleep(2000); // Let the counter reset
Assert.That(hardConnectCounter!.RawValue, Is.Zero);
Assert.That(hardDisconnectCounter!.RawValue, Is.Zero);
Assert.That(softConnectCounter!.RawValue, Is.Zero);
Assert.That(softDisconnectCounter!.RawValue, Is.Zero);
pooledConn.Open(); // Pool is empty so this is a hard connect
nonPooledConn.Open();
Assert.That(hardConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(hardDisconnectCounter.RawValue, Is.Zero);
Assert.That(softConnectCounter.RawValue, Is.EqualTo(1));
Assert.That(softDisconnectCounter.RawValue, Is.Zero);
pooledConn.Close();
nonPooledConn.Close();
// The non-pooled was a hard disconnect, the pooled was a soft
Assert.That(hardConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(hardDisconnectCounter.RawValue, Is.EqualTo(1));
Assert.That(softConnectCounter.RawValue, Is.EqualTo(1));
Assert.That(softDisconnectCounter.RawValue, Is.EqualTo(1));
pooledConn.Open();
Assert.That(hardConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(hardDisconnectCounter.RawValue, Is.EqualTo(1));
Assert.That(softConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(softDisconnectCounter.RawValue, Is.EqualTo(1));
pooledConn.Close();
Assert.That(hardConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(hardDisconnectCounter.RawValue, Is.EqualTo(1));
Assert.That(softConnectCounter.RawValue, Is.EqualTo(2));
Assert.That(softDisconnectCounter.RawValue, Is.EqualTo(2));
}
}
[Test]
public void NumberOfNonPooledConnections()
{
var counter = Counters.NumberOfNonPooledConnections.DiagnosticsCounter;
var nonPooledConnString = new NpgsqlConnectionStringBuilder(ConnectionString)
{
Pooling = false
}.ToString();
using (var pooledConn = new NpgsqlConnection(ConnectionString))
using (var nonPooledConn = new NpgsqlConnection(nonPooledConnString))
{
Assert.That(counter!.RawValue, Is.Zero);
nonPooledConn.Open();
Assert.That(counter.RawValue, Is.EqualTo(1));
pooledConn.Open();
Assert.That(counter.RawValue, Is.EqualTo(1));
nonPooledConn.Close();
Assert.That(counter.RawValue, Is.Zero);
pooledConn.Close();
Assert.That(counter.RawValue, Is.Zero);
}
}
[Test]
public void PooledConnections()
{
var totalCounter = Counters.NumberOfPooledConnections.DiagnosticsCounter;
var activeCounter = Counters.NumberOfActiveConnections.DiagnosticsCounter;
var freeCounter = Counters.NumberOfFreeConnections.DiagnosticsCounter;
var nonPooledConnString = new NpgsqlConnectionStringBuilder(ConnectionString)
{
Pooling = false
}.ToString();
using (var pooledConn = new NpgsqlConnection(ConnectionString))
using (var nonPooledConn = new NpgsqlConnection(nonPooledConnString))
{
Assert.That(totalCounter!.RawValue, Is.Zero);
Assert.That(activeCounter!.RawValue, Is.Zero);
Assert.That(freeCounter!.RawValue, Is.Zero);
nonPooledConn.Open();
Assert.That(totalCounter.RawValue, Is.Zero);
Assert.That(activeCounter.RawValue, Is.Zero);
Assert.That(freeCounter.RawValue, Is.Zero);
pooledConn.Open();
Assert.That(totalCounter.RawValue, Is.EqualTo(1));
Assert.That(activeCounter.RawValue, Is.EqualTo(1));
Assert.That(freeCounter.RawValue, Is.Zero);
nonPooledConn.Close();
Assert.That(totalCounter.RawValue, Is.EqualTo(1));
Assert.That(activeCounter.RawValue, Is.EqualTo(1));
Assert.That(freeCounter.RawValue, Is.Zero);
pooledConn.Close();
Assert.That(totalCounter.RawValue, Is.EqualTo(1));
Assert.That(activeCounter.RawValue, Is.Zero);
Assert.That(freeCounter.RawValue, Is.EqualTo(1));
}
NpgsqlConnection.ClearAllPools();
Assert.That(totalCounter.RawValue, Is.Zero);
Assert.That(activeCounter.RawValue, Is.Zero);
Assert.That(freeCounter.RawValue, Is.Zero);
}
[OneTimeSetUp]
public void OneTimeSetup()
{
var countersExist = false;
try
{
countersExist = PerformanceCounterCategory.Exists(Counter.DiagnosticsCounterCategory);
}
catch (UnauthorizedAccessException) {}
Assert.That(countersExist, "The Npgsql performance counters haven't been created");
var perfCtrSwitch = new TraceSwitch("ConnectionPoolPerformanceCounterDetail", "level of detail to track with connection pool performance counters");
var verboseCounters = perfCtrSwitch.Level == TraceLevel.Verbose;
if (!verboseCounters)
Assert.Fail("Verbose performance counters not enabled");
}
[SetUp]
public void Setup()
{
NpgsqlConnection.ClearAllPools();
}
}
}
#endif