1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Future timing instrumentation.
//!
//! Provides instrumentation to record the time taken by a future. This includes the busy time and
//! the idle time.
//!
//! ## Busy time
//!
//! The busy time of a future is the sum of all the time consumed during calls to [`Future::poll`]
//! on that future.
//!
//! ## Idle time
//!
//! The idle time of a future is the sum of all the time between calls to [`Future::poll`]. The
//! time before the first poll is not included.
//!
//! # Usage
//!
//! First, add this to your Cargo.toml `dependencies`:
//!
//! ```toml
//! future-timing = "0.1"
//! ```
//!
//! Record the timing of a future in the following manner.
//!
//! ```
//! # async fn some_async_fn() -> u64 {
//! # tokio::time::sleep(std::time::Duration::from_micros(10)).await;
//! # 42
//! # }
//! # fn do_something_with_output(_: u64) {}
//! # #[tokio::main]
//! # async fn main() {
//! let output = future_timing::timed(some_async_fn()).await;
//! let (timing, future_output) = output.into_parts();
//!
//! do_something_with_output(future_output);
//!
//! assert!(!timing.idle().is_zero());
//! assert!(!timing.busy().is_zero());
//! # }
//! ```
//!
//! # Comparison with similar crates
//!
//! This is a single purpose crate, created because I couldn't find any other crate that included
//! the functionality I needed, which is to say, record future timing and make it available to the
//! code that awaited the future upon that future resolving.
//!
//! If you want to record and analyze the timing of many different futures (and you're using the
//! [Tokio runtime], then you can use Tokio's [`RuntimeMetrics`] for an aggregated view or [Tokio
//! Console] to see the timings of each task individually.
//!
//! If you don't actualy want to record the timing of a future, but instead want a future which
//! resolves after a specific period of time, then you're in the wrong place. Have a look at the
//! [`async-timer`] crate instead.
//!
//! # Supported Rust Versions
//!
//! `future-timing` is built against the latest stable release. The minimum supported version is
//! 1.70. The current version of `future-timing` is not guaranteed to build on Rust versions earlier
//! than the minimum supported version.
//!
//! # License
//!
//! This project is licensed under the [MIT license].
//!
//! ## Contribution
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion
//! in `future-timing` by you, shall be licensed as MIT, without any additional terms or conditions.
//!
//! [`async-timer`]: https://docs.rs/async-timer/latest/async_timer/
//! [`RuntimeMetrics`]: https://docs.rs/tokio/latest/tokio/runtime/struct.RuntimeMetrics.html
//! [Tokio Console]: https://docs.rs/tokio-console/latest/tokio_console/
//! [Tokio runtime]: https://docs.rs/tokio/latest/tokio/
//! [MIT license]: https://github.com/hds/future-timing/blob/main/LICENSE
use ;
use pin_project;
/// Instrument a future to record its timing
///
/// The busy and idle time for the future will be recorded separately in the result together with
/// the output of the wrapped future. See the documentation for [`Timing`] for more details.
///
/// # Examples
///
/// ```
/// # async fn some_async_fn() -> u64 {
/// # tokio::time::sleep(std::time::Duration::from_micros(10)).await;
/// # 42
/// # }
/// # fn do_something_with_output(_: u64) {}
/// # #[tokio::main]
/// # async fn main() {
/// let output = future_timing::timed(some_async_fn()).await;
/// let (timing, future_output) = output.into_parts();
///
/// do_something_with_output(future_output);
///
/// assert!(!timing.idle().is_zero());
/// assert!(!timing.busy().is_zero());
/// # }
pin_project!
/// A wrapper around timing information for an instrumented future and that future's output.
///
/// See the documentation on [`Timing`] for further details.
/// The timing information for an instrumented future.
///
/// The busy time and the idle time of the instrumented future is available.
///
/// ## Busy time
///
/// The busy time of a future is the sum of all the time consumed during calls to [`Future::poll`]
/// on that future.
///
/// The busy time will always be non-zero.
///
/// ## Idle time
///
/// The idle time of a future is the sum of all the time between calls to [`Future::poll`]. The
/// time before the first poll is not included.
///
/// The idle time may be zero if the inner future returns [`Poll::Ready`] on the first poll (and so
/// never returns [`Poll::Pending`]).