2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-01-26 19:32:49 -05:00
|
|
|
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-01-26 19:32:49 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
2021-05-11 00:54:10 -04:00
|
|
|
use deno_core::serde_json::json;
|
2021-04-19 17:10:43 -04:00
|
|
|
use std::cmp;
|
2021-01-26 19:32:49 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::VecDeque;
|
2021-04-19 17:10:43 -04:00
|
|
|
use std::fmt;
|
2021-01-26 19:32:49 -05:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
2021-12-15 13:23:43 -05:00
|
|
|
use super::logging::lsp_debug;
|
|
|
|
|
2021-04-19 17:10:43 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2021-01-26 19:32:49 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct PerformanceAverage {
|
|
|
|
pub name: String,
|
|
|
|
pub count: u32,
|
|
|
|
pub average_duration: u32,
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:10:43 -04:00
|
|
|
impl PartialOrd for PerformanceAverage {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for PerformanceAverage {
|
|
|
|
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
|
|
|
self.name.cmp(&other.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:32:49 -05:00
|
|
|
/// A structure which serves as a start of a measurement span.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PerformanceMark {
|
|
|
|
name: String,
|
|
|
|
count: u32,
|
|
|
|
start: Instant,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A structure which holds the information about the measured span.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PerformanceMeasure {
|
|
|
|
pub name: String,
|
|
|
|
pub count: u32,
|
|
|
|
pub duration: Duration,
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:10:43 -04:00
|
|
|
impl fmt::Display for PerformanceMeasure {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{} ({}ms)", self.name, self.duration.as_millis())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:32:49 -05:00
|
|
|
impl From<PerformanceMark> for PerformanceMeasure {
|
|
|
|
fn from(value: PerformanceMark) -> Self {
|
|
|
|
Self {
|
|
|
|
name: value.name,
|
|
|
|
count: value.count,
|
|
|
|
duration: value.start.elapsed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A simple structure for marking a start of something to measure the duration
|
|
|
|
/// of and measuring that duration. Each measurement is identified by a string
|
|
|
|
/// name and a counter is incremented each time a new measurement is marked.
|
|
|
|
///
|
|
|
|
/// The structure will limit the size of measurements to the most recent 1000,
|
|
|
|
/// and will roll off when that limit is reached.
|
2022-01-17 17:09:43 -05:00
|
|
|
#[derive(Debug)]
|
2021-01-26 19:32:49 -05:00
|
|
|
pub struct Performance {
|
2022-01-17 17:09:43 -05:00
|
|
|
counts: Mutex<HashMap<String, u32>>,
|
2021-01-26 19:32:49 -05:00
|
|
|
max_size: usize,
|
2022-01-17 17:09:43 -05:00
|
|
|
measures: Mutex<VecDeque<PerformanceMeasure>>,
|
2021-01-26 19:32:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Performance {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
counts: Default::default(),
|
2022-01-24 03:01:33 -05:00
|
|
|
max_size: 3_000,
|
2021-01-26 19:32:49 -05:00
|
|
|
measures: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Performance {
|
|
|
|
/// Return the count and average duration of a measurement identified by name.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn average(&self, name: &str) -> Option<(usize, Duration)> {
|
|
|
|
let mut items = Vec::new();
|
2021-07-06 23:48:01 -04:00
|
|
|
for measure in self.measures.lock().iter() {
|
2021-01-26 19:32:49 -05:00
|
|
|
if measure.name == name {
|
|
|
|
items.push(measure.duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let len = items.len();
|
|
|
|
|
|
|
|
if len > 0 {
|
|
|
|
let average = items.into_iter().sum::<Duration>() / len as u32;
|
|
|
|
Some((len, average))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return an iterator which provides the names, count, and average duration
|
|
|
|
/// of each measurement.
|
|
|
|
pub fn averages(&self) -> Vec<PerformanceAverage> {
|
|
|
|
let mut averages: HashMap<String, Vec<Duration>> = HashMap::new();
|
2021-07-06 23:48:01 -04:00
|
|
|
for measure in self.measures.lock().iter() {
|
2021-01-26 19:32:49 -05:00
|
|
|
averages
|
|
|
|
.entry(measure.name.clone())
|
|
|
|
.or_default()
|
|
|
|
.push(measure.duration);
|
|
|
|
}
|
|
|
|
averages
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, d)| {
|
|
|
|
let a = d.clone().into_iter().sum::<Duration>() / d.len() as u32;
|
|
|
|
PerformanceAverage {
|
|
|
|
name: k,
|
|
|
|
count: d.len() as u32,
|
|
|
|
average_duration: a.as_millis() as u32,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks the start of a measurement which returns a performance mark
|
|
|
|
/// structure, which is then passed to `.measure()` to finalize the duration
|
|
|
|
/// and add it to the internal buffer.
|
2021-05-11 00:54:10 -04:00
|
|
|
pub fn mark<S: AsRef<str>, V: Serialize>(
|
|
|
|
&self,
|
|
|
|
name: S,
|
|
|
|
maybe_args: Option<V>,
|
|
|
|
) -> PerformanceMark {
|
2021-01-26 19:32:49 -05:00
|
|
|
let name = name.as_ref();
|
2021-07-06 23:48:01 -04:00
|
|
|
let mut counts = self.counts.lock();
|
2021-01-26 19:32:49 -05:00
|
|
|
let count = counts.entry(name.to_string()).or_insert(0);
|
|
|
|
*count += 1;
|
2021-05-11 00:54:10 -04:00
|
|
|
let msg = if let Some(args) = maybe_args {
|
|
|
|
json!({
|
|
|
|
"type": "mark",
|
|
|
|
"name": name,
|
|
|
|
"count": count,
|
|
|
|
"args": args,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
json!({
|
|
|
|
"type": "mark",
|
|
|
|
"name": name,
|
|
|
|
})
|
|
|
|
};
|
2021-12-15 13:23:43 -05:00
|
|
|
lsp_debug!("{},", msg);
|
2021-01-26 19:32:49 -05:00
|
|
|
PerformanceMark {
|
|
|
|
name: name.to_string(),
|
|
|
|
count: *count,
|
|
|
|
start: Instant::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A function which accepts a previously created performance mark which will
|
|
|
|
/// be used to finalize the duration of the span being measured, and add the
|
|
|
|
/// measurement to the internal buffer.
|
2021-02-11 23:17:48 -05:00
|
|
|
pub fn measure(&self, mark: PerformanceMark) -> Duration {
|
2021-01-26 19:32:49 -05:00
|
|
|
let measure = PerformanceMeasure::from(mark);
|
2021-12-15 13:23:43 -05:00
|
|
|
lsp_debug!(
|
2021-05-11 00:54:10 -04:00
|
|
|
"{},",
|
|
|
|
json!({
|
|
|
|
"type": "measure",
|
|
|
|
"name": measure.name,
|
|
|
|
"count": measure.count,
|
|
|
|
"duration": measure.duration.as_millis() as u32,
|
|
|
|
})
|
|
|
|
);
|
2021-02-11 23:17:48 -05:00
|
|
|
let duration = measure.duration;
|
2021-07-06 23:48:01 -04:00
|
|
|
let mut measures = self.measures.lock();
|
2021-04-19 17:10:43 -04:00
|
|
|
measures.push_front(measure);
|
2021-01-26 19:32:49 -05:00
|
|
|
while measures.len() > self.max_size {
|
2021-04-19 17:10:43 -04:00
|
|
|
measures.pop_back();
|
2021-01-26 19:32:49 -05:00
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
duration
|
2021-01-26 19:32:49 -05:00
|
|
|
}
|
2021-04-19 17:10:43 -04:00
|
|
|
|
|
|
|
pub fn to_vec(&self) -> Vec<PerformanceMeasure> {
|
2021-07-06 23:48:01 -04:00
|
|
|
let measures = self.measures.lock();
|
2021-04-19 17:10:43 -04:00
|
|
|
measures.iter().cloned().collect()
|
|
|
|
}
|
2021-01-26 19:32:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_average() {
|
|
|
|
let performance = Performance::default();
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark1 = performance.mark("a", None::<()>);
|
|
|
|
let mark2 = performance.mark("a", None::<()>);
|
|
|
|
let mark3 = performance.mark("b", None::<()>);
|
2021-01-26 19:32:49 -05:00
|
|
|
performance.measure(mark2);
|
|
|
|
performance.measure(mark1);
|
|
|
|
performance.measure(mark3);
|
|
|
|
let (count, _) = performance.average("a").expect("should have had value");
|
|
|
|
assert_eq!(count, 2);
|
|
|
|
let (count, _) = performance.average("b").expect("should have had value");
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
assert!(performance.average("c").is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_averages() {
|
|
|
|
let performance = Performance::default();
|
2021-05-11 00:54:10 -04:00
|
|
|
let mark1 = performance.mark("a", None::<()>);
|
|
|
|
let mark2 = performance.mark("a", None::<()>);
|
2021-01-26 19:32:49 -05:00
|
|
|
performance.measure(mark2);
|
|
|
|
performance.measure(mark1);
|
|
|
|
let averages = performance.averages();
|
|
|
|
assert_eq!(averages.len(), 1);
|
|
|
|
assert_eq!(averages[0].count, 2);
|
|
|
|
}
|
|
|
|
}
|