Skip to content
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

Fix issue #79 (negative duration/elapsed time) #80

Merged
merged 2 commits into from
Mar 7, 2024
Merged

Conversation

oswaldobapvicjr
Copy link
Owner

@oswaldobapvicjr oswaldobapvicjr commented Mar 7, 2024

This PR solves the bug #79.

I could reproduce the trouble creating a MonitoredRunnable that executes a simple and fast runnable 1 million times (the average duration for the SYSTEM_TIME counter was 0.000002312 seconds).

During the analysis, I realized that, in some (frequent) cases, the nanoseconds after the operation were populated with an amount that was lower than the value retrieved before the operation, due to a known accuracy issue inside JDK's ThreadMXBean.

According to the JDK 17 Javadoc: "The CPU time provided is of nanoseconds precision but not necessarily nanoseconds accuracy" (https://docs.oracle.com/en/java/javase/17/docs/api/java.management/java/lang/management/ThreadMXBean.html).

So, to avoid the negative elapsed time, I implemented the following fixes:

  • The Counter.elapsedTimeNanos() was modified to return zero (instead of -1) when the time retrieved after the operation is lower than the one retrieved before the operation
  • The Duration class now also allows negative amounts, to prevent the issue from happening in another situation (for example, if CPU time is not supported by the JVM)

The fix was validated with JUnit and a new stress test method at the PerformetricsTestDrive class.

✅ The fix will be released in Performetrics version 2.5.2.

@oswaldobapvicjr oswaldobapvicjr added the bug Something isn't working label Mar 7, 2024
@oswaldobapvicjr oswaldobapvicjr self-assigned this Mar 7, 2024
@oswaldobapvicjr oswaldobapvicjr linked an issue Mar 7, 2024 that may be closed by this pull request
Copy link

codecov bot commented Mar 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1c595bf) to head (4306f58).

Additional details and impacted files
@@             Coverage Diff             @@
##              master       #80   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
+ Complexity       326       325    -1     
===========================================
  Files             21        21           
  Lines            831       829    -2     
  Branches          34        34           
===========================================
- Hits             831       829    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@oswaldobapvicjr oswaldobapvicjr merged commit beddca5 into master Mar 7, 2024
6 checks passed
@oswaldobapvicjr oswaldobapvicjr deleted the issue/0079 branch March 7, 2024 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The duration amount must be a positive value
1 participant