Skip to content

Unbreak microbench, add os.now() #93

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 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1948,22 +1948,33 @@ static JSValue js_os_signal(JSContext *ctx, JSValueConst this_val,
}

#if defined(__linux__) || defined(__APPLE__)
static int64_t get_time_ms(void)
static int64_t get_time_us(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
return (int64_t)ts.tv_sec * 1000000 + (ts.tv_nsec / 1000);
}
#else
/* more portable, but does not work if the date is updated */
static int64_t get_time_ms(void)
static int64_t get_time_us(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
}
#endif

static int64_t get_time_ms(void)
{
return get_time_us() / 1000;
}

static JSValue js_os_now(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_NewInt64(ctx, get_time_us());
}

static void unlink_timer(JSRuntime *rt, JSOSTimer *th)
{
if (th->link.prev) {
Expand Down Expand Up @@ -3606,6 +3617,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
OS_FLAG(SIGTTIN),
OS_FLAG(SIGTTOU),
#endif
JS_CFUNC_DEF("now", 0, js_os_now ),
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ),
JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ),
Expand Down
3 changes: 2 additions & 1 deletion tests/microbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* THE SOFTWARE.
*/
import * as std from "std";
import * as os from "os";

function pad(str, n) {
str += "";
Expand Down Expand Up @@ -97,7 +98,7 @@ var clocks_per_sec = 1000000;
var max_iterations = 100;
var clock_threshold = 2000; /* favoring short measuring spans */
var min_n_argument = 1;
var get_clock = Date.now;
var get_clock = os.now;

function log_one(text, n, ti) {
var ref;
Expand Down