-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add Instant
class to conveniently track elapsed and start/end times
#13397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
22d2244
to
0fd0447
Compare
986016f
to
8bea3c8
Compare
Throughout the codebase we often need to track elapsed times, using a performance clock, and also start/end times that given seconds since epoch (via `time.time()`). `Instant` encapsulates both functionalities to simplify the code and ensure we are using the correct functions from `_pytest.timing`, which we can mock in tests for reliability.
8bea3c8
to
0cf9cd9
Compare
src/_pytest/runner.py
Outdated
start=start, | ||
stop=stop, | ||
duration=duration, | ||
start=duration.start, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to make duration itself a attribute of run result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I would like to expose that object as part of the public API now.
d1716a6
to
fd86b59
Compare
fd86b59
to
65395b9
Compare
edc4ba4
to
46469ff
Compare
46469ff
to
2923b02
Compare
src/_pytest/timing.py
Outdated
default_factory=lambda: perf_counter(), init=False | ||
) | ||
|
||
def duration(self) -> Duration: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def duration(self) -> Duration: | |
def until_current(self) -> Duration: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"current" seems a bit weird to me, how about until_now
? as in "get me the duration until now"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to consult a LLM, Claude says:
Based on the code shown, I can suggest renaming the duration()
method to elapsed()
which would be more natural and aligns better with the concept of measuring time that has passed since the was created. This fits well with the existing property in the Duration
class and is commonly used in time measurement APIs. Instant``elapsed_s
Other natural alternatives could be:
elapsed_time()
time_since()
measure()
However, elapsed()
would be my top recommendation as it:
- Is concise and clear
- Matches common terminology in time measurement
- Aligns with the existing property in the codebase
elapsed_s
- Is consistent with similar APIs in other languages and libraries
I agree, further it is the same name used by Rust
's Instant
(which inspired this PR):
use std::time::{Duration, Instant};
use std::thread::sleep;
fn main() {
let now = Instant::now();
// we sleep for 2 seconds
sleep(Duration::new(2, 0));
// it prints '2'
println!("{}", now.elapsed().as_secs());
}
Agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
absolutely, thanks for researching this 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While at it, noticed that Duration.seconds
reads much better than Duration.elapsed_s
, as in instant.elapsed().seconds
vs instant.elapsed().elapsed_s
, so I renamed that as well. 👍
While at it, noticed that `Duration.seconds` reads much better than `Duration.elapsed_s`.
d52a620
to
e4810d2
Compare
@RonnyPfannschmidt re-requested review. 👍 |
Throughout the codebase we often need to track elapsed times, using a performance clock, and also start/end times that given seconds since epoch (via
time.time()
).Instant
encapsulates both functionalities to simplify the code and ensure we are using the correct functions from_pytest.timing
, which we can mock in tests for reliability.Built on top of #13394, will rebase once it gets merged.