You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit renames all instances of SamplyInterval
or samply_interval to use the word "timer" instead.
After writing a lot of documentation and examples,
I have decided that I think "timer" feels more ergonomic
to use as an abstraction, even though it emits an interval
type marker.
This feels like too much work to interactive rebase back
into the history of this stack, so I'm appending it on.
<br> **1)** Add [samply-markers](https://crates.io/crates/samply-markers) as a dependency to your project's `Cargo.toml`.
@@ -17,8 +49,8 @@ samply-markers = "{version}"
17
49
> Samply markers are designed to no-op by default, so they must be explicitly enabled in order
18
50
> to see them in profiles.
19
51
>
20
-
> * Using samply-markers has effectively zero cost when not enabled.
21
-
> * Using samply-markers does not pull in any additional dependencies when not enabled.
52
+
> * Using [samply-markers](https://crates.io/crates/samply-markers) has effectively zero cost when not enabled.
53
+
> * Using [samply-markers](https://crates.io/crates/samply-markers) does not pull in any additional dependencies when not enabled.
22
54
23
55
```toml
24
56
[features]
@@ -36,22 +68,22 @@ inherits = "release"
36
68
debug = true
37
69
```
38
70
39
-
<br> **4)** Import the [`samply_markers::prelude`](https://docs.rs/samply-markers/latest/samply_markers/prelude/) to get started adding markers to your code.
40
-
41
-
<br> **5)** Build your project for profiling, then record the resulting binary with [samply] to capture the emitted markers.
71
+
<br> **4)** Build your project for profiling, then record the resulting binary with [samply] to process the emitted markers.
### The [`samply_marker!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_marker.html) macro
51
81
52
-
The [`samply_marker!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_marker.html) macro is the foundational way to emit profiler markers. It creates and emits a [`SamplyMarker`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyMarker.html) at the current location in your code, supporting both instant markers (a single point in time) and interval markers (a span of time).
82
+
The [`samply_marker!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_marker.html) macro is the foundational way to emit profiler markers.
83
+
It creates and emits a [`SamplyMarker`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyMarker.html) at the current location in your code,
84
+
supporting both instant markers (a single point in time) and interval markers (a span of time).
An interval marker spans from a start time to the current time using [`SamplyTimestamp`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyTimestamp.html):
72
104
@@ -76,10 +108,10 @@ use samply_markers::prelude::*;
76
108
fnquery_database(query:&str) ->Vec<String> {
77
109
letstart=SamplyTimestamp::now();
78
110
79
-
// ... execute the database query
80
-
letresults=vec![]; // Placeholder for actual results
111
+
// ... execute the database query.
112
+
letresults=vec![]; // Placeholder for actual results.
### The [`samply_timer!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_timer.html) macro
95
127
96
-
While `samply_marker!` requires manually capturing a [`SamplyTimestamp`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyTimestamp.html) for interval markers, the [`samply_interval!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_interval.html) macro simplifies this pattern by wrapping the timestamp in a scoped RAII object. It creates a [`SamplyInterval`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyInterval.html) that captures the start time when created and automatically emits the interval marker when dropped at the end of its scope.
128
+
While [`samply_marker!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_marker.html) requires manually providing a
129
+
[`SamplyTimestamp`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyTimestamp.html) for interval markers,
130
+
the [`samply_timer!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_timer.html) macro simplifies this pattern
131
+
by wrapping the timestamp in a scoped RAII object. It creates a [`SamplyTimer`](https://docs.rs/samply-markers/latest/samply_markers/marker/struct.SamplyTimer.html)
132
+
that registers the time it was created and automatically emits the interval marker when dropped at the end of its scope.
97
133
98
-
**Automatic Interval**
134
+
#### Automatic Interval
99
135
100
-
The interval emits when the variable is dropped:
136
+
The interval marker emits when the timer is dropped:
// Emit the interval now, excluding cleanup from the measurement
162
+
// Emit the interval marker now, excluding cleanup from the measurement.
127
163
timer.emit();
128
164
129
-
// ... perform cleanup tasks (not included in the interval)
165
+
// ... perform cleanup tasks (not included in the interval marker).
130
166
131
-
// The interval will not emit a second time when dropped
167
+
// The interval marker will not emit a second time when dropped.
132
168
}
133
169
```
134
170
135
171
---
136
172
137
-
#### The `samply_measure!` macro
173
+
### The [`samply_measure!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_measure.html) macro
174
+
175
+
Building on the scoped approach of [`samply_timer!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_timer.html),
176
+
the [`samply_measure!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_measure.html) macro further simplifies profiling
177
+
by eliminating the need to create a scoped timer yourself. Instead, you wrap a code block, then its execution time is automatically measured with an interval marker.
138
178
139
-
Building on the scoped approach of `samply_interval!`, the [`samply_measure!`](https://docs.rs/samply-markers/latest/samply_markers/macro.samply_measure.html) macro further simplifies profiling by eliminating the need to create a scoped object yourself. Instead, you wrap a code block, and the macro measures its execution time and emits an interval marker, while preserving the block's return value.
179
+
#### Synchronous Example
140
180
141
-
**Synchronous Example**
181
+
The value of the block expression is returned:
142
182
143
183
```rust
144
184
usesamply_markers::prelude::*;
145
185
146
-
fncompute_fibonacci(n:u32) ->u64 {
186
+
fncompute_sum(values:&[i32]) ->i32 {
147
187
samply_measure!({
148
-
// ... compute fibonacci
149
-
42// Return value is preserved
188
+
values.iter().sum()
150
189
}, marker: {
151
-
name:format!("fibonacci({n})"),
190
+
name:"compute sum",
152
191
})
153
192
}
193
+
194
+
letvalues=vec![1, 2, 3, 4, 5];
195
+
letresult=compute_sum(&values);
196
+
assert_eq!(result, 15);
154
197
```
155
198
156
-
**With `?` Operator**
199
+
#### With `?` Operator
157
200
158
201
The block's control flow is preserved, including early returns:
159
202
@@ -162,12 +205,12 @@ use samply_markers::prelude::*;
0 commit comments