Compare commits
5 Commits
v0.28.2-al
...
jm/metrics
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fe946b3e5 | ||
|
|
b5f2c0cef3 | ||
|
|
3b132e2768 | ||
|
|
904c950c59 | ||
|
|
5673bb5039 |
@@ -30,6 +30,7 @@ document-features = { version = "0.2.7", optional = true }
|
||||
instability = "0.3.1"
|
||||
itertools = "0.13"
|
||||
lru = "0.12.0"
|
||||
once_cell = "1.19.0"
|
||||
paste = "1.0.2"
|
||||
palette = { version = "0.7.6", optional = true }
|
||||
serde = { version = "1", optional = true, features = ["derive"] }
|
||||
@@ -40,6 +41,8 @@ time = { version = "0.3.11", optional = true, features = ["local-offset"] }
|
||||
unicode-segmentation = "1.10"
|
||||
unicode-truncate = "1"
|
||||
unicode-width = "0.1.13"
|
||||
metrics = { version = "0.23.0", git = "https://github.com/joshka/metrics.git", branch = "jm/derive-debug" }
|
||||
quanta = "0.12.3"
|
||||
|
||||
[target.'cfg(not(windows))'.dependencies]
|
||||
# termion is not supported on Windows
|
||||
@@ -55,6 +58,7 @@ fakeit = "1.1"
|
||||
font8x8 = "0.3.1"
|
||||
futures = "0.3.30"
|
||||
indoc = "2"
|
||||
metrics-util = { version = "0.17.0", git = "https://github.com/joshka/metrics.git", branch = "jm/derive-debug" }
|
||||
octocrab = "0.39.0"
|
||||
pretty_assertions = "1.4.0"
|
||||
rand = "0.8.5"
|
||||
@@ -293,6 +297,11 @@ name = "line_gauge"
|
||||
required-features = ["crossterm"]
|
||||
doc-scrape-examples = true
|
||||
|
||||
[[example]]
|
||||
name = "metrics"
|
||||
required-features = ["crossterm"]
|
||||
doc-scrape-examples = true
|
||||
|
||||
[[example]]
|
||||
name = "hyperlink"
|
||||
required-features = ["crossterm"]
|
||||
|
||||
227
examples/metrics.rs
Normal file
227
examples/metrics.rs
Normal file
@@ -0,0 +1,227 @@
|
||||
use std::{
|
||||
sync::{atomic::Ordering, Arc},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use color_eyre::Result;
|
||||
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
||||
use itertools::Itertools;
|
||||
use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit};
|
||||
use metrics_util::{
|
||||
registry::{AtomicStorage, Registry},
|
||||
Summary,
|
||||
};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
style::{palette::tailwind::SLATE, Stylize},
|
||||
widgets::{Row, Table, Widget},
|
||||
DefaultTerminal, Frame,
|
||||
};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let recorder = MetricsRecorder::new();
|
||||
let recorder_widget = recorder.widget();
|
||||
recorder.install();
|
||||
let terminal = ratatui::init();
|
||||
let app = App::new(recorder_widget);
|
||||
let result = app.run(terminal);
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct App {
|
||||
should_quit: bool,
|
||||
recorder_widget: RecorderWidget,
|
||||
}
|
||||
|
||||
impl App {
|
||||
const fn new(recorder_widget: RecorderWidget) -> Self {
|
||||
Self {
|
||||
should_quit: false,
|
||||
recorder_widget,
|
||||
}
|
||||
}
|
||||
|
||||
fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
let mut last_frame = Instant::now();
|
||||
let frame_duration = Duration::from_secs_f64(1.0 / 60.0);
|
||||
while !self.should_quit {
|
||||
if last_frame.elapsed() >= frame_duration {
|
||||
last_frame = Instant::now();
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
}
|
||||
self.handle_events(frame_duration.saturating_sub(last_frame.elapsed()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
let [top, main] =
|
||||
Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]).areas(frame.area());
|
||||
let title = if cfg!(debug_assertions) {
|
||||
"Metrics Example (debug)"
|
||||
} else {
|
||||
"Metrics Example (release)"
|
||||
};
|
||||
frame.render_widget(title.blue().into_centered_line(), top);
|
||||
frame.render_widget(&self.recorder_widget, main);
|
||||
}
|
||||
|
||||
fn handle_events(&mut self, timeout: Duration) -> Result<()> {
|
||||
if !event::poll(timeout)? {
|
||||
return Ok(());
|
||||
}
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_press(key),
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_key_press(&mut self, key: event::KeyEvent) {
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct MetricsRecorder {
|
||||
metrics: Arc<Metrics>,
|
||||
}
|
||||
|
||||
impl MetricsRecorder {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
fn widget(&self) -> RecorderWidget {
|
||||
RecorderWidget {
|
||||
metrics: Arc::clone(&self.metrics),
|
||||
}
|
||||
}
|
||||
|
||||
fn install(self) {
|
||||
metrics::set_global_recorder(self).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Metrics {
|
||||
registry: Registry<Key, AtomicStorage>,
|
||||
}
|
||||
|
||||
impl Default for Metrics {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
registry: Registry::atomic(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
fn counter(&self, key: &Key) -> Counter {
|
||||
self.registry
|
||||
.get_or_create_counter(key, |c| Counter::from_arc(c.clone()))
|
||||
}
|
||||
|
||||
fn gauge(&self, key: &Key) -> Gauge {
|
||||
self.registry
|
||||
.get_or_create_gauge(key, |g| Gauge::from_arc(g.clone()))
|
||||
}
|
||||
|
||||
fn histogram(&self, key: &Key) -> Histogram {
|
||||
self.registry
|
||||
.get_or_create_histogram(key, |h| Histogram::from_arc(h.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RecorderWidget {
|
||||
metrics: Arc<Metrics>,
|
||||
}
|
||||
|
||||
impl Widget for &RecorderWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer)
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let mut counters = vec![];
|
||||
self.metrics.registry.visit_counters(|key, counter| {
|
||||
let value = counter.load(Ordering::SeqCst);
|
||||
counters.push((key.clone(), value.to_string()));
|
||||
});
|
||||
let mut gauges = vec![];
|
||||
self.metrics.registry.visit_gauges(|key, gauge| {
|
||||
let value = gauge.load(Ordering::SeqCst);
|
||||
gauges.push((key.clone(), value.to_string()));
|
||||
});
|
||||
let mut histograms = vec![];
|
||||
self.metrics.registry.visit_histograms(|key, histogram| {
|
||||
let mut summary = Summary::with_defaults();
|
||||
for data in histogram.data() {
|
||||
summary.add(data);
|
||||
}
|
||||
if summary.is_empty() {
|
||||
// we omit the empty histograms, but this is how you would render them
|
||||
// histograms.push((key.clone(), "empty".to_string()));
|
||||
} else {
|
||||
let min = Duration::from_secs_f64(summary.min());
|
||||
let max = Duration::from_secs_f64(summary.max());
|
||||
let p50 = Duration::from_secs_f64(summary.quantile(0.5).unwrap());
|
||||
let p90 = Duration::from_secs_f64(summary.quantile(0.9).unwrap());
|
||||
let p99 = Duration::from_secs_f64(summary.quantile(0.99).unwrap());
|
||||
let line = format!(
|
||||
"min:{min:>9.2?} p50:{p50:>9.2?} p90:{p90:>9.2?} p99:{p99:>9.2?} max:{max:>9.2?}"
|
||||
);
|
||||
histograms.push((key.clone(), line));
|
||||
}
|
||||
});
|
||||
counters.sort();
|
||||
|
||||
gauges.sort();
|
||||
histograms.sort();
|
||||
let lines = counters
|
||||
.iter()
|
||||
.chain(gauges.iter())
|
||||
.chain(histograms.iter());
|
||||
let row_colors = [SLATE.c950, SLATE.c900];
|
||||
let rows = lines
|
||||
.map(|(key, line)| Row::new([key.name(), line]))
|
||||
.zip(row_colors.iter().cycle())
|
||||
.map(|(row, style)| row.bg(*style))
|
||||
.collect_vec();
|
||||
Table::new(rows, [Constraint::Length(40), Constraint::Fill(1)]).render(area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl Recorder for MetricsRecorder {
|
||||
fn describe_counter(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
|
||||
// todo!()
|
||||
}
|
||||
|
||||
fn describe_gauge(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
|
||||
// todo!()
|
||||
}
|
||||
|
||||
fn describe_histogram(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
|
||||
// todo!()
|
||||
}
|
||||
|
||||
fn register_counter(&self, key: &Key, metadata: &Metadata<'_>) -> Counter {
|
||||
self.metrics.counter(key)
|
||||
}
|
||||
|
||||
fn register_gauge(&self, key: &Key, metadata: &Metadata<'_>) -> Gauge {
|
||||
self.metrics.gauge(key)
|
||||
}
|
||||
|
||||
fn register_histogram(&self, key: &Key, metadata: &Metadata<'_>) -> Histogram {
|
||||
self.metrics.histogram(key)
|
||||
}
|
||||
}
|
||||
@@ -102,10 +102,13 @@
|
||||
//! [Ratatui Website]: https://ratatui.rs
|
||||
use std::io;
|
||||
|
||||
use metrics::{Counter, Histogram};
|
||||
use once_cell::sync::Lazy;
|
||||
use strum::{Display, EnumString};
|
||||
|
||||
use crate::{
|
||||
buffer::Cell,
|
||||
counter, duration_histogram,
|
||||
layout::{Position, Size},
|
||||
};
|
||||
|
||||
@@ -127,6 +130,89 @@ pub use self::termwiz::TermwizBackend;
|
||||
mod test;
|
||||
pub use self::test::TestBackend;
|
||||
|
||||
static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Metrics {
|
||||
pub clear_region_count: Counter,
|
||||
pub clear_region_duration: Histogram,
|
||||
pub draw_count: Counter,
|
||||
pub draw_duration: Histogram,
|
||||
pub append_lines_count: Counter,
|
||||
pub append_lines_duration: Histogram,
|
||||
pub hide_cursor_duration: Histogram,
|
||||
pub show_cursor_duration: Histogram,
|
||||
pub get_cursor_position_duration: Histogram,
|
||||
pub set_cursor_position_duration: Histogram,
|
||||
pub size_duration: Histogram,
|
||||
pub window_size_duration: Histogram,
|
||||
pub flush_count: Counter,
|
||||
pub flush_duration: Histogram,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
clear_region_count: counter!(
|
||||
"ratatui.backend.clear_region.count",
|
||||
"Number of times a region of the backend buffer was cleared"
|
||||
),
|
||||
clear_region_duration: duration_histogram!(
|
||||
"ratatui.backend.clear.time",
|
||||
"Time spent clearing the backend buffer"
|
||||
),
|
||||
draw_count: counter!(
|
||||
"ratatui.backend.draw.count",
|
||||
"Number of times the backend buffer was drawn to the terminal"
|
||||
),
|
||||
draw_duration: duration_histogram!(
|
||||
"ratatui.backend.draw.time",
|
||||
"Time spent drawing the backend buffer to the terminal"
|
||||
),
|
||||
hide_cursor_duration: duration_histogram!(
|
||||
"ratatui.backend.hide_cursor.time",
|
||||
"Time spent hiding the cursor in the backend"
|
||||
),
|
||||
show_cursor_duration: duration_histogram!(
|
||||
"ratatui.backend.show_cursor.time",
|
||||
"Time spent showing the cursor in the backend"
|
||||
),
|
||||
get_cursor_position_duration: duration_histogram!(
|
||||
"ratatui.backend.get_cursor_position.time",
|
||||
"Time spent getting the cursor position from the backend"
|
||||
),
|
||||
set_cursor_position_duration: duration_histogram!(
|
||||
"ratatui.backend.set_cursor_position.time",
|
||||
"Time spent setting the cursor position in the backend"
|
||||
),
|
||||
append_lines_count: counter!(
|
||||
"ratatui.backend.append_lines.count",
|
||||
"Number of times lines were appended to the backend buffer"
|
||||
),
|
||||
append_lines_duration: duration_histogram!(
|
||||
"ratatui.backend.append_lines.time",
|
||||
"Time spent appending lines to the backend buffer"
|
||||
),
|
||||
size_duration: duration_histogram!(
|
||||
"ratatui.backend.size.time",
|
||||
"Time spent getting the size of the backend buffer"
|
||||
),
|
||||
window_size_duration: duration_histogram!(
|
||||
"ratatui.backend.window_size.time",
|
||||
"Time spent getting the window size of the backend buffer"
|
||||
),
|
||||
flush_count: counter!(
|
||||
"ratatui.backend.flush.count",
|
||||
"Number of times the backend buffer was flushed to the terminal"
|
||||
),
|
||||
flush_duration: duration_histogram!(
|
||||
"ratatui.backend.flush.time",
|
||||
"Time spent flushing the backend buffer to the terminal"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Enum representing the different types of clearing operations that can be performed
|
||||
/// on the terminal screen.
|
||||
#[derive(Debug, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
//! [Crossterm]: https://crates.io/crates/crossterm
|
||||
use std::io::{self, Write};
|
||||
|
||||
use crossterm::cursor;
|
||||
#[cfg(feature = "underline-color")]
|
||||
use crossterm::style::SetUnderlineColor;
|
||||
|
||||
use super::METRICS;
|
||||
use crate::{
|
||||
backend::{Backend, ClearType, WindowSize},
|
||||
buffer::Cell,
|
||||
@@ -20,6 +22,7 @@ use crate::{
|
||||
terminal::{self, Clear},
|
||||
},
|
||||
layout::{Position, Size},
|
||||
metrics::HistogramExt,
|
||||
style::{Color, Modifier, Style},
|
||||
};
|
||||
|
||||
@@ -154,6 +157,9 @@ where
|
||||
where
|
||||
I: Iterator<Item = (u16, u16, &'a Cell)>,
|
||||
{
|
||||
METRICS.draw_count.increment(1);
|
||||
let _timing = METRICS.draw_duration.start_timing();
|
||||
|
||||
let mut fg = Color::Reset;
|
||||
let mut bg = Color::Reset;
|
||||
#[cfg(feature = "underline-color")]
|
||||
@@ -210,20 +216,24 @@ where
|
||||
}
|
||||
|
||||
fn hide_cursor(&mut self) -> io::Result<()> {
|
||||
let _timing = METRICS.hide_cursor_duration.start_timing();
|
||||
execute!(self.writer, Hide)
|
||||
}
|
||||
|
||||
fn show_cursor(&mut self) -> io::Result<()> {
|
||||
let _timing = METRICS.show_cursor_duration.start_timing();
|
||||
execute!(self.writer, Show)
|
||||
}
|
||||
|
||||
fn get_cursor_position(&mut self) -> io::Result<Position> {
|
||||
crossterm::cursor::position()
|
||||
let _timing = METRICS.get_cursor_position_duration.start_timing();
|
||||
cursor::position()
|
||||
.map(|(x, y)| Position { x, y })
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
|
||||
}
|
||||
|
||||
fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> io::Result<()> {
|
||||
let _timing = METRICS.set_cursor_position_duration.start_timing();
|
||||
let Position { x, y } = position.into();
|
||||
execute!(self.writer, MoveTo(x, y))
|
||||
}
|
||||
@@ -233,6 +243,8 @@ where
|
||||
}
|
||||
|
||||
fn clear_region(&mut self, clear_type: ClearType) -> io::Result<()> {
|
||||
METRICS.clear_region_count.increment(1);
|
||||
let _timing = METRICS.clear_region_duration.start_timing();
|
||||
execute!(
|
||||
self.writer,
|
||||
Clear(match clear_type {
|
||||
@@ -246,6 +258,8 @@ where
|
||||
}
|
||||
|
||||
fn append_lines(&mut self, n: u16) -> io::Result<()> {
|
||||
METRICS.append_lines_count.increment(1);
|
||||
let _timing = METRICS.append_lines_duration.start_timing();
|
||||
for _ in 0..n {
|
||||
queue!(self.writer, Print("\n"))?;
|
||||
}
|
||||
@@ -253,31 +267,37 @@ where
|
||||
}
|
||||
|
||||
fn size(&self) -> io::Result<Size> {
|
||||
let (width, height) = terminal::size()?;
|
||||
Ok(Size { width, height })
|
||||
let _timing = METRICS.size_duration.start_timing();
|
||||
terminal::size().map(Size::from)
|
||||
}
|
||||
|
||||
fn window_size(&mut self) -> io::Result<WindowSize> {
|
||||
let crossterm::terminal::WindowSize {
|
||||
columns,
|
||||
rows,
|
||||
width,
|
||||
height,
|
||||
} = terminal::window_size()?;
|
||||
Ok(WindowSize {
|
||||
columns_rows: Size {
|
||||
width: columns,
|
||||
height: rows,
|
||||
},
|
||||
pixels: Size { width, height },
|
||||
})
|
||||
let _timing = METRICS.window_size_duration.start_timing();
|
||||
terminal::window_size().map(WindowSize::from)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
METRICS.flush_count.increment(1);
|
||||
let _timing = METRICS.flush_duration.start_timing();
|
||||
self.writer.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crossterm::terminal::WindowSize> for WindowSize {
|
||||
fn from(value: crossterm::terminal::WindowSize) -> Self {
|
||||
Self {
|
||||
columns_rows: Size {
|
||||
width: value.columns,
|
||||
height: value.rows,
|
||||
},
|
||||
pixels: Size {
|
||||
width: value.width,
|
||||
height: value.height,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for CColor {
|
||||
fn from(color: Color) -> Self {
|
||||
match color {
|
||||
|
||||
@@ -3,10 +3,14 @@ use std::{
|
||||
ops::{Index, IndexMut},
|
||||
};
|
||||
|
||||
use metrics::Histogram;
|
||||
use once_cell::sync::Lazy;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::{buffer::Cell, layout::Position, prelude::*};
|
||||
use crate::{
|
||||
buffer::Cell, duration_histogram, layout::Position, metrics::HistogramExt, prelude::*,
|
||||
};
|
||||
|
||||
/// A buffer that maps to the desired content of the terminal after the draw call
|
||||
///
|
||||
@@ -70,6 +74,23 @@ pub struct Buffer {
|
||||
pub content: Vec<Cell>,
|
||||
}
|
||||
|
||||
static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);
|
||||
|
||||
struct Metrics {
|
||||
diff_duration: Histogram,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
diff_duration: duration_histogram!(
|
||||
"ratatui.buffer.diff.time",
|
||||
"Time spent diffing buffers"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
/// Returns a Buffer with all cells set to the default one
|
||||
#[must_use]
|
||||
@@ -464,6 +485,7 @@ impl Buffer {
|
||||
/// Updates: `0: a, 1: コ` (double width symbol at index 1 - skip index 2)
|
||||
/// ```
|
||||
pub fn diff<'a>(&self, other: &'a Self) -> Vec<(u16, u16, &'a Cell)> {
|
||||
let _timing = METRICS.diff_duration.start_timing();
|
||||
let previous_buffer = &self.content;
|
||||
let next_buffer = &other.content;
|
||||
|
||||
|
||||
@@ -310,6 +310,7 @@ pub use termwiz;
|
||||
pub mod backend;
|
||||
pub mod buffer;
|
||||
pub mod layout;
|
||||
pub(crate) mod metrics;
|
||||
pub mod prelude;
|
||||
pub mod style;
|
||||
pub mod symbols;
|
||||
|
||||
66
src/metrics.rs
Normal file
66
src/metrics.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use metrics::Histogram;
|
||||
|
||||
/// A helper macro that registers and describes a counter
|
||||
#[macro_export]
|
||||
macro_rules! counter {
|
||||
($name:expr, $description:expr $(,)?) => {{
|
||||
::metrics::describe_counter!($name, ::metrics::Unit::Count, $description);
|
||||
::metrics::counter!($name)
|
||||
}};
|
||||
}
|
||||
|
||||
/// A helper macro that registers and describes a histogram that tracks durations.
|
||||
#[macro_export]
|
||||
macro_rules! duration_histogram {
|
||||
($name:expr, $description:expr $(,)?) => {{
|
||||
::metrics::describe_histogram!($name, ::metrics::Unit::Seconds, $description);
|
||||
::metrics::histogram!($name)
|
||||
}};
|
||||
}
|
||||
|
||||
/// A helper macro that registers and describes a histogram that tracks bytes.
|
||||
#[macro_export]
|
||||
macro_rules! bytes_histogram {
|
||||
($name:expr, $description:expr $(,)?) => {{
|
||||
::metrics::describe_histogram!($name, ::metrics::Unit::Bytes, $description);
|
||||
::metrics::histogram!($name)
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) trait HistogramExt {
|
||||
fn measure_duration<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce() -> R;
|
||||
|
||||
fn start_timing(&self) -> DurationMeasurementGuard;
|
||||
}
|
||||
|
||||
impl HistogramExt for Histogram {
|
||||
fn measure_duration<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
{
|
||||
let start = quanta::Instant::now();
|
||||
let result = f();
|
||||
self.record(start.elapsed().as_secs_f64());
|
||||
result
|
||||
}
|
||||
|
||||
fn start_timing(&self) -> DurationMeasurementGuard {
|
||||
DurationMeasurementGuard {
|
||||
start: quanta::Instant::now(),
|
||||
histogram: self.clone(), // this is safe because `Histogram` stores an `Arc`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DurationMeasurementGuard {
|
||||
start: quanta::Instant,
|
||||
histogram: Histogram,
|
||||
}
|
||||
|
||||
impl Drop for DurationMeasurementGuard {
|
||||
fn drop(&mut self) {
|
||||
self.histogram.record(self.start.elapsed().as_secs_f64());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::io;
|
||||
|
||||
use metrics::{Counter, Histogram};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{
|
||||
backend::ClearType, buffer::Cell, prelude::*, CompletedFrame, TerminalOptions, Viewport,
|
||||
backend::ClearType, buffer::Cell, counter, duration_histogram, metrics::HistogramExt,
|
||||
prelude::*, CompletedFrame, TerminalOptions, Viewport,
|
||||
};
|
||||
|
||||
/// An interface to interact and draw [`Frame`]s on the user's terminal.
|
||||
@@ -84,6 +88,69 @@ pub struct Options {
|
||||
pub viewport: Viewport,
|
||||
}
|
||||
|
||||
static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Metrics {
|
||||
pub clear_duration: Histogram,
|
||||
pub draw_callback_duration: Histogram,
|
||||
pub draw_count: Counter,
|
||||
pub draw_duration: Histogram,
|
||||
pub flush_duration: Histogram,
|
||||
pub flush_count: Counter,
|
||||
pub resize_duration: Histogram,
|
||||
pub resize_count: Counter,
|
||||
pub insert_before_count: Counter,
|
||||
pub insert_before_duration: Histogram,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
clear_duration: duration_histogram!(
|
||||
"ratatui.terminal.clear.time",
|
||||
"Time spent clearing the terminal buffer"
|
||||
),
|
||||
draw_callback_duration: duration_histogram!(
|
||||
"ratatui.terminal.draw.callback.time",
|
||||
"Time spent calling the draw callback (application code)"
|
||||
),
|
||||
draw_count: counter!(
|
||||
"ratatui.terminal.draw.count",
|
||||
"Number of times the terminal buffer was drawn to the backend"
|
||||
),
|
||||
draw_duration: duration_histogram!(
|
||||
"ratatui.terminal.draw.time",
|
||||
"Time spent drawing the terminal buffer to the backend"
|
||||
),
|
||||
flush_duration: duration_histogram!(
|
||||
"ratatui.terminal.flush.time",
|
||||
"Time spent flushing the terminal buffer to the terminal"
|
||||
),
|
||||
flush_count: counter!(
|
||||
"ratatui.terminal.flush.count",
|
||||
"Number of times the terminal buffer was flushed to the terminal"
|
||||
),
|
||||
resize_duration: duration_histogram!(
|
||||
"ratatui.terminal.resize.time",
|
||||
"Time spent resizing the terminal buffer"
|
||||
),
|
||||
resize_count: counter!(
|
||||
"ratatui.terminal.resize.count",
|
||||
"Number of times the terminal buffer was resized"
|
||||
),
|
||||
insert_before_count: counter!(
|
||||
"ratatui.terminal.insert_before.count",
|
||||
"Number of times content was inserted before the viewport"
|
||||
),
|
||||
insert_before_duration: duration_histogram!(
|
||||
"ratatui.terminal.insert_before.time",
|
||||
"Time spent inserting content before the viewport"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> Drop for Terminal<B>
|
||||
where
|
||||
B: Backend,
|
||||
@@ -190,6 +257,8 @@ where
|
||||
/// Obtains a difference between the previous and the current buffer and passes it to the
|
||||
/// current backend for drawing.
|
||||
pub fn flush(&mut self) -> io::Result<()> {
|
||||
METRICS.flush_count.increment(1);
|
||||
let _timing = METRICS.flush_duration.start_timing();
|
||||
let previous_buffer = &self.buffers[1 - self.current];
|
||||
let current_buffer = &self.buffers[self.current];
|
||||
let updates = previous_buffer.diff(current_buffer);
|
||||
@@ -204,6 +273,8 @@ where
|
||||
/// Requested area will be saved to remain consistent when rendering. This leads to a full clear
|
||||
/// of the screen.
|
||||
pub fn resize(&mut self, area: Rect) -> io::Result<()> {
|
||||
METRICS.resize_count.increment(1);
|
||||
let _timing = METRICS.resize_duration.start_timing();
|
||||
let next_area = match self.viewport {
|
||||
Viewport::Fullscreen => area,
|
||||
Viewport::Inline(height) => {
|
||||
@@ -377,13 +448,17 @@ where
|
||||
F: FnOnce(&mut Frame) -> Result<(), E>,
|
||||
E: Into<io::Error>,
|
||||
{
|
||||
// Autoresize - otherwise we get glitches if shrinking or potential desync between widgets
|
||||
// and the terminal (if growing), which may OOB.
|
||||
METRICS.draw_count.increment(1);
|
||||
let _timing = METRICS.draw_duration.start_timing();
|
||||
// Autoresize - otherwise we get glitches if shrinking or potential desync between
|
||||
// widgets and the terminal (if growing), which may OOB.
|
||||
self.autoresize()?;
|
||||
|
||||
let mut frame = self.get_frame();
|
||||
|
||||
render_callback(&mut frame).map_err(Into::into)?;
|
||||
METRICS
|
||||
.draw_callback_duration
|
||||
.measure_duration(|| render_callback(&mut frame).map_err(Into::into))?;
|
||||
|
||||
// We can't change the cursor position right away because we have to flush the frame to
|
||||
// stdout first. But we also can't keep the frame around, since it holds a &mut to
|
||||
@@ -465,6 +540,7 @@ where
|
||||
|
||||
/// Clear the terminal and force a full redraw on the next draw call.
|
||||
pub fn clear(&mut self) -> io::Result<()> {
|
||||
let _timing = METRICS.clear_duration.start_timing();
|
||||
match self.viewport {
|
||||
Viewport::Fullscreen => self.backend.clear_region(ClearType::All)?,
|
||||
Viewport::Inline(_) => {
|
||||
@@ -573,9 +649,11 @@ where
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
METRICS.insert_before_count.increment(1);
|
||||
let _timing = METRICS.insert_before_duration.start_timing();
|
||||
// The approach of this function is to first render all of the lines to insert into a
|
||||
// temporary buffer, and then to loop drawing chunks from the buffer to the screen. drawing
|
||||
// this buffer onto the screen.
|
||||
// temporary buffer, and then to loop drawing chunks from the buffer to the screen.
|
||||
// drawing this buffer onto the screen.
|
||||
let area = Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -594,9 +672,9 @@ where
|
||||
let screen_height: i32 = self.last_known_area.height.into();
|
||||
|
||||
// The algorithm here is to loop, drawing large chunks of text (up to a screen-full at a
|
||||
// time), until the remainder of the buffer plus the viewport fits on the screen. We choose
|
||||
// this loop condition because it guarantees that we can write the remainder of the buffer
|
||||
// with just one call to Self::draw_lines().
|
||||
// time), until the remainder of the buffer plus the viewport fits on the screen. We
|
||||
// choose this loop condition because it guarantees that we can write the
|
||||
// remainder of the buffer with just one call to Self::draw_lines().
|
||||
while buffer_height + viewport_height > screen_height {
|
||||
// We will draw as much of the buffer as possible on this iteration in order to make
|
||||
// forward progress. So we have:
|
||||
@@ -604,8 +682,9 @@ where
|
||||
// to_draw = min(buffer_height, screen_height)
|
||||
//
|
||||
// We may need to scroll the screen up to make room to draw. We choose the minimal
|
||||
// possible scroll amount so we don't end up with the viewport sitting in the middle of
|
||||
// the screen when this function is done. The amount to scroll by is:
|
||||
// possible scroll amount so we don't end up with the viewport sitting in the middle
|
||||
// of the screen when this function is done. The amount to scroll by
|
||||
// is:
|
||||
//
|
||||
// scroll_up = max(0, drawn_height + to_draw - screen_height)
|
||||
//
|
||||
@@ -623,13 +702,14 @@ where
|
||||
|
||||
// There is now enough room on the screen for the remaining buffer plus the viewport,
|
||||
// though we may still need to scroll up some of the existing text first. It's possible
|
||||
// that by this point we've drained the buffer, but we may still need to scroll up to make
|
||||
// room for the viewport.
|
||||
// that by this point we've drained the buffer, but we may still need to scroll up to
|
||||
// make room for the viewport.
|
||||
//
|
||||
// We want to scroll up the exact amount that will leave us completely filling the screen.
|
||||
// However, it's possible that the viewport didn't start on the bottom of the screen and
|
||||
// the added lines weren't enough to push it all the way to the bottom. We deal with this
|
||||
// case by just ensuring that our scroll amount is non-negative.
|
||||
// We want to scroll up the exact amount that will leave us completely filling the
|
||||
// screen. However, it's possible that the viewport didn't start on the
|
||||
// bottom of the screen and the added lines weren't enough to push it all
|
||||
// the way to the bottom. We deal with this case by just ensuring that our
|
||||
// scroll amount is non-negative.
|
||||
//
|
||||
// We want:
|
||||
// screen_height = drawn_height - scroll_up + buffer_height + viewport_height
|
||||
@@ -651,8 +731,9 @@ where
|
||||
|
||||
// Clear the viewport off the screen. We didn't clear earlier for two reasons. First, it
|
||||
// wasn't necessary because the buffer we drew out of isn't sparse, so it overwrote
|
||||
// whatever was on the screen. Second, there is a weird bug with tmux where a full screen
|
||||
// clear plus immediate scrolling causes some garbage to go into the scrollback.
|
||||
// whatever was on the screen. Second, there is a weird bug with tmux where a full
|
||||
// screen clear plus immediate scrolling causes some garbage to go into the
|
||||
// scrollback.
|
||||
self.clear()?;
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user