Skip to content

Commit f7a9517

Browse files
authored
Report Query times (#166)
* Report avg and total query timing * Report query times * fmt
1 parent 4ae1bc8 commit f7a9517

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/client.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use bytes::{Buf, BufMut, BytesMut};
33
use log::{debug, error, info, trace};
44
use std::collections::HashMap;
5+
use std::time::Instant;
56
use tokio::io::{split, AsyncReadExt, BufReader, ReadHalf, WriteHalf};
67
use tokio::net::TcpStream;
78
use tokio::sync::broadcast::Receiver;
@@ -994,6 +995,7 @@ where
994995
self.send_server_message(server, message, &address, &pool)
995996
.await?;
996997

998+
let query_start = Instant::now();
997999
// Read all data the server has to offer, which can be multiple messages
9981000
// buffered in 8196 bytes chunks.
9991001
loop {
@@ -1013,7 +1015,11 @@ where
10131015
}
10141016

10151017
// Report query executed statistics.
1016-
self.stats.query(self.process_id, server.server_id());
1018+
self.stats.query(
1019+
self.process_id,
1020+
server.server_id(),
1021+
Instant::now().duration_since(query_start).as_millis(),
1022+
);
10171023

10181024
Ok(())
10191025
}

src/stats.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
99
use tokio::time::Instant;
1010

1111
use crate::pool::{get_all_pools, get_number_of_addresses};
12-
use crate::server;
1312

1413
/// Convenience types for various stats
1514
type ClientStatesLookup = HashMap<i32, ClientInformation>;
@@ -138,6 +137,7 @@ enum EventName {
138137
Query {
139138
client_id: i32,
140139
server_id: i32,
140+
duration_ms: u128,
141141
},
142142
Transaction {
143143
client_id: i32,
@@ -269,11 +269,12 @@ impl Reporter {
269269
}
270270

271271
/// Report a query executed by a client against a server
272-
pub fn query(&self, client_id: i32, server_id: i32) {
272+
pub fn query(&self, client_id: i32, server_id: i32, duration_ms: u128) {
273273
let event = Event {
274274
name: EventName::Query {
275275
client_id,
276276
server_id,
277+
duration_ms,
277278
},
278279
value: 1,
279280
};
@@ -562,6 +563,7 @@ impl Collector {
562563
EventName::Query {
563564
client_id,
564565
server_id,
566+
duration_ms,
565567
} => {
566568
// Update client stats
567569
let app_name = match client_states.get_mut(&client_id) {
@@ -585,6 +587,11 @@ impl Collector {
585587
.entry("total_query_count".to_string())
586588
.or_insert(0);
587589
*counter += stat.value;
590+
591+
let duration = pool_stats
592+
.entry("total_query_time".to_string())
593+
.or_insert(0);
594+
*duration += duration_ms as i64;
588595
}
589596
None => (),
590597
}

tests/ruby/admin_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
processes.pgcat.shutdown
1212
end
1313

14+
describe "SHOW STATS" do
15+
context "clients connect and make one query" do
16+
it "updates *_query_time and *_wait_time" do
17+
connection = PG::connect("#{pgcat_conn_str}?application_name=one_query")
18+
connection.async_exec("SELECT pg_sleep(0.25)")
19+
connection.async_exec("SELECT pg_sleep(0.25)")
20+
connection.async_exec("SELECT pg_sleep(0.25)")
21+
connection.close
22+
23+
# wait for averages to be calculated, we shouldn't do this too often
24+
sleep(15.5)
25+
admin_conn = PG::connect(processes.pgcat.admin_connection_string)
26+
results = admin_conn.async_exec("SHOW STATS")[0]
27+
admin_conn.close
28+
expect(results["total_query_time"].to_i).to be_within(200).of(750)
29+
expect(results["avg_query_time"].to_i).to_not eq(0)
30+
31+
expect(results["total_wait_time"].to_i).to_not eq(0)
32+
expect(results["avg_wait_time"].to_i).to_not eq(0)
33+
end
34+
end
35+
end
36+
1437
describe "SHOW POOLS" do
1538
context "bad credentials" do
1639
it "does not change any stats" do

0 commit comments

Comments
 (0)