|
| 1 | +/*! |
| 2 | +
|
| 3 | +The `console.time` and `console.timeEnd` functions allow you to log the |
| 4 | +timing of named operations to the browser's developer tools console. You |
| 5 | +call `console.time("foo")` when the operation begins, and call |
| 6 | +`console.timeEnd("foo")` when it finishes. |
| 7 | +
|
| 8 | +Additionally, these measurements will show up in your browser's profiler's |
| 9 | +"timeline" or "waterfall" view. |
| 10 | +
|
| 11 | +[See MDN for more info](https://developer.mozilla.org/en-US/docs/Web/API/console#Timers). |
| 12 | +
|
| 13 | +This API wraps both the `time` and `timeEnd` calls into a single type |
| 14 | +named `ConsoleTimer`, ensuring both are called. |
| 15 | +
|
| 16 | +## Scoped measurement |
| 17 | +
|
| 18 | +Wrap code to be measured in a closure with `ConsoleTimer::scope`. |
| 19 | +
|
| 20 | +```no_run |
| 21 | +use gloo_console_timer::ConsoleTimer; |
| 22 | +
|
| 23 | +let value = ConsoleTimer::scope("foo", || { |
| 24 | + // Place code to be measured here |
| 25 | + // Optionally return a value. |
| 26 | +}); |
| 27 | +``` |
| 28 | +
|
| 29 | +## RAII-style measurement |
| 30 | +
|
| 31 | +For scenarios where `ConsoleTimer::scope` can't be used, like with |
| 32 | +asynchronous operations, you can use `ConsoleTimer::new` to create a timer. |
| 33 | +The measurement ends when the timer object goes out of scope / is dropped. |
| 34 | +
|
| 35 | +```no_run |
| 36 | +use gloo_console_timer::ConsoleTimer; |
| 37 | +use gloo_timers::Timeout; |
| 38 | +
|
| 39 | +let timeout = Timeout::new(1_000, move || { |
| 40 | + let _timer = ConsoleTimer::new("foo"); |
| 41 | + // Do some work which will be measured |
| 42 | +}); |
| 43 | +``` |
| 44 | +
|
| 45 | + */ |
| 46 | + |
| 47 | +#![deny(missing_docs, missing_debug_implementations)] |
| 48 | + |
| 49 | +use web_sys::console; |
| 50 | + |
| 51 | +/// A console time measurement. |
| 52 | +/// |
| 53 | +/// See `ConsoleTimer::scope` for starting a labeled time measurement |
| 54 | +/// of code wrapped in a closure. |
| 55 | +#[derive(Debug)] |
| 56 | +pub struct ConsoleTimer<'a> { |
| 57 | + label: &'a str, |
| 58 | +} |
| 59 | + |
| 60 | +impl<'a> ConsoleTimer<'a> { |
| 61 | + /// Starts a console time measurement. The measurement |
| 62 | + /// ends when the constructed `ConsoleTimer` object is dropped. |
| 63 | + /// |
| 64 | + /// # Example |
| 65 | + /// |
| 66 | + /// ```no_run |
| 67 | + /// use gloo_console_timer::ConsoleTimer; |
| 68 | + /// |
| 69 | + /// let _timer = ConsoleTimer::new("foo"); |
| 70 | + /// ``` |
| 71 | + pub fn new(label: &'a str) -> ConsoleTimer<'a> { |
| 72 | + console::time_with_label(label); |
| 73 | + ConsoleTimer { label } |
| 74 | + } |
| 75 | + |
| 76 | + /// Starts a scoped console time measurement |
| 77 | + /// |
| 78 | + /// # Example |
| 79 | + /// |
| 80 | + /// ```no_run |
| 81 | + /// use gloo_console_timer::ConsoleTimer; |
| 82 | + /// |
| 83 | + /// let value = ConsoleTimer::scope("foo", || { |
| 84 | + /// // Code to measure here |
| 85 | + /// }); |
| 86 | + /// ``` |
| 87 | + pub fn scope<F, T>(label: &str, f: F) -> T |
| 88 | + where |
| 89 | + F: FnOnce() -> T, |
| 90 | + { |
| 91 | + let _timer = ConsoleTimer::new(label); |
| 92 | + f() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<'a> Drop for ConsoleTimer<'a> { |
| 97 | + fn drop(&mut self) { |
| 98 | + console::time_end_with_label(self.label); |
| 99 | + } |
| 100 | +} |
0 commit comments