Skip to content

Commit c397fdd

Browse files
authored
Multiplexing counters return -1 whenever not used and "prepared commands ration" now returns value between 0 and 100 (#3963)
1 parent c601731 commit c397fdd

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

src/Npgsql/NpgsqlEventSource.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal void MultiplexingBatchSent(int numCommands, Stopwatch stopwatch)
9696
}
9797

9898
#if !NETSTANDARD2_0
99-
int GetIdleConnections()
99+
double GetIdleConnections()
100100
{
101101
// Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards
102102
// to different counters. So idle and busy and be unsynchronized, as they're not polled together.
@@ -111,7 +111,7 @@ int GetIdleConnections()
111111
}
112112
}
113113

114-
int GetBusyConnections()
114+
double GetBusyConnections()
115115
{
116116
// Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards
117117
// to different counters. So idle and busy and be unsynchronized, as they're not polled together.
@@ -126,14 +126,34 @@ int GetBusyConnections()
126126
}
127127
}
128128

129-
int GetPoolsCount()
129+
double GetPoolsCount()
130130
{
131131
lock (_poolsLock)
132132
{
133133
return _pools.Count;
134134
}
135135
}
136136

137+
double GetMultiplexingAverageCommandsPerBatch()
138+
{
139+
var batchesSent = Interlocked.Read(ref _multiplexingBatchesSent);
140+
if (batchesSent == 0)
141+
return -1;
142+
143+
var commandsSent = (double)Interlocked.Read(ref _multiplexingCommandsSent);
144+
return commandsSent / batchesSent;
145+
}
146+
147+
double GetMultiplexingAverageWriteTimePerBatch()
148+
{
149+
var batchesSent = Interlocked.Read(ref _multiplexingBatchesSent);
150+
if (batchesSent == 0)
151+
return -1;
152+
153+
var ticksWritten = (double)Interlocked.Read(ref _multiplexingTicksWritten);
154+
return ticksWritten / batchesSent / 1000;
155+
}
156+
137157
protected override void OnEventCommand(EventCommandEventArgs command)
138158
{
139159
if (command.Command == EventCommand.Enable)
@@ -180,33 +200,33 @@ protected override void OnEventCommand(EventCommandEventArgs command)
180200
_preparedCommandsRatioCounter = new PollingCounter(
181201
"prepared-commands-ratio",
182202
this,
183-
() => (double)Interlocked.Read(ref _totalPreparedCommands) / Interlocked.Read(ref _totalCommands))
203+
() => (double)Interlocked.Read(ref _totalPreparedCommands) / Interlocked.Read(ref _totalCommands) * 100)
184204
{
185205
DisplayName = "Prepared Commands Ratio",
186206
DisplayUnits = "%"
187207
};
188208

189-
_poolsCounter = new PollingCounter("connection-pools", this, () => GetPoolsCount())
209+
_poolsCounter = new PollingCounter("connection-pools", this, GetPoolsCount)
190210
{
191211
DisplayName = "Connection Pools"
192212
};
193213

194-
_idleConnectionsCounter = new PollingCounter("idle-connections", this, () => GetIdleConnections())
214+
_idleConnectionsCounter = new PollingCounter("idle-connections", this, GetIdleConnections)
195215
{
196216
DisplayName = "Idle Connections"
197217
};
198218

199-
_busyConnectionsCounter = new PollingCounter("busy-connections", this, () => GetBusyConnections())
219+
_busyConnectionsCounter = new PollingCounter("busy-connections", this, GetBusyConnections)
200220
{
201221
DisplayName = "Busy Connections"
202222
};
203223

204-
_multiplexingAverageCommandsPerBatchCounter = new PollingCounter("multiplexing-average-commands-per-batch", this, () => (double)Interlocked.Read(ref _multiplexingCommandsSent) / Interlocked.Read(ref _multiplexingBatchesSent))
224+
_multiplexingAverageCommandsPerBatchCounter = new PollingCounter("multiplexing-average-commands-per-batch", this, GetMultiplexingAverageCommandsPerBatch)
205225
{
206226
DisplayName = "Average commands per multiplexing batch"
207227
};
208228

209-
_multiplexingAverageWriteTimePerBatchCounter = new PollingCounter("multiplexing-average-write-time-per-batch", this, () => (double)Interlocked.Read(ref _multiplexingTicksWritten) / Interlocked.Read(ref _multiplexingBatchesSent) / 1000)
229+
_multiplexingAverageWriteTimePerBatchCounter = new PollingCounter("multiplexing-average-write-time-per-batch", this, GetMultiplexingAverageWriteTimePerBatch)
210230
{
211231
DisplayName = "Average write time per multiplexing batch (us)",
212232
DisplayUnits = "us"

0 commit comments

Comments
 (0)