-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_sunspider_like.c
139 lines (118 loc) · 4.23 KB
/
run_sunspider_like.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
/*
* QuickJS Javascript Engine Benchmark Suite
*
* Copyright (c) 2023 Divy Srivastava
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "quickjs/quickjs.h"
double total = 0;
int Execute(JSContext *ctx, const char *filename) {
clock_t start_time, end_time;
double execution_time;
/* assumes max test script size */
static char buf[8 << 20]; // 8 MB
FILE *file = fopen(filename, "r");
if (file == NULL)
return -1;
size_t nread = fread(buf, 1, sizeof(buf), file);
fclose(file);
if (nread == 0) {
fprintf(stderr, "fread failed: %s\n", filename);
return -1;
}
if (nread == sizeof(buf)) {
fprintf(stderr, "file too large: %s\n", filename);
return -1;
}
buf[nread] = '\0';
start_time = clock();
JSValue ret_val = JS_Eval(ctx, buf, nread, filename, JS_EVAL_TYPE_GLOBAL);
end_time = clock();
execution_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
total += execution_time;
printf("%s (%.4f seconds)\n", filename, execution_time);
if (JS_IsException(ret_val)) {
JSValue exception = JS_GetException(ctx);
if (JS_IsError(ctx, exception)) {
JSValue stack;
const char *stack_str;
stack = JS_GetPropertyStr(ctx, exception, "stack");
if (!JS_IsUndefined(stack)) {
stack_str = JS_ToCString(ctx, stack);
if (stack_str) {
printf("%s\n", stack_str);
JS_FreeCString(ctx, stack_str);
}
}
JS_FreeValue(ctx, stack);
}
JS_FreeValue(ctx, exception);
return -1;
}
JS_FreeValue(ctx, ret_val);
return 0;
}
void RunTest(JSContext* ctx, char* path, char* buffer) {
char fullpath[256];
snprintf(fullpath, sizeof(fullpath), "%s%s-data.js", path, buffer);
/* Pass, only used for kraken. */
Execute(ctx, fullpath);
snprintf(fullpath, sizeof(fullpath), "%s%s.js", path, buffer);
if (Execute(ctx, fullpath) != 0) {
fprintf(stderr, "Error executing file: %s\n", buffer);
}
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <benchmark folder> [optional: name]\n", argv[0]);
return 1;
}
JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
/* disable stack limit */
JS_SetMaxStackSize(rt, 0);
char *path = argv[1];
char filename[256];
snprintf(filename, sizeof(filename), "%s%s", path, "LIST");
FILE *listFile = fopen(filename, "r");
if (listFile == NULL) {
fprintf(stderr, "Error opening file: %s\n", filename);
return 1;
}
char buffer[100]; // Adjust the buffer size as needed
while (fgets(buffer, sizeof(buffer), listFile) != NULL) {
size_t i = strcspn(buffer, "\n");
buffer[i] = '\0';
if (argc == 3 && strcmp(argv[2], buffer) != 0) {
printf("Skipping %s\n", buffer);
continue;
}
RunTest(ctx, path, buffer);
JS_RunGC(rt);
}
printf("\nTotal (ms): %.4f\n", total * 1000);
fclose(listFile);
JS_FreeContext(ctx);
return 0;
}