kvdb/
io_stats.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Generic statistics for key-value databases
10
11/// Statistic kind to query.
12pub enum Kind {
13	/// Overall statistics since start.
14	Overall,
15	/// Statistics since previous query.
16	SincePrevious,
17}
18
19/// Statistic for the `span` period
20#[derive(Debug, Clone)]
21pub struct IoStats {
22	/// Number of transaction.
23	pub transactions: u64,
24	/// Number of read operations.
25	pub reads: u64,
26	/// Number of reads resulted in a read from cache.
27	pub cache_reads: u64,
28	/// Number of write operations.
29	pub writes: u64,
30	/// Number of bytes read
31	pub bytes_read: u64,
32	/// Number of bytes read from cache
33	pub cache_read_bytes: u64,
34	/// Number of bytes write
35	pub bytes_written: u64,
36	/// Start of the statistic period.
37	pub started: std::time::Instant,
38	/// Total duration of the statistic period.
39	pub span: std::time::Duration,
40}
41
42impl IoStats {
43	/// Empty statistic report.
44	pub fn empty() -> Self {
45		Self {
46			transactions: 0,
47			reads: 0,
48			cache_reads: 0,
49			writes: 0,
50			bytes_read: 0,
51			cache_read_bytes: 0,
52			bytes_written: 0,
53			started: std::time::Instant::now(),
54			span: std::time::Duration::default(),
55		}
56	}
57
58	/// Average batch (transaction) size (writes per transaction)
59	pub fn avg_batch_size(&self) -> f64 {
60		if self.writes == 0 {
61			return 0.0
62		}
63		self.transactions as f64 / self.writes as f64
64	}
65
66	/// Read operations per second.
67	pub fn reads_per_sec(&self) -> f64 {
68		if self.span.as_secs_f64() == 0.0 {
69			return 0.0
70		}
71
72		self.reads as f64 / self.span.as_secs_f64()
73	}
74
75	pub fn byte_reads_per_sec(&self) -> f64 {
76		if self.span.as_secs_f64() == 0.0 {
77			return 0.0
78		}
79
80		self.bytes_read as f64 / self.span.as_secs_f64()
81	}
82
83	/// Write operations per second.
84	pub fn writes_per_sec(&self) -> f64 {
85		if self.span.as_secs_f64() == 0.0 {
86			return 0.0
87		}
88
89		self.writes as f64 / self.span.as_secs_f64()
90	}
91
92	pub fn byte_writes_per_sec(&self) -> f64 {
93		if self.span.as_secs_f64() == 0.0 {
94			return 0.0
95		}
96
97		self.bytes_written as f64 / self.span.as_secs_f64()
98	}
99
100	/// Total number of operations per second.
101	pub fn ops_per_sec(&self) -> f64 {
102		if self.span.as_secs_f64() == 0.0 {
103			return 0.0
104		}
105
106		(self.writes as f64 + self.reads as f64) / self.span.as_secs_f64()
107	}
108
109	/// Transactions per second.
110	pub fn transactions_per_sec(&self) -> f64 {
111		if self.span.as_secs_f64() == 0.0 {
112			return 0.0
113		}
114
115		(self.transactions as f64) / self.span.as_secs_f64()
116	}
117
118	pub fn avg_transaction_size(&self) -> f64 {
119		if self.transactions == 0 {
120			return 0.0
121		}
122
123		self.bytes_written as f64 / self.transactions as f64
124	}
125
126	pub fn cache_hit_ratio(&self) -> f64 {
127		if self.reads == 0 {
128			return 0.0
129		}
130
131		self.cache_reads as f64 / self.reads as f64
132	}
133}