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 incorrect time measurement by using monotonic time #70

Merged
merged 1 commit into from
May 3, 2018

Conversation

kyrylo
Copy link
Contributor

@kyrylo kyrylo commented Apr 16, 2018

Statsd#time uses the Time class to measure the difference between
elapsed time. This is incorrect because Time.now uses realtime,
which is meant to be used for system clock.

clock_gettime from time.h supports monotonic time. Let me quote
the man page of clock_gettime man to demonstrate the difference
between them:

CLOCK_REALTIME
the system's real time (i.e. wall time) clock, expressed as the
amount of time since the Epoch.

CLOCK_MONOTONIC
clock that increments monotonically, tracking the time since an
arbitrary point, and will continue to increment while the system is
asleep.

Since we measure elapsed time, we should be using monotonic time,
since it more accurate and meant to be used for that reason.

`Statsd#time` uses the `Time` class to measure the difference between
elapsed time. This is incorrect because `Time.now` uses realtime,
which is meant to be used for system clock.

`clock_gettime` from `time.h` supports *monotonic time*. Let me quote
the man page of `clock_gettime` man to demonstrate the difference
between them:

> CLOCK_REALTIME
> the system's real time (i.e. wall time) clock, expressed as the
> amount of time since the Epoch.

> CLOCK_MONOTONIC
> clock that increments monotonically, tracking the time since an
> arbitrary point, and will continue to increment while the system is
> asleep.

Since we measure elapsed time, we should be using monotonic time,
since it more accurate and meant to be used for that reason.
@kyrylo
Copy link
Contributor Author

kyrylo commented Apr 16, 2018

The errors in the build seem to be unrelated to the changes here (the specs pass locally for me).

@raggi raggi merged commit 63630b3 into reinh:master May 3, 2018
@raggi
Copy link
Collaborator

raggi commented May 3, 2018

Thank you!

bilbof pushed a commit to alphagov/publishing-api that referenced this pull request Jan 7, 2021
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

We therefore must update our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.

This is the same dependency on some Statsd internal behaviour, it
just _looks_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)
bilbof pushed a commit to alphagov/publishing-api that referenced this pull request Jan 7, 2021
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

We therefore must update our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.

This is the same dependency on some Statsd internal behaviour, it
just _looks_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)
bilbof pushed a commit to alphagov/publishing-api that referenced this pull request Jan 7, 2021
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

We therefore must update our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.

This is the same dependency on some Statsd internal behaviour, it
just _looks_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)
bilbof pushed a commit to alphagov/publishing-api that referenced this pull request Jan 7, 2021
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

I initially updated our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.
But, this is the same dependency on some Statsd internal behaviour, it
just _looked_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)

Instead, sleeping for 1 second doesn't also test rely on testing the
internals of the Statsd gem.
fredericfran-gds pushed a commit to alphagov/publishing-api that referenced this pull request Apr 29, 2021
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

We therefore must update our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.

This is the same dependency on some Statsd internal behaviour, it
just _looks_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants