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

Use Process.clock_gettime to measure track execution time #100

Merged
merged 1 commit into from
Nov 21, 2020

Conversation

mrzasa
Copy link
Contributor

@mrzasa mrzasa commented Nov 19, 2020

Using any variation of Time.now is not a reliable way of measuring elapsed time. It may be affected by jumps in local clock. Instead we can use a more reliable Process.clock_gettime(Process::CLOCK_MONOTINIC).

I've removed a Timecop dependency from the production code, but left it in the tests to check that a new solution works correctly with Timecop.

For detailed explaination, please read this excellent blogpost: https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/

@ArturT
Copy link
Member

ArturT commented Nov 19, 2020

@mrzasa Interesting article. Thank you for sharing.

So this solution should work fine when someone manipulates time with Timecop or uses Rails travel_to method, right?

I added this PR to my TODO list to take care of it.

@mrzasa
Copy link
Contributor Author

mrzasa commented Nov 19, 2020

I tested that it works correctly Timecop. travel_to stubs Date, Time and Datetime, so Process.clock_gettime shouldn't be affected.

@ArturT
Copy link
Member

ArturT commented Nov 19, 2020

Thank you @mrzasa

@ArturT
Copy link
Member

ArturT commented Nov 21, 2020

@mrzasa I've released a new version of knapsack gem 1.20.0

I also released a new version of knapsack_pro gem KnapsackPro/knapsack_pro-ruby#132

Here you can learn more about how Queue Mode in knapsack_pro gem can help run tests faster https://docs.knapsackpro.com/2020/how-to-speed-up-ruby-and-javascript-tests-with-ci-parallelisation

or you can check the latest feature to auto split slow RSpec test files by test examples:
https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants