-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
time: Improve
Instant::now()
perf with test-util (#5513)
The test-util feature flag is only intended to be used with tests. However, it is possible to enable it in release mode accidentally. This patch reduces the overhead of `Instant::now()` when the `test-util` feature flag is enabled but `time::pause()` is not called. The optimization is implemented by adding a static atomic flag that tracks if `time::pause()` has ever been called. In `Instant::now()`, the atomic flag is first checked before the thread-local and mutex are accessed.
- Loading branch information
1 parent
815d89a
commit ee1c940
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Benchmark spawning a task onto the basic and threaded Tokio executors. | ||
//! This essentially measure the time to enqueue a task in the local and remote | ||
//! case. | ||
|
||
#[macro_use] | ||
extern crate bencher; | ||
|
||
use bencher::{black_box, Bencher}; | ||
|
||
fn time_now_current_thread(bench: &mut Bencher) { | ||
let rt = tokio::runtime::Builder::new_current_thread() | ||
.enable_time() | ||
.build() | ||
.unwrap(); | ||
|
||
bench.iter(|| { | ||
rt.block_on(async { | ||
black_box(tokio::time::Instant::now()); | ||
}) | ||
}) | ||
} | ||
|
||
bencher::benchmark_group!(time_now, time_now_current_thread,); | ||
|
||
bencher::benchmark_main!(time_now); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![cfg(all(feature = "full"))] | ||
|
||
use tokio::time::{Duration, Instant}; | ||
|
||
#[tokio::test(start_paused = true)] | ||
async fn test_start_paused() { | ||
let now = Instant::now(); | ||
|
||
// Pause a few times w/ std sleep and ensure `now` stays the same | ||
for _ in 0..5 { | ||
std::thread::sleep(Duration::from_millis(1)); | ||
assert_eq!(now, Instant::now()); | ||
} | ||
} |