Skip to content

Commit f33e36c

Browse files
committed
[#29205] docdb: Fixed rounding error in StopWatch
Summary: This test expects simulated sleep time to match the expectations, but depending on number of CPU cycles per seconds there might be a rounding error in `StopWatch` calculations. Fixed this by updating calculation formula to first do multiplication and then do division. Jira: DB-18968 Test Plan: Jenkins Reviewers: arybochkin Reviewed By: arybochkin Subscribers: ybase Tags: #jenkins-ready Differential Revision: https://phorge.dev.yugabyte.com/D48042
1 parent 1a22cc7 commit f33e36c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/yb/rocksdb/util/stop_watch.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ class StopWatch {
6868
if (!elapsed_ && !stats_enabled_) {
6969
return;
7070
}
71-
const auto elapsed = (env_->NowCpuCycles() - start_time_) /
72-
(base::CyclesPerSecond() / UnitsInSecond(kTimeResolution));
71+
// Multiply first and then divide to minimize rounding error (important for some tests), use
72+
// double to avoid overflow.
73+
const auto elapsed = 1.0 * (env_->NowCpuCycles() - start_time_) *
74+
UnitsInSecond(kTimeResolution) / base::CyclesPerSecond();
7375
if (elapsed_) {
7476
*elapsed_ = elapsed;
7577
}

0 commit comments

Comments
 (0)