2019-05-24 19:52:07 -07:00
|
|
|
const std = @import("std.zig");
|
2020-02-24 22:52:27 -08:00
|
|
|
const builtin = std.builtin;
|
2019-05-24 19:52:07 -07:00
|
|
|
const assert = std.debug.assert;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2019-05-24 19:52:07 -07:00
|
|
|
const os = std.os;
|
2019-05-27 09:16:32 -07:00
|
|
|
const math = std.math;
|
2020-05-24 17:06:56 -07:00
|
|
|
const is_windows = std.Target.current.os.tag == .windows;
|
2018-04-18 11:52:25 -07:00
|
|
|
|
2019-05-26 22:35:58 -07:00
|
|
|
pub const epoch = @import("time/epoch.zig");
|
2018-04-18 11:52:25 -07:00
|
|
|
|
2019-05-04 12:16:39 -07:00
|
|
|
/// Spurious wakeups are possible and no precision of timing is guaranteed.
|
2020-02-16 10:25:30 -08:00
|
|
|
/// TODO integrate with evented I/O
|
2018-10-10 23:02:59 -07:00
|
|
|
pub fn sleep(nanoseconds: u64) void {
|
2020-02-24 22:52:27 -08:00
|
|
|
if (is_windows) {
|
2019-05-27 09:16:32 -07:00
|
|
|
const big_ms_from_ns = nanoseconds / ns_per_ms;
|
|
|
|
const ms = math.cast(os.windows.DWORD, big_ms_from_ns) catch math.maxInt(os.windows.DWORD);
|
|
|
|
os.windows.kernel32.Sleep(ms);
|
|
|
|
return;
|
|
|
|
}
|
Add/fix missing WASI functionality to pass libstd tests
This rather large commit adds/fixes missing WASI functionality
in `libstd` needed to pass the `libstd` tests. As such, now by
default tests targeting `wasm32-wasi` target are enabled in
`test/tests.zig` module. However, they can be disabled by passing
the `-Dskip-wasi=true` flag when invoking the `zig build test`
command. When the flag is set to `false`, i.e., when WASI tests are
included, `wasmtime` with `--dir=.` is used as the default testing
command.
Since the majority of `libstd` tests were relying on `fs.cwd()`
call to get current working directory handle wrapped in `Dir`
struct, in order to make the tests WASI-friendly, `fs.cwd()`
call was replaced with `testing.getTestDir()` function which
resolved to either `fs.cwd()` for non-WASI targets, or tries to
fetch the preopen list from the WASI runtime and extract a
preopen for '.' path.
The summary of changes introduced by this commit:
* implement `Dir.makeDir` and `Dir.openDir` targeting WASI
* implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI
* fix `os.close` and map errors in `unlinkat`
* move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi`
to `std.os` module
* implement `lseek_{SET, CUR, END}` targeting WASI
* implement `futimens` targeting WASI
* implement `ftruncate` targeting WASI
* implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI
* make sure ANSI escape codes are _not_ used in stderr or stdout
in WASI, as WASI always sanitizes stderr, and sanitizes stdout if
fd is a TTY
* fix specifying WASI rights when opening/creating files/dirs
* tweak `AtomicFile` to be WASI-compatible
* implement `os.renameatWasi` for WASI-compliant `os.renameat` function
* implement sleep() targeting WASI
* fix `process.getEnvMap` targeting WASI
2020-05-05 08:23:49 -07:00
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
const w = std.os.wasi;
|
|
|
|
const userdata: w.userdata_t = 0x0123_45678;
|
|
|
|
const clock = w.subscription_clock_t{
|
|
|
|
.id = w.CLOCK_MONOTONIC,
|
|
|
|
.timeout = nanoseconds,
|
|
|
|
.precision = 0,
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
const in = w.subscription_t{
|
|
|
|
.userdata = userdata,
|
|
|
|
.u = w.subscription_u_t{
|
|
|
|
.tag = w.EVENTTYPE_CLOCK,
|
|
|
|
.u = w.subscription_u_u_t{
|
|
|
|
.clock = clock,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var event: w.event_t = undefined;
|
|
|
|
var nevents: usize = undefined;
|
2020-05-18 08:06:02 -07:00
|
|
|
_ = w.poll_oneoff(&in, &event, 1, &nevents);
|
|
|
|
return;
|
Add/fix missing WASI functionality to pass libstd tests
This rather large commit adds/fixes missing WASI functionality
in `libstd` needed to pass the `libstd` tests. As such, now by
default tests targeting `wasm32-wasi` target are enabled in
`test/tests.zig` module. However, they can be disabled by passing
the `-Dskip-wasi=true` flag when invoking the `zig build test`
command. When the flag is set to `false`, i.e., when WASI tests are
included, `wasmtime` with `--dir=.` is used as the default testing
command.
Since the majority of `libstd` tests were relying on `fs.cwd()`
call to get current working directory handle wrapped in `Dir`
struct, in order to make the tests WASI-friendly, `fs.cwd()`
call was replaced with `testing.getTestDir()` function which
resolved to either `fs.cwd()` for non-WASI targets, or tries to
fetch the preopen list from the WASI runtime and extract a
preopen for '.' path.
The summary of changes introduced by this commit:
* implement `Dir.makeDir` and `Dir.openDir` targeting WASI
* implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI
* fix `os.close` and map errors in `unlinkat`
* move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi`
to `std.os` module
* implement `lseek_{SET, CUR, END}` targeting WASI
* implement `futimens` targeting WASI
* implement `ftruncate` targeting WASI
* implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI
* make sure ANSI escape codes are _not_ used in stderr or stdout
in WASI, as WASI always sanitizes stderr, and sanitizes stdout if
fd is a TTY
* fix specifying WASI rights when opening/creating files/dirs
* tweak `AtomicFile` to be WASI-compatible
* implement `os.renameatWasi` for WASI-compliant `os.renameat` function
* implement sleep() targeting WASI
* fix `process.getEnvMap` targeting WASI
2020-05-05 08:23:49 -07:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:52:07 -07:00
|
|
|
const s = nanoseconds / ns_per_s;
|
|
|
|
const ns = nanoseconds % ns_per_s;
|
|
|
|
std.os.nanosleep(s, ns);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
/// Get a calendar timestamp, in seconds, relative to UTC 1970-01-01.
|
|
|
|
/// Precision of timing depends on the hardware and operating system.
|
|
|
|
/// The return value is signed because it is possible to have a date that is
|
|
|
|
/// before the epoch.
|
|
|
|
/// See `std.os.clock_gettime` for a POSIX timestamp.
|
|
|
|
pub fn timestamp() i64 {
|
2020-06-01 11:42:17 -07:00
|
|
|
return @divFloor(milliTimestamp(), ms_per_s);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
/// Get a calendar timestamp, in milliseconds, relative to UTC 1970-01-01.
|
|
|
|
/// Precision of timing depends on the hardware and operating system.
|
|
|
|
/// The return value is signed because it is possible to have a date that is
|
|
|
|
/// before the epoch.
|
|
|
|
/// See `std.os.clock_gettime` for a POSIX timestamp.
|
|
|
|
pub fn milliTimestamp() i64 {
|
|
|
|
return @intCast(i64, @divFloor(nanoTimestamp(), ns_per_ms));
|
2020-04-30 19:44:22 -07:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
/// Get a calendar timestamp, in nanoseconds, relative to UTC 1970-01-01.
|
|
|
|
/// Precision of timing depends on the hardware and operating system.
|
|
|
|
/// On Windows this has a maximum granularity of 100 nanoseconds.
|
|
|
|
/// The return value is signed because it is possible to have a date that is
|
|
|
|
/// before the epoch.
|
|
|
|
/// See `std.os.clock_gettime` for a POSIX timestamp.
|
|
|
|
pub fn nanoTimestamp() i128 {
|
2020-02-24 22:52:27 -08:00
|
|
|
if (is_windows) {
|
2020-05-24 17:06:56 -07:00
|
|
|
// FileTime has a granularity of 100 nanoseconds and uses the NTFS/Windows epoch,
|
|
|
|
// which is 1601-01-01.
|
|
|
|
const epoch_adj = epoch.windows * (ns_per_s / 100);
|
2019-05-24 19:52:07 -07:00
|
|
|
var ft: os.windows.FILETIME = undefined;
|
|
|
|
os.windows.kernel32.GetSystemTimeAsFileTime(&ft);
|
2019-11-06 20:25:57 -08:00
|
|
|
const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
|
2020-05-24 17:06:56 -07:00
|
|
|
return @as(i128, @bitCast(i64, ft64) + epoch_adj) * 100;
|
2019-05-24 19:52:07 -07:00
|
|
|
}
|
2020-02-24 22:52:27 -08:00
|
|
|
if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
2019-05-24 19:52:07 -07:00
|
|
|
var ns: os.wasi.timestamp_t = undefined;
|
|
|
|
const err = os.wasi.clock_time_get(os.wasi.CLOCK_REALTIME, 1, &ns);
|
|
|
|
assert(err == os.wasi.ESUCCESS);
|
2020-04-30 19:44:22 -07:00
|
|
|
return ns;
|
2019-05-24 19:52:07 -07:00
|
|
|
}
|
|
|
|
var ts: os.timespec = undefined;
|
2020-05-24 17:06:56 -07:00
|
|
|
os.clock_gettime(os.CLOCK_REALTIME, &ts) catch |err| switch (err) {
|
|
|
|
error.UnsupportedClock, error.Unexpected => return 0, // "Precision of timing depends on hardware and OS".
|
|
|
|
};
|
|
|
|
return (@as(i128, ts.tv_sec) * ns_per_s) + ts.tv_nsec;
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
// Divisions of a nanosecond.
|
|
|
|
pub const ns_per_us = 1000;
|
|
|
|
pub const ns_per_ms = 1000 * ns_per_us;
|
|
|
|
pub const ns_per_s = 1000 * ns_per_ms;
|
|
|
|
pub const ns_per_min = 60 * ns_per_s;
|
|
|
|
pub const ns_per_hour = 60 * ns_per_min;
|
|
|
|
pub const ns_per_day = 24 * ns_per_hour;
|
|
|
|
pub const ns_per_week = 7 * ns_per_day;
|
|
|
|
|
|
|
|
// Divisions of a microsecond.
|
|
|
|
pub const us_per_ms = 1000;
|
|
|
|
pub const us_per_s = 1000 * us_per_ms;
|
|
|
|
pub const us_per_min = 60 * us_per_s;
|
|
|
|
pub const us_per_hour = 60 * us_per_min;
|
|
|
|
pub const us_per_day = 24 * us_per_hour;
|
|
|
|
pub const us_per_week = 7 * us_per_day;
|
|
|
|
|
|
|
|
// Divisions of a millisecond.
|
2018-04-18 11:52:25 -07:00
|
|
|
pub const ms_per_s = 1000;
|
2020-05-24 17:06:56 -07:00
|
|
|
pub const ms_per_min = 60 * ms_per_s;
|
|
|
|
pub const ms_per_hour = 60 * ms_per_min;
|
|
|
|
pub const ms_per_day = 24 * ms_per_hour;
|
|
|
|
pub const ms_per_week = 7 * ms_per_day;
|
2018-04-18 11:52:25 -07:00
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
// Divisions of a second.
|
2018-04-18 11:52:25 -07:00
|
|
|
pub const s_per_min = 60;
|
|
|
|
pub const s_per_hour = s_per_min * 60;
|
|
|
|
pub const s_per_day = s_per_hour * 24;
|
|
|
|
pub const s_per_week = s_per_day * 7;
|
|
|
|
|
|
|
|
/// A monotonic high-performance timer.
|
|
|
|
/// Timer.start() must be called to initialize the struct, which captures
|
2020-05-24 17:06:56 -07:00
|
|
|
/// the counter frequency on windows and darwin, records the resolution,
|
|
|
|
/// and gives the user an opportunity to check for the existnece of
|
|
|
|
/// monotonic clocks without forcing them to check for error on each read.
|
2018-05-28 17:23:55 -07:00
|
|
|
/// .resolution is in nanoseconds on all platforms but .start_time's meaning
|
2020-05-24 17:06:56 -07:00
|
|
|
/// depends on the OS. On Windows and Darwin it is a hardware counter
|
|
|
|
/// value that requires calculation to convert to a meaninful unit.
|
2018-11-13 05:08:37 -08:00
|
|
|
pub const Timer = struct {
|
2019-05-24 19:52:07 -07:00
|
|
|
///if we used resolution's value when performing the
|
|
|
|
/// performance counter calc on windows/darwin, it would
|
|
|
|
/// be less precise
|
2020-02-24 22:52:27 -08:00
|
|
|
frequency: switch (builtin.os.tag) {
|
2019-05-24 19:52:07 -07:00
|
|
|
.windows => u64,
|
2019-05-26 20:35:26 -07:00
|
|
|
.macosx, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data,
|
2018-04-18 11:52:25 -07:00
|
|
|
else => void,
|
|
|
|
},
|
|
|
|
resolution: u64,
|
|
|
|
start_time: u64,
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
pub const Error = error{TimerUnsupported};
|
2019-05-24 19:52:07 -07:00
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
/// At some point we may change our minds on RAW, but for now we're
|
|
|
|
/// sticking with posix standard MONOTONIC. For more information, see:
|
|
|
|
/// https://github.com/ziglang/zig/pull/933
|
2019-05-24 19:52:07 -07:00
|
|
|
const monotonic_clock_id = os.CLOCK_MONOTONIC;
|
2020-05-24 17:06:56 -07:00
|
|
|
|
2018-04-19 08:01:41 -07:00
|
|
|
/// Initialize the timer structure.
|
2020-05-24 17:06:56 -07:00
|
|
|
/// Can only fail when running in a hostile environment that intentionally injects
|
|
|
|
/// error values into syscalls, such as using seccomp on Linux to intercept
|
|
|
|
/// `clock_gettime`.
|
2019-05-24 19:52:07 -07:00
|
|
|
pub fn start() Error!Timer {
|
2020-05-24 17:06:56 -07:00
|
|
|
// This gives us an opportunity to grab the counter frequency in windows.
|
|
|
|
// On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000.
|
|
|
|
// On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
|
|
|
|
// supported, or if the timespec pointer is out of bounds, which should be
|
|
|
|
// impossible here barring cosmic rays or other such occurrences of
|
|
|
|
// incredibly bad luck.
|
|
|
|
// On Darwin: This cannot fail, as far as I am able to tell.
|
2020-02-24 22:52:27 -08:00
|
|
|
if (is_windows) {
|
2020-05-24 17:06:56 -07:00
|
|
|
const freq = os.windows.QueryPerformanceFrequency();
|
|
|
|
return Timer{
|
|
|
|
.frequency = freq,
|
|
|
|
.resolution = @divFloor(ns_per_s, freq),
|
|
|
|
.start_time = os.windows.QueryPerformanceCounter(),
|
|
|
|
};
|
2019-10-23 22:06:03 -07:00
|
|
|
} else if (comptime std.Target.current.isDarwin()) {
|
2020-05-24 17:06:56 -07:00
|
|
|
var freq: os.darwin.mach_timebase_info_data = undefined;
|
|
|
|
os.darwin.mach_timebase_info(&freq);
|
|
|
|
|
|
|
|
return Timer{
|
|
|
|
.frequency = freq,
|
|
|
|
.resolution = @divFloor(freq.numer, freq.denom),
|
|
|
|
.start_time = os.darwin.mach_absolute_time(),
|
|
|
|
};
|
2019-05-24 19:52:07 -07:00
|
|
|
} else {
|
2020-05-24 17:06:56 -07:00
|
|
|
// On Linux, seccomp can do arbitrary things to our ability to call
|
|
|
|
// syscalls, including return any errno value it wants and
|
|
|
|
// inconsistently throwing errors. Since we can't account for
|
|
|
|
// abuses of seccomp in a reasonable way, we'll assume that if
|
|
|
|
// seccomp is going to block us it will at least do so consistently
|
|
|
|
var res: os.timespec = undefined;
|
|
|
|
os.clock_getres(monotonic_clock_id, &res) catch return error.TimerUnsupported;
|
2019-05-24 19:52:07 -07:00
|
|
|
|
2020-05-24 17:06:56 -07:00
|
|
|
var ts: os.timespec = undefined;
|
2019-05-24 19:52:07 -07:00
|
|
|
os.clock_gettime(monotonic_clock_id, &ts) catch return error.TimerUnsupported;
|
2020-05-24 17:06:56 -07:00
|
|
|
|
|
|
|
return Timer{
|
|
|
|
.resolution = @intCast(u64, res.tv_sec) * ns_per_s + @intCast(u64, res.tv_nsec),
|
|
|
|
.start_time = @intCast(u64, ts.tv_sec) * ns_per_s + @intCast(u64, ts.tv_nsec),
|
|
|
|
.frequency = {},
|
|
|
|
};
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
2019-05-24 19:52:07 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
return self;
|
|
|
|
}
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
/// Reads the timer value since start or the last reset in nanoseconds
|
2020-02-23 15:25:52 -08:00
|
|
|
pub fn read(self: Timer) u64 {
|
2018-04-18 11:52:25 -07:00
|
|
|
var clock = clockNative() - self.start_time;
|
2020-02-23 15:25:52 -08:00
|
|
|
return self.nativeDurationToNanos(clock);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
/// Resets the timer value to 0/now.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn reset(self: *Timer) void {
|
2018-04-18 11:52:25 -07:00
|
|
|
self.start_time = clockNative();
|
|
|
|
}
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
/// Returns the current value of the timer in nanoseconds, then resets it
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn lap(self: *Timer) u64 {
|
2018-04-18 11:52:25 -07:00
|
|
|
var now = clockNative();
|
2020-02-23 15:25:52 -08:00
|
|
|
var lap_time = self.nativeDurationToNanos(now - self.start_time);
|
2018-04-18 11:52:25 -07:00
|
|
|
self.start_time = now;
|
|
|
|
return lap_time;
|
|
|
|
}
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2019-05-24 19:52:07 -07:00
|
|
|
fn clockNative() u64 {
|
2020-02-24 22:52:27 -08:00
|
|
|
if (is_windows) {
|
2019-05-26 20:35:26 -07:00
|
|
|
return os.windows.QueryPerformanceCounter();
|
2019-05-24 19:52:07 -07:00
|
|
|
}
|
2019-10-23 22:06:03 -07:00
|
|
|
if (comptime std.Target.current.isDarwin()) {
|
2019-05-26 20:35:26 -07:00
|
|
|
return os.darwin.mach_absolute_time();
|
2019-05-24 19:52:07 -07:00
|
|
|
}
|
|
|
|
var ts: os.timespec = undefined;
|
|
|
|
os.clock_gettime(monotonic_clock_id, &ts) catch unreachable;
|
2019-11-06 20:25:57 -08:00
|
|
|
return @intCast(u64, ts.tv_sec) * @as(u64, ns_per_s) + @intCast(u64, ts.tv_nsec);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
2020-02-23 15:25:52 -08:00
|
|
|
|
|
|
|
fn nativeDurationToNanos(self: Timer, duration: u64) u64 {
|
2020-02-24 22:52:27 -08:00
|
|
|
if (is_windows) {
|
2020-02-23 15:25:52 -08:00
|
|
|
return @divFloor(duration * ns_per_s, self.frequency);
|
|
|
|
}
|
|
|
|
if (comptime std.Target.current.isDarwin()) {
|
|
|
|
return @divFloor(duration * self.frequency.numer, self.frequency.denom);
|
|
|
|
}
|
|
|
|
return duration;
|
|
|
|
}
|
2018-04-18 11:52:25 -07:00
|
|
|
};
|
|
|
|
|
2019-05-24 19:52:07 -07:00
|
|
|
test "sleep" {
|
2018-10-10 23:02:59 -07:00
|
|
|
sleep(1);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:52:07 -07:00
|
|
|
test "timestamp" {
|
Add/fix missing WASI functionality to pass libstd tests
This rather large commit adds/fixes missing WASI functionality
in `libstd` needed to pass the `libstd` tests. As such, now by
default tests targeting `wasm32-wasi` target are enabled in
`test/tests.zig` module. However, they can be disabled by passing
the `-Dskip-wasi=true` flag when invoking the `zig build test`
command. When the flag is set to `false`, i.e., when WASI tests are
included, `wasmtime` with `--dir=.` is used as the default testing
command.
Since the majority of `libstd` tests were relying on `fs.cwd()`
call to get current working directory handle wrapped in `Dir`
struct, in order to make the tests WASI-friendly, `fs.cwd()`
call was replaced with `testing.getTestDir()` function which
resolved to either `fs.cwd()` for non-WASI targets, or tries to
fetch the preopen list from the WASI runtime and extract a
preopen for '.' path.
The summary of changes introduced by this commit:
* implement `Dir.makeDir` and `Dir.openDir` targeting WASI
* implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI
* fix `os.close` and map errors in `unlinkat`
* move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi`
to `std.os` module
* implement `lseek_{SET, CUR, END}` targeting WASI
* implement `futimens` targeting WASI
* implement `ftruncate` targeting WASI
* implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI
* make sure ANSI escape codes are _not_ used in stderr or stdout
in WASI, as WASI always sanitizes stderr, and sanitizes stdout if
fd is a TTY
* fix specifying WASI rights when opening/creating files/dirs
* tweak `AtomicFile` to be WASI-compatible
* implement `os.renameatWasi` for WASI-compliant `os.renameat` function
* implement sleep() targeting WASI
* fix `process.getEnvMap` targeting WASI
2020-05-05 08:23:49 -07:00
|
|
|
const margin = ns_per_ms * 50;
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 15:43:35 -07:00
|
|
|
const time_0 = milliTimestamp();
|
2018-10-10 23:02:59 -07:00
|
|
|
sleep(ns_per_ms);
|
2018-04-18 15:43:35 -07:00
|
|
|
const time_1 = milliTimestamp();
|
2018-04-18 11:52:25 -07:00
|
|
|
const interval = time_1 - time_0;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(interval > 0 and interval < margin);
|
2018-04-18 11:52:25 -07:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:52:07 -07:00
|
|
|
test "Timer" {
|
2018-05-30 11:38:41 -07:00
|
|
|
const margin = ns_per_ms * 150;
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
var timer = try Timer.start();
|
2018-10-10 23:02:59 -07:00
|
|
|
sleep(10 * ns_per_ms);
|
2018-04-18 11:52:25 -07:00
|
|
|
const time_0 = timer.read();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(time_0 > 0 and time_0 < margin);
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
const time_1 = timer.lap();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(time_1 >= time_0);
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2018-04-18 11:52:25 -07:00
|
|
|
timer.reset();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(timer.read() < time_1);
|
2018-04-22 15:11:50 -07:00
|
|
|
}
|