Skip to content

Commit 3d26cd4

Browse files
authored
Merge pull request #32 from cbrevik/console-timer
Add gloo-console-timer
2 parents 0063e92 + 1126f2b commit 3d26cd4

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ version = "0.1.0"
99

1010
[dependencies]
1111
gloo-timers = { version = "0.1.0", path = "crates/timers" }
12+
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
1213

1314
[workspace]

crates/console-timer/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "gloo-console-timer"
3+
version = "0.1.0"
4+
authors = ["Rust and WebAssembly Working Group"]
5+
edition = "2018"
6+
7+
[dependencies.web-sys]
8+
version = "0.3.14"
9+
features = [
10+
"console",
11+
]
12+
13+
[dev-dependencies]
14+
wasm-bindgen-test = "0.2.37"
15+
gloo-timers = { version = "0.1.0", path = "../timers" }

crates/console-timer/src/lib.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

crates/console-timer/tests/web.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Test suite for the Web and headless browsers.
2+
3+
#![cfg(target_arch = "wasm32")]
4+
5+
use gloo_console_timer::ConsoleTimer;
6+
use wasm_bindgen_test::*;
7+
8+
wasm_bindgen_test_configure!(run_in_browser);
9+
10+
#[wasm_bindgen_test]
11+
fn scoped_timer_returns_value() {
12+
let value = ConsoleTimer::scope("foo", || true);
13+
14+
assert!(value);
15+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
#![deny(missing_docs, missing_debug_implementations)]
55

66
// Re-exports of toolkit crates.
7+
pub use gloo_console_timer as console_timer;
78
pub use gloo_timers as timers;

0 commit comments

Comments
 (0)