Skip to content

Commit 01cf897

Browse files
committed
graph: Add a facility for tracking query effort
1 parent 38e21b3 commit 01cf897

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

graph/src/data/graphql/effort.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Utilities to keep moving statistics about queries
2+
3+
use std::collections::HashMap;
4+
use std::sync::{Arc, RwLock};
5+
use std::time::{Duration, Instant};
6+
7+
use crate::util::stats::MovingStats;
8+
9+
pub struct QueryEffort {
10+
inner: Arc<RwLock<QueryEffortInner>>,
11+
}
12+
13+
/// Track the effort for queries (identified by their ShapeHash) over a
14+
/// time window.
15+
struct QueryEffortInner {
16+
window_size: Duration,
17+
bin_size: Duration,
18+
effort: HashMap<u64, MovingStats>,
19+
total: MovingStats,
20+
}
21+
22+
impl QueryEffort {
23+
pub fn new(window_size: Duration, bin_size: Duration) -> Self {
24+
Self {
25+
inner: Arc::new(RwLock::new(QueryEffortInner::new(window_size, bin_size))),
26+
}
27+
}
28+
29+
pub fn add(&self, shape_hash: u64, duration: Duration) {
30+
let mut inner = self.inner.write().unwrap();
31+
inner.add(shape_hash, duration);
32+
}
33+
}
34+
35+
impl QueryEffortInner {
36+
fn new(window_size: Duration, bin_size: Duration) -> Self {
37+
Self {
38+
window_size,
39+
bin_size,
40+
effort: HashMap::default(),
41+
total: MovingStats::new(window_size, bin_size),
42+
}
43+
}
44+
45+
fn add(&mut self, shape_hash: u64, duration: Duration) {
46+
let window_size = self.window_size;
47+
let bin_size = self.bin_size;
48+
let now = Instant::now();
49+
self.effort
50+
.entry(shape_hash)
51+
.or_insert_with(|| MovingStats::new(window_size, bin_size))
52+
.add_at(now, duration);
53+
self.total.add_at(now, duration);
54+
}
55+
}

graph/src/data/graphql/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ pub use self::values::{
2121
};
2222

2323
pub mod shape_hash;
24+
25+
pub mod effort;

graph/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ pub mod prelude {
151151
pub use crate::log::split::split_logger;
152152
pub use crate::util::error::CompatErr;
153153
pub use crate::util::futures::{retry, TimeoutError};
154+
pub use crate::util::stats::MovingStats;
154155
}

0 commit comments

Comments
 (0)