Skip to content

Commit

Permalink
Merge branch 'develop' into wip/jd/test-is_digit-181176532
Browse files Browse the repository at this point in the history
  • Loading branch information
jdunkerley committed Feb 11, 2022
2 parents 2678498 + 593a603 commit 4d2ee54
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 296 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 3 additions & 167 deletions lib/rust/ensogl/core/src/debug/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! The module provides a structure which defines the statistics we are interested in ([`Stats`]),
//! and contains methods for modifying as well as retrieving the current values of the statistics
//! (often also referred to with the shortcut term "stats"). It also provides methods that need
//! to be called to ensure that some of the statistics are properly calculated per each frame, and
//! helper utility types for accumulating and summarizing stats over multiple frames. The intention
//! behind this module is to aid in detecting and debugging possible performance issues in the GUI.
//! to be called to ensure that some of the statistics are properly calculated per each frame. The
//! intention behind this module is to aid in detecting and debugging possible performance issues
//! in the GUI.
//!
//! Note: some statistics will not be collected (the fields will be present but always zero) when
//! this crate is compiled without the `statistics` feature flag. This is mediated by the
Expand All @@ -14,11 +14,9 @@
//! - `data_upload_size`

use enso_prelude::*;
use enso_types::*;

use js_sys::ArrayBuffer;
use js_sys::WebAssembly::Memory;
use num_traits::cast;
use wasm_bindgen::JsCast;


Expand Down Expand Up @@ -119,62 +117,6 @@ macro_rules! gen_stats {
);

)* }


// === Accumulator ===

/// Contains aggregated data from multiple [`StatsData`] objects. This is intended to be
/// used as a mutable data structure, which can have new data continuously added. To
/// calculate a summary of the data based on the aggregated samples, its [`summarize()`]
/// method should be called.
#[derive(Debug, Default)]
pub struct Accumulator {
/// How many samples of [`StatsData`] were accumulated.
samples_count: u32,
$($field : ValueAccumulator<$field_type>),*
}

impl Accumulator {
/// Includes the data of the sample into the Accumulator.
pub fn add_sample(&mut self, sample: &StatsData) {
self.samples_count += 1;
if self.samples_count == 1 {
$( self.$field = ValueAccumulator::new(sample.$field); )*
} else {
$( self.$field.add_sample(sample.$field); )*
}
}

/// Calculates a summary of data added into the Accumulator till now. Returns a
/// non-empty result only if [`add_sample`] was called at least once.
pub fn summarize(&self) -> Option<Summary> {
if self.samples_count == 0 {
None
} else {
let n = self.samples_count as f64;
let summary = Summary {
$($field : ValueSummary{
min: self.$field.min,
max: self.$field.max,
avg: self.$field.sum / n,
}),*
};
Some(summary)
}
}
}


// === Summary ===

/// Contains summarized values of stats fields from multiple [`StatsData`] objects.
#[derive(Copy, Clone, Debug)]
pub struct Summary {
$(
#[allow(missing_docs)]
pub $field : ValueSummary<$field_type>
),*
}
}};
}

Expand Down Expand Up @@ -243,109 +185,3 @@ macro_rules! if_compiled_with_stats {
{}
};
}



// ========================
// === ValueAccumulator ===
// ========================

#[derive(Debug, Default)]
struct ValueAccumulator<T> {
min: T,
max: T,
sum: f64,
}

impl<T: Min + Max + PartialOrd + cast::AsPrimitive<f64> + Copy> ValueAccumulator<T> {
fn new(v: T) -> Self {
Self { min: v, max: v, sum: v.as_() }
}

fn add_sample(&mut self, v: T) {
self.min = min(self.min, v);
self.max = max(self.max, v);
self.sum += v.as_();
}
}



// ====================
// === ValueSummary ===
// ====================

/// Summary for multiple values of type T. Intended to be used for storing a summary of multiple
/// samples of some runtime stat.
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub struct ValueSummary<T> {
pub min: T,
pub max: T,
pub avg: f64,
}



