diff options
author | Jan Katins <jasc@gmx.net> | 2020-09-21 19:06:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-21 19:06:15 +0200 |
commit | 6426bbe3e489faa4b3ebefc028f37496b3ca5caa (patch) | |
tree | 5e9c9003aab051ba258ae544fc2bf47216157157 /src/module.rs | |
parent | bb324834a515e18ca7a913ad6248cb8049e3ec33 (diff) |
feat: Add timings subcommand (#1629)
* feat: Add computational duration to all computed modules
This also means that in case we do some computations and these end up empty, we submit an empty module
* feat: Add timings subcommand
This outputs the timings of all computed modules, sorted by the duration it took to compute the module.
Useful for debugging why the prompt takes so long.
* feat: Add timings to explain output
* fix: Ensure that even empty custom modules get timings
* format main.rs
* feat: Only show interesting timings
* fix(tests): Change tests to look for empty string instead of None
* Use proper wording in timings help
* Revert "fix(tests): Change tests to look for empty string instead of None"
This reverts commit aca5bd1b03c48e1dee1b7ca91d66e2bda2d5a97c.
* fix(tests): Returning None in case the module produced an empty string
* fix: Ensure that linebreaks (and space) make a module not-empty
* Make cargo clippy happy
* Make Module.duration a proper Duration
* Only return a module if we would report it
* Change to cleaner way to return None for empty modules
* Avoid unnecessary module creation
* Simplify a string comparison
* Add timings to trace
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Diffstat (limited to 'src/module.rs')
-rw-r--r-- | src/module.rs | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/module.rs b/src/module.rs index 5ffb0cb69..768071c9c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,6 +3,7 @@ use crate::segment::Segment; use crate::utils::wrap_colorseq_for_shell; use ansi_term::{ANSIString, ANSIStrings}; use std::fmt; +use std::time::Duration; // List of all modules // Keep these ordered alphabetically. @@ -73,6 +74,9 @@ pub struct Module<'a> { /// The collection of segments that compose this module. pub segments: Vec<Segment>, + + /// the time it took to compute this module + pub duration: Duration, } impl<'a> Module<'a> { @@ -83,6 +87,7 @@ impl<'a> Module<'a> { name: name.to_string(), description: desc.to_string(), segments: Vec::new(), + duration: Duration::default(), } } @@ -105,7 +110,8 @@ impl<'a> Module<'a> { pub fn is_empty(&self) -> bool { self.segments .iter() - .all(|segment| segment.value.trim().is_empty()) + // no trim: if we add spaces/linebreaks it's not "empty" as we change the final output + .all(|segment| segment.value.is_empty()) } /// Get values of the module's segments @@ -167,6 +173,7 @@ mod tests { name: name.to_string(), description: desc.to_string(), segments: Vec::new(), + duration: Duration::default(), }; assert!(module.is_empty()); @@ -181,8 +188,39 @@ mod tests { name: name.to_string(), description: desc.to_string(), segments: vec![Segment::new(None, "")], + duration: Duration::default(), }; assert!(module.is_empty()); } + + #[test] + fn test_module_is_not_empty_with_linebreak_only() { + let name = "unit_test"; + let desc = "This is a unit test"; + let module = Module { + config: None, + name: name.to_string(), + description: desc.to_string(), + segments: vec![Segment::new(None, "\n")], + duration: Duration::default(), + }; + + assert!(!module.is_empty()); + } + + #[test] + fn test_module_is_not_empty_with_space_only() { + let name = "unit_test"; + let desc = "This is a unit test"; + let module = Module { + config: None, + name: name.to_string(), + description: desc.to_string(), + segments: vec![Segment::new(None, " ")], + duration: Duration::default(), + }; + + assert!(!module.is_empty()); + } } |