-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark-runner.cxx
More file actions
392 lines (333 loc) · 15.1 KB
/
benchmark-runner.cxx
File metadata and controls
392 lines (333 loc) · 15.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <benchmark/benchmark.h>
#include <jni.h>
#include <threads.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "benchmark-utils.h"
#include "graal_isolate.h"
#include "haversine.h"
#include "libbenchmark-runner.h"
#include "polyglot_scripts.h"
graal_isolate_t* isolate = nullptr;
graal_isolatethread_t* isolate_thread = nullptr;
JavaVM* jvm = nullptr;
JNIEnv* env = nullptr;
jobject context;
jclass javaDistanceClass;
jclass rubyDistanceClass;
jmethodID evalMethod;
jclass doubleClass;
jmethodID doubleValueOfMethod;
jmethodID asDoubleMethod;
jmethodID executeMethod;
volatile double A_LAT = 51.507222;
volatile double A_LONG = -0.1275;
volatile double B_LAT = 40.7127;
volatile double B_LONG = -74.0059;
static void DoCEntrySetup(const benchmark::State& state) {
if (isolate_thread == nullptr) {
#ifdef DUMP_GRAAL_GRAPHS
// This is a big hack. There is no guarantee that arguments can be passed
// to the isolate this way. The `_reserved_` prefix on the field names is
// a clear indication that none of this should be relied upon. With that
// said, barring an official way of passing JVM options, this approach does
// work with GraalVM 22.1.0 Community Edition.
char* args[2] = {(char*)"-XX:+ParseRuntimeOptions",
(char*)"-XX:Dump=Truffle:1"};
graal_create_isolate_params_t isolate_params;
isolate_params.version = 3;
isolate_params._reserved_1 = 2;
isolate_params._reserved_2 = args;
if (graal_create_isolate(&isolate_params, &isolate, &isolate_thread) != 0) {
std::cerr << "initialization error\n";
std::exit(1);
}
#else
if (graal_create_isolate(NULL, &isolate, &isolate_thread) != 0) {
std::cerr << "initialization error\n";
std::exit(1);
}
#endif
}
}
static void DoCEntryTeardown(const benchmark::State& state) {
#ifndef REUSE_CONTEXT
tear_down_isolate(isolate_thread);
isolate_thread = nullptr;
#endif
}
static void DoJNISetup(const benchmark::State& state) {
if (jvm == nullptr) {
JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = (char*)"-XX:Dump=Truffle:1";
vm_args.version = JNI_VERSION_10;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
#ifdef DUMP_GRAAL_GRAPHS
vm_args.nOptions = 1;
#else
vm_args.nOptions = 0;
#endif
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
delete[] options;
doubleClass = env->FindClass("java/lang/Double");
jclass stringClass = env->FindClass("java/lang/String");
jclass contextClass = env->FindClass("org/graalvm/polyglot/Context");
jclass builderClass =
env->FindClass("org/graalvm/polyglot/Context$Builder");
jclass valueClass = env->FindClass("org/graalvm/polyglot/Value");
javaDistanceClass = env->FindClass("com/shopify/truffleruby/NativeLibrary");
rubyDistanceClass =
env->FindClass("com/shopify/truffleruby/NativeLibraryRuby");
// Create an empty java.lang.String[].
jstring initialElement = env->NewStringUTF("");
jobjectArray emptyArgs =
env->NewObjectArray(0, stringClass, initialElement);
// java.lang.Double methods.
doubleValueOfMethod =
env->GetStaticMethodID(doubleClass, "valueOf", "(D)Ljava/lang/Double;");
// org.graalvm.polyglot.Context methods.
evalMethod = env->GetMethodID(contextClass, "eval",
"(Ljava/lang/String;Ljava/lang/"
"CharSequence;)Lorg/graalvm/polyglot/Value;");
jmethodID newBuilderMethod = env->GetStaticMethodID(
contextClass, "newBuilder",
"([Ljava/lang/String;)Lorg/graalvm/polyglot/Context$Builder;");
// org.graalvm.polyglot.Context.Builder methods.
jmethodID allowExperimentalOptionsMethod =
env->GetMethodID(builderClass, "allowExperimentalOptions",
"(Z)Lorg/graalvm/polyglot/Context$Builder;");
jmethodID buildMethod = env->GetMethodID(
builderClass, "build", "()Lorg/graalvm/polyglot/Context;");
jmethodID optionMethod =
env->GetMethodID(builderClass, "option",
"(Ljava/lang/String;Ljava/lang/String;)Lorg/graalvm/"
"polyglot/Context$Builder;");
// org.graalvm.polyglot.Value methods.
asDoubleMethod = env->GetMethodID(valueClass, "asDouble", "()D");
executeMethod =
env->GetMethodID(valueClass, "execute",
"([Ljava/lang/Object;)Lorg/graalvm/polyglot/Value;");
// Build a polyglot context.
jobject builder =
env->CallStaticObjectMethod(contextClass, newBuilderMethod, emptyArgs);
builder = env->CallObjectMethod(builder, allowExperimentalOptionsMethod,
JNI_TRUE);
builder = env->CallObjectMethod(builder, optionMethod,
env->NewStringUTF("ruby.no-home-provided"),
env->NewStringUTF("true"));
context = env->CallObjectMethod(builder, buildMethod);
}
}
static void DoJNITeardown(const benchmark::State& state) {
#ifndef REUSE_CONTEXT
if (jvm != nullptr) {
jvm->DestroyJavaVM();
jvm = nullptr;
env = nullptr;
}
#endif
}
static void BM_CEntryJavaDistance(benchmark::State& state, double a_lat,
double a_long, double b_lat, double b_long) {
for (auto _ : state) {
distance(isolate_thread, a_lat, a_long, b_lat, b_long);
}
}
static void BM_CEntryRubyDistance(benchmark::State& state, double a_lat,
double a_long, double b_lat, double b_long) {
// Parse and evaluate the guest code once before entering the timing loop.
distance_ruby(isolate_thread, a_lat, a_long, b_lat, b_long);
for (auto _ : state) {
distance_ruby(isolate_thread, a_lat, a_long, b_lat, b_long);
}
}
static void BM_CEntryPolyglotDistance(benchmark::State& state,
const char* language, const char* code,
double a_lat, double a_long, double b_lat,
double b_long) {
// Parse and evaluate the guest code once before entering the timing loop.
distance_polyglot_no_cache(isolate_thread, (char*)language, (char*)code,
a_lat, a_long, b_lat, b_long);
for (auto _ : state) {
distance_polyglot_no_cache(isolate_thread, (char*)language, (char*)code,
a_lat, a_long, b_lat, b_long);
}
}
static void BM_CEntryPolyglotDistanceNoParseCache(benchmark::State& state,
const char* language,
const char* code,
double a_lat, double a_long,
double b_lat, double b_long) {
// Parse and evaluate the guest code once before entering the timing loop.
distance_polyglot_no_parse_cache(isolate_thread, (char*)language, (char*)code,
a_lat, a_long, b_lat, b_long);
for (auto _ : state) {
distance_polyglot_no_parse_cache(isolate_thread, (char*)language,
(char*)code, a_lat, a_long, b_lat, b_long);
}
}
static void BM_CEntryPolyglotDistanceThreadSafeParseCache(
benchmark::State& state, const char* language, const char* code,
double a_lat, double a_long, double b_lat, double b_long) {
// Parse and evaluate the guest code once before entering the timing loop.
distance_polyglot_thread_safe_parse_cache(isolate_thread, (char*)language,
(char*)code, a_lat, a_long, b_lat,
b_long);
for (auto _ : state) {
distance_polyglot_thread_safe_parse_cache(isolate_thread, (char*)language,
(char*)code, a_lat, a_long, b_lat,
b_long);
}
}
static void BM_CEntryPolyglotDistanceThreadUnsafeParseCache(
benchmark::State& state, const char* language, const char* code,
double a_lat, double a_long, double b_lat, double b_long) {
// Parse and evaluate the guest code once before entering the timing loop.
distance_polyglot_thread_unsafe_parse_cache(isolate_thread, (char*)language,
(char*)code, a_lat, a_long, b_lat,
b_long);
for (auto _ : state) {
distance_polyglot_thread_unsafe_parse_cache(isolate_thread, (char*)language,
(char*)code, a_lat, a_long,
b_lat, b_long);
}
}
static void BM_JNIJavaDistance(benchmark::State& state, double a_lat,
double a_long, double b_lat, double b_long) {
jmethodID javaDistanceMethod =
env->GetStaticMethodID(javaDistanceClass, "distance",
"(Lorg/graalvm/nativeimage/IsolateThread;DDDD)D");
// Parse and evaluate the guest code once before entering the timing loop.
env->CallStaticDoubleMethod(javaDistanceClass, javaDistanceMethod, nullptr,
a_lat, a_long, b_lat, b_long);
for (auto _ : state) {
env->CallStaticDoubleMethod(javaDistanceClass, javaDistanceMethod, nullptr,
a_lat, a_long, b_lat, b_long);
}
}
static void BM_JNIRubyDistance(benchmark::State& state, double a_lat,
double a_long, double b_lat, double b_long) {
jmethodID rubyDistanceMethod =
env->GetStaticMethodID(rubyDistanceClass, "distance",
"(Lorg/graalvm/nativeimage/IsolateThread;DDDD)D");
// Parse and evaluate the guest code once before entering the timing loop.
env->CallStaticDoubleMethod(rubyDistanceClass, rubyDistanceMethod, nullptr,
a_lat, a_long, b_lat, b_long);
for (auto _ : state) {
env->CallStaticDoubleMethod(rubyDistanceClass, rubyDistanceMethod, nullptr,
a_lat, a_long, b_lat, b_long);
}
}
static void BM_JNIPolyglotDistance(benchmark::State& state,
const char* language, const char* code,
double a_lat, double a_long, double b_lat,
double b_long) {
jobject truffle_distance =
env->CallObjectMethod(context, evalMethod, env->NewStringUTF(language),
env->NewStringUTF(code));
CHECK_EXCEPTION(env);
jobjectArray distanceArgs = env->NewObjectArray(4, doubleClass, 0);
env->SetObjectArrayElement(
distanceArgs, 0,
env->CallStaticObjectMethod(doubleClass, doubleValueOfMethod, a_lat));
env->SetObjectArrayElement(
distanceArgs, 1,
env->CallStaticObjectMethod(doubleClass, doubleValueOfMethod, a_long));
env->SetObjectArrayElement(
distanceArgs, 2,
env->CallStaticObjectMethod(doubleClass, doubleValueOfMethod, b_lat));
env->SetObjectArrayElement(
distanceArgs, 3,
env->CallStaticObjectMethod(doubleClass, doubleValueOfMethod, b_long));
// Parse and evaluate the guest code once before entering the timing loop.
jobject truffle_result =
env->CallObjectMethod(truffle_distance, executeMethod, distanceArgs);
CHECK_EXCEPTION(env);
env->CallDoubleMethod(truffle_result, asDoubleMethod);
for (auto _ : state) {
jobject truffle_result =
env->CallObjectMethod(truffle_distance, executeMethod, distanceArgs);
CHECK_EXCEPTION(env);
env->CallDoubleMethod(truffle_result, asDoubleMethod);
}
}
static void BM_CppDistance(benchmark::State& state, double a_lat, double a_long,
double b_lat, double b_long) {
for (auto _ : state) {
haversine_distance(a_lat, a_long, b_lat, b_long);
}
}
BENCHMARK_CAPTURE(BM_CppDistance, placeholder, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("C++");
BENCHMARK_CAPTURE(BM_CEntryJavaDistance, placeholder, A_LAT, A_LONG, B_LAT,
B_LONG)
->Name("@CEntryPoint: Java")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryRubyDistance, placeholder, A_LAT, A_LONG, B_LAT,
B_LONG)
->Name("@CEntryPoint: Ruby")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistance, placeholder, "ruby",
RUBY_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (Ruby)")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistance, placeholder, "js",
JS_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (JS)")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceNoParseCache, placeholder, "ruby",
RUBY_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (Ruby) - No Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceNoParseCache, placeholder, "js",
JS_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (JS) - No Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceThreadSafeParseCache, placeholder,
"ruby", RUBY_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (Ruby) - Safe Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceThreadSafeParseCache, placeholder,
"js", JS_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (JS) - Safe Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceThreadUnsafeParseCache, placeholder,
"ruby", RUBY_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (Ruby) - Unsafe Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_CEntryPolyglotDistanceThreadUnsafeParseCache, placeholder,
"js", JS_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("@CEntryPoint: Polyglot (JS) - Unsafe Parse Cache")
->Setup(DoCEntrySetup)
->Teardown(DoCEntryTeardown);
BENCHMARK_CAPTURE(BM_JNIJavaDistance, placeholder, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("JNI: Java")
->Setup(DoJNISetup)
->Teardown(DoJNITeardown);
BENCHMARK_CAPTURE(BM_JNIRubyDistance, placeholder, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("JNI: Ruby")
->Setup(DoJNISetup)
->Teardown(DoJNITeardown);
BENCHMARK_CAPTURE(BM_JNIPolyglotDistance, placeholder, "ruby",
RUBY_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("JNI: Polyglot (Ruby)")
->Setup(DoJNISetup)
->Teardown(DoJNITeardown);
BENCHMARK_CAPTURE(BM_JNIPolyglotDistance, placeholder, "js",
JS_HAVERSINE_DISTANCE, A_LAT, A_LONG, B_LAT, B_LONG)
->Name("JNI: Polyglot (JS)")
->Setup(DoJNISetup)
->Teardown(DoJNITeardown);
BENCHMARK_MAIN();