// =============
// === Tests ===
// =============

#[cfg(test)]
mod tests {
use super::*;

use assert_approx_eq::assert_approx_eq;



macro_rules! test_with_new_sample {
(
$accumulator:expr;
$(
$field:ident : $type:tt = $sample:literal
=> min: $min:literal avg: $avg:literal max: $max:literal
)*
) => {
$(let $field: $type = $sample;)*
let sample_stats = StatsData { $($field,)* ..default() };
$accumulator.add_sample(&sample_stats);
let tested_summary = $accumulator.summarize().unwrap();
$(
test_with_new_sample!($type, tested_summary.$field.min, $min);
test_with_new_sample!(f64, tested_summary.$field.avg, $avg);
test_with_new_sample!($type, tested_summary.$field.max, $max);
)*
};

// Helper rules for asserting equality on various types
(f64, $val1:expr, $val2:expr) => { assert_approx_eq!($val1, $val2); };
(u32, $val1:expr, $val2:expr) => { assert_eq!($val1, $val2); };
(usize, $val1:expr, $val2:expr) => { assert_eq!($val1, $val2); };
}

#[test]
fn stats_summaries() {
// This tests attempts to verify calculation of proper summaries for stats of each
// primitive type supported by `gen_stats!`.

let mut accumulator: Accumulator = default();
assert!(matches!(accumulator.summarize(), None));

test_with_new_sample!(accumulator;
fps: f64 = 55.0 => min: 55.0 avg: 55.0 max: 55.0
wasm_memory_usage: u32 = 1000 => min: 1000 avg: 1000.0 max: 1000
buffer_count: usize = 3 => min: 3 avg: 3.0 max: 3
);
test_with_new_sample!(accumulator;
fps: f64 = 57.0 => min: 55.0 avg: 56.0 max: 57.0
wasm_memory_usage: u32 = 2000 => min: 1000 avg: 1500.0 max: 2000
buffer_count: usize = 2 => min: 2 avg: 2.5 max: 3
);
test_with_new_sample!(accumulator;
fps: f64 = 56.0 => min: 55.0 avg: 56.0 max: 57.0
wasm_memory_usage: u32 = 3000 => min: 1000 avg: 2000.0 max: 3000
buffer_count: usize = 1 => min: 1 avg: 2.0 max: 3
);
}
}
1 change: 0 additions & 1 deletion lib/rust/ensogl/example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ensogl-example-shape-system = { path = "shape-system" }
ensogl-example-slider = { path = "slider" }
ensogl-example-sprite-system = { path = "sprite-system" }
ensogl-example-sprite-system-benchmark = { path = "sprite-system-benchmark" }
ensogl-example-stats = { path = "stats" }
ensogl-example-text-area = { path = "text-area" }
#enso-frp = { path = "../../frp" }
#enso-logger = { path = "../../logger"}
Expand Down
1 change: 0 additions & 1 deletion lib/rust/ensogl/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ pub use ensogl_example_shape_system as shape_system;
pub use ensogl_example_slider as slider;
pub use ensogl_example_sprite_system as sprite_system;
pub use ensogl_example_sprite_system_benchmark as sprite_system_benchmark;
pub use ensogl_example_stats as stats;
pub use ensogl_example_text_area as text_area;
16 changes: 0 additions & 16 deletions lib/rust/ensogl/example/stats/Cargo.toml

This file was deleted.

97 changes: 0 additions & 97 deletions lib/rust/ensogl/example/stats/src/lib.rs

This file was deleted.

4 changes: 2 additions & 2 deletions lib/rust/types/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ macro_rules! gen_min {
)*};
}

gen_min!([f32, f64, i32, i64, u32, usize]);
gen_min!([f32, f64, i32, i64, usize]);



Expand All @@ -225,7 +225,7 @@ macro_rules! gen_max {
)*};
}

gen_max!([f32, f64, i32, i64, u32, usize]);
gen_max!([f32, f64, i32, i64, usize]);



Expand Down

0 comments on commit 4d2ee54

Please sign in to comment.