forked from bellard/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbench.c
168 lines (131 loc) · 3.77 KB
/
qbench.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#if !defined(_WIN32)
#include <unistd.h>
#else
#include "win/sys_time.h"
#include <sys/utime.h>
#endif
#include "quickjs.h"
#include "monolithic_examples.h"
static uint64_t
njs_time(void)
{
struct timeval ts;
gettimeofday(&ts, NULL);
return (uint64_t) ts.tv_sec * 1000000 + ts.tv_usec;
}
#if defined(BUILD_MONOLITHIC)
#define main qjs_benchmark_main
#endif
int main(int argc, const char **argv)
{
JSRuntime *rt;
JSContext *ctx;
#if defined(RUSAGE_SELF)
struct rusage usage;
#endif
uint64_t iters = 10000000;
uint64_t ns = njs_time();
uint64_t ms;
#if 1
const char *str;
#define SRC "var COUNT= 5; function test() { return ['hello', 'world', ++COUNT].join(' '); }"
rt = JS_NewRuntime();
if (!rt) {
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
return 2;
}
ctx = JS_NewContext(rt);
if (!ctx) {
fprintf(stderr, "qjs: cannot allocate JS context\n");
return 2;
}
JSValue gcode = JS_Eval(ctx, (const char *) SRC, strlen(SRC), "<input>",
JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL);
if (JS_IsException(gcode)) {
JSValue val = JS_GetException(ctx);
str = JS_ToCString(ctx, val);
if (!str) {
fprintf(stderr, "qjs: cannot get exception after failed to compile\n");
return 2;
}
fprintf(stderr, "qjs: failed to compile: %s\n", str);
JS_FreeCString(ctx, str);
JS_FreeValue(ctx, val);
return 2;
}
gcode = JS_DupValue(ctx, gcode);
JSValue ret = JS_EvalFunction(ctx, gcode);
if (JS_IsException(ret)) {
fprintf(stderr, "qjs: failed to run\n");
return 2;
}
JS_FreeValue(ctx, ret);
JSValue global = JS_GetGlobalObject(ctx);
JSAtom atom = JS_NewAtom(ctx, "test");
JSValue func = JS_GetProperty(ctx, global, atom);
JS_FreeAtom(ctx, atom);
if (JS_IsException(func)) {
fprintf(stderr, "qjs: failed to get 'test'\n");
return 2;
}
if (!JS_IsFunction(ctx, func)) {
fprintf(stderr, "qjs: 'test' is not a function\n");
return 2;
}
for (int i = 0; i < iters; i++) {
JSValue val = JS_Call(ctx, func, global, 0, NULL);
str = JS_ToCString(ctx, val);
if (!str) {
fprintf(stderr, "qjs: cannot get value of the script\n");
return 2;
}
// fprintf(stderr, "qjs: %s\n", str);
JS_FreeCString(ctx, str);
JS_FreeValue(ctx, val);
}
JS_FreeValue(ctx, func);
JS_FreeValue(ctx, global);
JS_FreeValue(ctx, gcode);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
#else
for (int i = 0; i < iters; i++) {
rt = JS_NewRuntime();
if (!rt) {
fprintf(stderr, "qjs: cannot allocate JS runtime\n");
return 2;
}
ctx = JS_NewContext(rt);
if (!ctx) {
fprintf(stderr, "qjs: cannot allocate JS context\n");
return 2;
}
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
}
#endif
uint64_t total = njs_time() - ns;
ms = total / 1000000;
ns = total % 1000000;
printf("total: %lu.%06lums\n", (long)ms, (long)ns);
uint64_t periter = total / iters;
ms = periter / 1000000;
ns = periter % 1000000;
printf("per req: %lu.%06lums\n", (long)ms, (long)ns);
#if defined(RUSAGE_SELF)
getrusage(RUSAGE_SELF, &usage);
uint64_t us = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec
+ usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;
#else
uint64_t us = total;
#endif
printf("%.3fμs per VM, %d times/s\n",
(double) us / iters, (int) ((uint64_t) iters * 1000000 / us));
return 0;
}