-
Notifications
You must be signed in to change notification settings - Fork 36
Use Stopwatch.GetTimestamp on MacOS #540
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8bdeb29
macos sw
08cd8da
Merge branch 'main' into users/alexpeck/macostime
bitfaster 5737a9e
dump params
a3f6379
more info
bf79fc2
Merge branch 'main' into users/alexpeck/macostime
f3db75a
use duration
849fdbb
merge
6d69518
use duration
62d47bd
fix test
7902e39
cleanup
81abb03
duration basic tests
20e0094
more duration tests
cd16803
update bench
6370b8d
bench duration
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,160 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using BitFaster.Caching.Lru; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace BitFaster.Caching.UnitTests | ||
{ | ||
public class DurationTests | ||
{ | ||
private readonly ITestOutputHelper testOutputHelper; | ||
|
||
public DurationTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
this.testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
public void SinceEpoch() | ||
{ | ||
#if NETCOREAPP3_0_OR_GREATER | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
// eps is 1/200 of a second | ||
ulong eps = (ulong)(Stopwatch.Frequency / 200); | ||
Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); | ||
} | ||
else | ||
{ | ||
Duration.SinceEpoch().raw.Should().BeCloseTo(Environment.TickCount64, 15); | ||
} | ||
#else | ||
// eps is 1/200 of a second | ||
ulong eps = (ulong)(Stopwatch.Frequency / 200); | ||
Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps); | ||
#endif | ||
} | ||
|
||
[Fact] | ||
public void ToTimeSpan() | ||
{ | ||
#if NETCOREAPP3_0_OR_GREATER | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
new Duration(100).ToTimeSpan().Should().BeCloseTo(new TimeSpan(100), TimeSpan.FromMilliseconds(50)); | ||
} | ||
else | ||
{ | ||
new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10)); | ||
} | ||
#else | ||
// for Stopwatch.GetTimestamp() this is number of ticks | ||
new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10)); | ||
#endif | ||
} | ||
|
||
[Fact] | ||
public void FromTimeSpan() | ||
{ | ||
#if NETCOREAPP3_0_OR_GREATER | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw | ||
.Should().Be(Stopwatch.Frequency); | ||
} | ||
else | ||
{ | ||
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw | ||
.Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds); | ||
} | ||
#else | ||
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw | ||
.Should().Be(Stopwatch.Frequency); | ||
#endif | ||
} | ||
|
||
[Fact] | ||
public void RoundTripMilliseconds() | ||
{ | ||
Duration.FromMilliseconds(2000) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromMilliseconds(2000), TimeSpan.FromMilliseconds(50)); | ||
} | ||
|
||
[Fact] | ||
public void RoundTripSeconds() | ||
{ | ||
Duration.FromSeconds(2) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(50)); | ||
} | ||
|
||
[Fact] | ||
public void RoundTripMinutes() | ||
{ | ||
Duration.FromMinutes(2) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(100)); | ||
} | ||
|
||
[Fact] | ||
public void RoundTripHours() | ||
{ | ||
var d = Duration.FromHours(2); | ||
d.ToTimeSpan().Should().BeCloseTo(TimeSpan.FromHours(2), TimeSpan.FromMilliseconds(100)); | ||
Duration.FromHours(2) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromHours(2), TimeSpan.FromMilliseconds(100)); | ||
} | ||
|
||
[Fact] | ||
public void RoundTripDays() | ||
{ | ||
var d = Duration.FromDays(2); | ||
d.ToTimeSpan().Should().BeCloseTo(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); | ||
Duration.FromDays(2) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); | ||
} | ||
|
||
[Fact] | ||
public void OperatorPlus() | ||
{ | ||
(Duration.FromDays(2) + Duration.FromDays(2)) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromDays(4), TimeSpan.FromMilliseconds(100)); | ||
} | ||
|
||
[Fact] | ||
public void OperatorMinus() | ||
{ | ||
(Duration.FromDays(4) - Duration.FromDays(2)) | ||
.ToTimeSpan() | ||
.Should().BeCloseTo(TimeSpan.FromDays(2), TimeSpan.FromMilliseconds(100)); | ||
} | ||
|
||
[Fact] | ||
public void OperatorGreater() | ||
{ | ||
(Duration.FromDays(4) > Duration.FromDays(2)) | ||
.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OperatorLess() | ||
{ | ||
(Duration.FromDays(4) < Duration.FromDays(2)) | ||
.Should().BeFalse(); | ||
} | ||
|
||
// This is for diagnostic purposes when tests run on different operating systems. | ||
[Fact] | ||
public void OutputTimeParameters() | ||
{ | ||
this.testOutputHelper.WriteLine($"Stopwatch.Frequency {Stopwatch.Frequency}"); | ||
this.testOutputHelper.WriteLine($"TimeSpan.TicksPerSecond {TimeSpan.TicksPerSecond}"); | ||
this.testOutputHelper.WriteLine($"stopwatchAdjustmentFactor {StopwatchTickConverter.stopwatchAdjustmentFactor}"); | ||
var d = Duration.SinceEpoch(); | ||
this.testOutputHelper.WriteLine($"Duration.SinceEpoch {d.raw} ({d.ToTimeSpan()})"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.