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
use std::time::Duration;
/// Describes the server-side resource consumption.
///
/// This object can be retrieved from `Connection`, `PreparedStatement`,
/// `ResultSet`, `BLob`, `CLob`, and `NCLob`.
///
/// ## Example
///
/// A `ResultSet` may cause additional roundtrips while its `Row`s are iterated over,
/// because not all rows might be transferred and missing rows might need to be fetched.
/// These fetches change the values in the `ServerUsage`
/// both of the `ResultSet` and of the `Connection` with which the `ResultSet` was obtained.
///
#[derive(Clone, Copy, Debug, Default)]
pub struct ServerUsage {
/// The server-side processing time that was consumed by the last server-call
/// that was triggered from the parent object.
pub proc_time: Duration,
/// The accumulated server-side processing time that was consumed by all server-calls
/// that were triggered from the parent object.
pub accum_proc_time: Duration,
// it is not clear when and how the cpu and memory are transferred,
// so we keep then invisible
// / The server-side cpu time that was consumed by the last server-call
// / that was triggered from the parent object.
cpu_time: Duration,
// / The accumulated server-side cpu time that was consumed by all server-calls
// / that were triggered from the parent object.
accum_cpu_time: Duration,
/// The server-side memory that was consumed by the last server-call
/// that was triggered from the parent object.
pub server_memory_usage: u64,
}
impl ServerUsage {
pub(crate) fn update(
&mut self,
o_server_proc_time: Option<Duration>,
o_server_cpu_time: Option<Duration>,
o_server_memory_usage: Option<u64>,
) {
if let Some(duration) = o_server_proc_time {
self.proc_time = duration;
self.accum_proc_time += duration;
}
if let Some(duration) = o_server_cpu_time {
self.cpu_time = duration;
self.accum_cpu_time += duration;
}
if let Some(server_memory_usage) = o_server_memory_usage {
self.server_memory_usage = server_memory_usage;
} else {
self.server_memory_usage = 0;
}
}
/// Server processing time
pub fn proc_time(&self) -> &Duration {
&self.proc_time
}
/// Accumulated server processing time
pub fn accum_proc_time(&self) -> &Duration {
&self.accum_proc_time
}
/// Server memory usage
pub fn server_memory_usage(&self) -> &u64 {
&self.server_memory_usage
}
}
impl std::fmt::Display for ServerUsage {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
formatter,
"\
processing time: {}.{:06}s, \
accumulated processing time: {}.{:06}s, \
",
self.proc_time.as_secs(),
self.proc_time.subsec_micros(),
self.accum_proc_time.as_secs(),
self.accum_proc_time.subsec_micros(),
)
}
}